Android绘图缩放了Bitmap而没有OutOfMemory错误

时间:2016-02-23 17:11:23

标签: android canvas bitmap out-of-memory drawbitmap

我需要将我的位图缩放到screenSizeAverage / 3.当我这样做时,我有时会出现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错误,最好的方法是什么?

1 个答案:

答案 0 :(得分: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);

}

根据您的情况修改。