2012-03-22 18 views
0

ビットマップの配列を特定の割合の画面にリサイズしています(すべてのデバイスで同じように見えます)。いくつかのビットマップは、サイズが+ 256kbのスプライト(爆発など)です。ビットマップが範囲外のエラー

明らかに、ビットマップが2回変換されると、VMのメモリが不足しています。ビットマップはアンドロイドアプリケーションの先頭でのみ変換されますが、まだエラーが発生しています。

誰でも教えてください。このコードをビットマップとして返​​す、より良い、より速く、より効果的な方法がありますか?

好奇心から逃れたのは、参照渡しのビットマップ値ですか? (オブジェクト・パラメーターは、同じオブジェクトに対して同じ行のメモリーを使用しますか?)。

は、とにかくここにあるzのコードです:

public Bitmap ResizeBitmap(Bitmap bitmap, float s_percentage, int frames, int viewport_width, int viewport_height) 
{ 
    float percentage = s_percentage/100.0f; 

    float scale = viewport_width/100 * percentage; 
    if(viewport_width < viewport_height) 
    { 
     scale = viewport_height/100 * percentage; 
    } 

    int newWidth = (int) (bitmap.getWidth() * scale); 
    int newHeight = (int) (bitmap.getHeight() * scale); 

    if(newWidth <= 0 || newHeight <= 0) 
    { 
     // Extra check, for invalid width/height 
     Log.e("Function List, Resize Bitmap", "invalid dimension ("+newWidth+"x"+newHeight+")"); 
     return bitmap; 
    } 

    //Round up to closet factor of total frames 
    int rW = (newWidth/frames)+1; 
    newWidth = rW*frames; 

    Bitmap newBitmap = Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, false); 

    return newBitmap; 
} 

答えて

1

このようなあなたのビットマップを縮小しようとするVMの予算であるために。

BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 
    BitmapFactory.decodeFile(filename, options); 
     options.inJustDecodeBounds = false; 
     options.inSampleSize = 4; 

     bitmap = BitmapFactory.decodeFile(filename, options); 
     if (bitmap != null) { 
      bitmap = Bitmap.createScaledBitmap(bitmap, width, height, false); 
     } 

// 2、4、8等

ような値にSampleSizeを調整
関連する問題