0

经过研究,我只找到 InSampleSize 来调整位图的大小

但我正在寻找可以让我将位图大小调整为更大或更小的东西,具体取决于屏幕分辨率

由于 bitmap.createBitmap 会导致 OOM,我必须使用其他东西......

请帮忙

这是我调整位图大小的代码,每次调整位图大小时都会产生 5mb~10mb 的凹凸

Bitmap createdBitmap = Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, true);

谢谢

4

1 回答 1

1

您可以使用它来获取当前正在运行的屏幕:

@Override
public void onSizeChanged(int w, int h, int oldw, int oldh)
{
    super.onSizeChanged(w, h, oldw, oldh);
    screenW = w;
    screenH = h;
}

然后使用它来加载位图,以便您将其合理地加载到内存中:

 BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(selectedImagePath, options);
        int imageHeight = options.outHeight;
        int imageWidth = options.outWidth;
        String imageType = options.outMimeType;
        if(imageWidth > imageHeight){
            options.inSampleSize = calculateInSampleSize(options,screenH,screenW);

        }else{
            options.inSampleSize = calculateInSampleSize(options,screenW,screenH);

        }
        options.inJustDecodeBounds = false;
        photo = BitmapFactory.decodeFile(selectedImagePath,options);

方法:

public int calculateInSampleSize(
        BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;

if (height > reqHeight || width > reqWidth) {

    // Calculate ratios of height and width to requested height and width
    final int heightRatio = Math.round((float) height / (float) reqHeight);
    final int widthRatio = Math.round((float) width / (float) reqWidth);

    // Choose the smallest ratio as inSampleSize value, this will guarantee
    // a final image with both dimensions larger than or equal to the
    // requested height and width.
    inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}

return inSampleSize;
}
于 2013-04-10T02:24:42.170 回答