简体   繁体   English

Android绘图比例位图没有OutOfMemory错误

[英]Android drawing scaled Bitmap without OutOfMemory error

I need scale my bitmap to screenSizeAverage / 3. When I do it like this, I have sometimes OutOfMemory error. 我需要将位图缩放为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);
}

What is the best way to do this without OutOfMemory error? 没有OutOfMemory错误的最佳方法是什么?

From developer.android.com 来自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);

}

Modified for your case. 根据您的情况进行修改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM