0

我需要将位图缩放到 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 错误的最佳方法是什么?

4

1 回答 1

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);

}

针对您的情况进行了修改。

于 2016-02-23T17:36:43.600 回答