サイズの大きい画像読み込み時のOutOfMemory回避
スライドパズルサンプルで、最初にサイズの大きな画像を読み込む場合にOutOfMemoryが発生しにくい対応の追加メモ。BitmapFactory.Options()を利用します。
OpenGL版コードだと、こんな感じ。
前略…(前記事参照) public void loadPicture(Context ctx) { Context context= ctx; Resources r=context.getResources(); int resID=R.drawable.picture; options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeResource(r,resID, options); int scaleW = options.outWidth / 480 + 1; int scaleH = options.outHeight / 320 + 1; int scale = Math.max(scaleW, scaleH); options.inJustDecodeBounds = false; options.inSampleSize = scale; image = BitmapFactory.decodeResource(r,resID, options); image = Bitmap.createScaledBitmap(image, 300, 300, true); 後略…(前記事参照)