2016-02-23 31 views
0

我需要縮放我的位圖screenSizeAverage/3.當我這樣做時,我有時會出現OutOfMemory錯誤。Android繪圖縮放位圖無OutOfMemory錯誤

screenWidth = size.x; 
screenHeight = size.y; 
screenSizeAverage = (screenWidth + screenHeight)/2; 
Bitmap b2 = BitmapFactory.decodeResource(getResources(), R.drawable.logoqrtz); 
logoqrtz = Bitmap.createScaledBitmap(b2, screenSizeAverage/3,screenSizeAverage/3, true); 
protected void onDraw(Canvas canvas) { 
    canvas.drawBitmap(logoqrtz, (int) (screenWidth/2, (int) (screenHeight /2), p); 
} 

沒有OutOfMemory錯誤的最好方法是什麼?

+0

你在該行canvas.drawBitmap(讓你的OOM ....? –

回答

0

developer.android.com

private Bitmap setPic() { 
    // Get the dimensions of the View 
    int targetW = size.x; 
    int targetH = size.y; 

    int size = ((screenWidth + screenHeight)/2)/3; 

    // Get the dimensions of the bitmap 
    BitmapFactory.Options bmOptions = new BitmapFactory.Options(); 
    bmOptions.inJustDecodeBounds = true; 
    BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions); 
    int photoW = bmOptions.outWidth; 
    int photoH = bmOptions.outHeight; 

    // Determine how much to scale down the image 
    int scaleFactor = Math.min(photoW/size, photoH/size); 

    // Decode the image file into a Bitmap sized to fill the View 
    bmOptions.inJustDecodeBounds = false; 
    bmOptions.inSampleSize = scaleFactor; 
    bmOptions.inPurgeable = true; 

    return BitmapFactory.decodeResource(getResources(), R.drawable.logoqrtz, bmOptions); 

} 

修改爲你的情況。