2013-08-01 34 views
2

即時通訊存在高分辨率圖像問題。在設備上創建位圖時內存不足

對於1280x720圖像,我使用nodpi繪圖文件夾並使用此代碼對其進行縮放。

public static Drawable scaleDrawable(Drawable d, int width, Activity cxt) 
    { 
     BitmapDrawable bd = (BitmapDrawable)d; 

     double oldWidth = bd.getBitmap().getWidth(); 
     double scaleFactor = width/oldWidth; 

     int newHeight = (int) (d.getIntrinsicHeight() * scaleFactor); 
     int newWidth = (int) (oldWidth * scaleFactor); 

     Drawable drawable = new BitmapDrawable(cxt.getResources(),MainScreen.getResizedBitmap(bd.getBitmap(),newHeight,newWidth)); 

     BitmapDrawable bd2 = (BitmapDrawable)drawable; 

     return drawable; 
    } 

    public static Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) { 

     int width = bm.getWidth(); 
     int height = bm.getHeight(); 

     float scaleWidth = ((float) newWidth)/width; 
     float scaleHeight = ((float) newHeight)/height; 

     // create a matrix for the manipulation 
     Matrix matrix = new Matrix(); 

     // resize the bit map 
     matrix.postScale(scaleWidth, scaleHeight); 

     // recreate the new Bitmap 
     Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false); 

     return resizedBitmap; 
     } 

我用代碼來縮放圖像篩選witdh這樣即使屏幕分辨率320x480是圖像將擴展到320,並保持比例,我不關心,如果圖像走出屏幕的從下。

它的所有工作都很好,但是在xhdpi設備上嘗試時,特別是三星Galaxy Note 2,屏幕大小恰好爲720x1280。

它崩潰與內存異常的線路:

Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false); 

我無法理解爲什麼,圖像應相應從720到720,但我的代碼必須是非常糟糕的優化或東西。

我還沒有試過1080x1920設備,但它似乎也會崩潰。

查看代碼時有人會看到不好的東西?

回答

7

使用這種方法來調整你的bitmap-

Bitmap bm=decodeSampledBitmapFromPath(src, reqWidth, reqHeight); 

使用本Defination-

public Bitmap decodeSampledBitmapFromPath(String path, int reqWidth, 
    int reqHeight) { 

final BitmapFactory.Options options = new BitmapFactory.Options(); 
options.inJustDecodeBounds = true; 
BitmapFactory.decodeFile(path, options); 

options.inSampleSize = calculateInSampleSize(options, reqWidth, 
     reqHeight); 

// Decode bitmap with inSampleSize set 
options.inJustDecodeBounds = false; 
Bitmap bmp = BitmapFactory.decodeFile(path, options); 
return bmp; 
} 
} 
    public int calculateInSampleSize(BitmapFactory.Options options, 
    int reqWidth, int reqHeight) { 

final int height = options.outHeight; 
final int width = options.outWidth; 
int inSampleSize = 1; 

if (height > reqHeight || width > reqWidth) { 
    if (width > height) { 
     inSampleSize = Math.round((float) height/(float) reqHeight); 
    } else { 
     inSampleSize = Math.round((float) width/(float) reqWidth); 
    } 
} 
return inSampleSize; 
} 

如果您使用的資源,那麼替換法BitmapFactory的decodeResource方法..

public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, 
    int reqWidth, int reqHeight) { 

.... 
..... 
return BitmapFactory.decodeResource(res, resId, options); 
} 
+0

我的圖像可繪製存儲在drawable-nodpi中,該方法從路徑使用decodeFile將適用於我?如何? – Nanoc

+0

使用BitmapFactory.decodeResource(Resources res,int id),您可以從文件夾訪問drawable。 – Horschtele

0

您應該使用工具類BitmapFactory進行圖像處理操作。還可以使用BitmapFactory.Options來調整位圖的輸入/輸出大小。在不再需要位圖之後,您應該釋放相關的內存。