【发布时间】:2014-03-07 21:03:26
【问题描述】:
我有一个实用方法(如下),它可以调整位图的大小并返回一个新版本。由于我对很多图像执行此操作,并且我想减少内存不足的机会,因此我在使用后回收了位图。
这几乎适用于所有设备。但是,我注意到三星 Galaxy Tab 3(10 英寸)和注释 10.1(2014)我得到以下堆栈跟踪:
java.lang.IllegalArgumentException: Cannot draw recycled bitmaps
at android.view.GLES20Canvas.drawBitmap(GLES20Canvas.java:756)
at android.view.GLES20RecordingCanvas.drawBitmap(GLES20RecordingCanvas.java:104)
以下是我的调整大小代码:
private static Bitmap resizeBitmap(int newWidth, int newHeight, Bitmap bitmapResize) {
if (bitmapResize == null) {
return null;
}
int width = bitmapResize.getWidth();
int height = bitmapResize.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap resizedBitmap = Bitmap.createScaledBitmap(bitmapResize,
newWidth, newHeight, true);
//SHOULD I DO THIS???
bitmapResize.recycle();
return resizedBitmap;
}
我还没有弄清楚为什么几乎所有设备都可以工作,除了那 2 个(可能还有更多)。模拟器也没有问题。
值得注意的是,并非所有图像都会出现“无法绘制回收位图”错误。只有一些。但它始终是相同的图像。
(如果使用,我的应用程序运行在 2.2 以上)
【问题讨论】:
-
mm...也许我应该改写标题 - 我想我应该问我是否应该在调整大小时回收?
-
显然不是,因为你不能画然后...
标签: android