什么方法能改进BitmapFactory.de codeStream的速度()? [英] Any way to improve speed of BitmapFactory.decodeStream()?

查看:134
本文介绍了什么方法能改进BitmapFactory.de codeStream的速度()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

显然,这是一种昂贵的/耗时的操作。什么方法能改进吗?

Obviously this is an expensive/time-consuming operation. Any way to improve this?

Bitmap bm = BitmapFactory.decodeStream((InputStream) new URL(
                                            someUrl).getContent());

我猜真的没有办法避免这种比较激烈的操作,而是想看看如果任何人有任何调整,他们可以推荐(除了缓存实际的位图,它不管是什么原因根本不是与此有关)

I'm guessing there's really no way to avoid this relatively intense operation, but wanted to see if anyone had any tweaks they could recommend (aside from caching the actual bitmap, which for whatever reasons simply isn't relevant here)

推荐答案

如果您不需要全分辨率,你可以把它只能读取过第n个像素,其中n为2的幂。你可以通过设置 inSampleSize 上的<一个href="http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html">Options你传递给 BitmapFactory.de codeFILE 对象。你可以只通过在选项设置 inJustDe codeBounds 阅读从第一次通过文件的元数据发现样本量对象。除此之外 - 不,我不认为有一个简单的方法,使去任何比它已经这样做了快

If you don't need the full resolution you can have it only read ever nth pixel, where n is a power of 2. You do this by setting inSampleSize on an Options object which you pass to BitmapFactory.decodeFile. You can find the sample size by just reading the meta-data from the file in a first pass by setting inJustDecodeBounds on the Options object. Aside from that - no, I don't think there's an easy way to make to go any faster than it already does.

编辑,例如:

    Options opts = new Options();
    // Get bitmap dimensions before reading...
    opts.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(path, opts);
    int width = opts.outWidth;
    int height = opts.outHeight;
    int largerSide = Math.max(width, height);
    opts.inJustDecodeBounds = false; // This time it's for real!
    int sampleSize = ??; // Calculate your sampleSize here
    opts.inSampleSize = sampleSize;
    Bitmap bmp = BitmapFactory.decodeFile(path, opts);

这篇关于什么方法能改进BitmapFactory.de codeStream的速度()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆