我正在開發包含許多圖像爲精靈的Android遊戲。在Android中縮放圖像會導致OutOfMemory異常
當我加載圖像的方式如下:
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
爲什麼結垢造成內存溢出的例外?我甚至嘗試回收原始圖像以節省一些空間。我沒有故意使用Bitmap.createScaledBitmap(...)
,因爲此方法會在內部泄漏內存(如其他在線資源中所述)。
謝謝你在前進,
茲拉特科
爲什麼第一個代碼片段永遠不會導致OOM異常?事實上,在第二個片段中,我將原始位圖縮放到其大小的80%。分配,有2.5MB,而不是25MB – Bart 2011-05-28 00:54:33