0

我目前正在研究一个我想旋转位图的项目。

第一次,我使用以下代码创建位图:

myBitmap = BitmapFactory.decodeResource(getResources(), drawableResource);

然后我使用以下代码旋转位图:

final Matrix matrix = new Matrix();
matrix.postRotate(currentRotate);
myBitmap = Bitmap.createBitmap(myBitmap, 0, 0, directionBitmap.getWidth(), directionBitmap.getHeight(), matrix, true);

我工作,但几次后,内存增加,我有以下异常:

java.lang.OutOfMemoryError:无法分配 119071756 字节分配,其中 16775968 个可用字节和 96MB 直到 OOM

旧的位图似乎仍在内存中。如何删除/回收它们以节省内存?

感谢您的帮助。

4

2 回答 2

0

就我而言,问题在于我使用的位图的大小。对于给定的设备,我使用的位图具有非常高的像素。在这种情况下,Android 系统必须对它们进行除垢以降低像素密度,即适合您设备的像素密度。当您旋转位图时,Android 系统会占用大量内存以将其缩小为较低的像素。此外,Android 会变得很忙,在某些情况下会导致 UI 线程阻塞。此外,增加清单文件中的堆大小。

于 2017-08-19T21:27:09.297 回答
0

我可以建议你使用Github 的Glide库。

该库在后台线程中工作。无论如何,您可以像这样在背景上执行旋转:

runOnUiThread(new Runnable(final Matrix matrix = new Matrix();
matrix.postRotate(currentRotate);
myBitmap = Bitmap.createBitmap(myBitmap, 0, 0, directionBitmap.getWidth(), directionBitmap.getHeight(), matrix, true);
)
于 2017-08-19T21:32:29.963 回答