多くの画像をスプライトとして構成する 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(...)
このメソッドは内部でメモリ リークを行うため (他のオンライン リソースで説明されているように)、意図的に使用しているわけではありません。
前もってありがとう、
ズラトコ