Pet*_*urg 38 java android memory-leaks bitmap
我有这样的事情:
Bitmap.Config conf = Bitmap.Config.ARGB_8888;
WeakReference<Bitmap> bm = new WeakReference<Bitmap>(Bitmap.createBitmap(3000 + 3000, 2000, conf));
Canvas canvas = new Canvas(bm.get());
canvas.drawBitmap(firstBitmap, 0, 0, null);
canvas.drawBitmap(bm, firstBitmap.getWidth(), 0, null);
imageView.setImageBitmap(bm);
Run Code Online (Sandbox Code Playgroud)
我将它应用于10个以上逐个创建的imageView.每当我创建新的ImageView时,我想从第一个回收'bm'对象,导致这个代码在那里,导致我的堆越来越多,然后抛出OutOfMemoryError,所以我这样做:
bm.recycle()
Run Code Online (Sandbox Code Playgroud)
在我将Bitmap(bm)设置为imageView对象之后.这导致ImageView的画布想要绘制回收的Bitmap的异常.
回收已在ImageView上作为图像放置的Bitmap的方法是什么?
Thanksb
dev*_*ole 56
在你的onDestroy方法中你可以尝试这样的事情:
ImageView imageView = (ImageView)findViewById(R.id.my_image);
Drawable drawable = imageView.getDrawable();
if (drawable instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
Bitmap bitmap = bitmapDrawable.getBitmap();
bitmap.recycle();
}
Run Code Online (Sandbox Code Playgroud)
由于setImageBitmap实现为,因此强制转换应该有效
public void setImageBitmap(Bitmap bm) {
setImageDrawable(new BitmapDrawable(mContext.getResources(), bm));
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
37122 次 |
| 最近记录: |