我正在开发一个由许多图像作为精灵组成的 Android 游戏。
当我通过以下方式加载图像时:
public static Bitmap loadBitmap(int resId) {
return BitmapFactory.decodeResource(getResources(), resId, options);
}
一切正常。
当我尝试使用此代码缩小或放大位图时:
public static Bitmap loadBitmap(int resId) {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), resId, options);
Matrix matrix = new Matrix();
matrix.postScale(0.8f, 0.8f);
Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
bitmap.recycle();
bitmap = null;
return scaledBitmap;
}
应用程序崩溃,但出现以下异常:
2211840-byte external allocation too large for this process.
Out of memory: Heap Size=4935KB, Allocated=2549KB, Bitmap Size=18463KB
VM won't let us allocate 2211840 bytes
为什么缩放会导致 OutOfMemory 异常?我什至尝试回收原始图像以节省一些空间。我不是Bitmap.createScaledBitmap(...)
故意使用的,因为这种方法会在内部发生内存泄漏(如其他在线资源中所述)。
提前谢谢你,
兹拉特科