2012-03-22 113 views
0

我正在调整位图数组的大小到一定百分比的屏幕(因此在所有设备上看起来都是一样的)。一些位图是+ 256kb大小的精灵(爆炸等)。位图超出范围错误

显然,一旦位图转换了两次,虚拟机内存不足,位图仅在android应用程序的开始时进行转换,但仍然出现错误。

任何人都可以告诉我,有没有更好,更快,更高效的方法来将此代码作为位图返回。

只是出于好奇,是通过引用传递的位图值? (就像对象参数对同一对象使用同一行内存?)。

总之在这里为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等