安卓BitmapDrawable的回收和重用

3
在下面的代码中,我有两个位图(我省略了创建它们的代码,因为这与我的问题无关),还有一个ImageView在我的布局中。 我使ImageView显示第一个位图作为drawable,然后再次将其显示为第二个位图。

我知道位图可以被回收利用,我的问题与“new BitmapDrawable”部分相关,因为我无法确定BitmapDrawable到底是什么。 它只是一个引用吗?还是每次创建一个都会使用内存?换句话说,我为bitmap1创建的BitmapDrawable需要在创建另一个为bitmap2创建的BitmapDrawable之前被删除/回收吗?

谢谢。

Bitmap bitmap1,bitmap2;

...assume bitmap1 and bitmap2 contain valid bitmaps...

// get imageview
ImageView iv = (ImageView)findViewById(R.id.my_imageview);

// make the imageview display bitmap1
iv.setImageDrawable(new BitmapDrawable(getResources(),bitmap1));        

// now make the imageview display bitmap2
iv.setImageDrawable(new BitmapDrawable(getResources(),bitmap2));        

1
只是出于好奇:如果可以直接将位图设置到 ImageView 中,为什么要创建一个 BitmapDrawable?请参考ImageView.setImageBitmap(Bitmap)。如果你真的需要一个 Drawable,我建议使用 StateListDrawable,或者如果你喜欢在两个图像之间进行淡入淡出效果,则使用 TransitionDrawable。 - MH.
1个回答

1

4
我非常清楚这一点,但它与我的问题没有任何关系。 我不是在询问位图(bitmap),我特别询问的是可绘制对象(drawable)。它们并不相同。 - Rick

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接