1

I'm currently facing several performance issues (out-of-memory) when handling a vast amount of bitmaps. As this is just a problem that can be fixed I'm wondering if anybody can explain me the difference in using the following methods.

If I only want to load an image into an ImageView I usually use:

imageView.setImageDrawable(getResources.getDrawable(R.drawable.id));

If I want to sample the drawable beforehand I usually use (here without sampling):

Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.id);
imageView.setImageBitmap(bm);

My question is related to performance optimisation. I'm wondering whether it is better to provide as many drawables as possible using the different drawable folders (so these drawables nearly fit the required resolution for the different devices) or if it is better to sample high-quality drawables? What is setImageDrawable doing internally? Does it decode the resources using the BitmapFactory, just without sampling? There seems to be a trade-off between the actual size of the app and the cpu- and memory-load during runtime.

4

1 回答 1

0

如果您担心 apk 大小,那么拥有尽可能多的可绘制对象并不是理想的选择。但不要忘记,当你解码位图时,你可以传递一个样本大小,这样它就会缩小到屏幕大小,只给你你需要的像素,所以屏幕较小的旧手机不需要解码 8mp 图像。

检查BitmapFactory.Options这里

于 2013-11-05T14:39:32.577 回答