0

在我的应用程序中,我必须加载具有高分辨率示例(1500*1500)的图像。我正在使用 touchimageview 库来实现移动、双击缩放、捏缩放功能。当我想从我的本地资源加载图像时 BitmapFactory.decodeFileDescriptor() 抛出内存异常。

我在网上搜索过,发现我必须对图像进行子采样才能加载到图像视图中。但我不想分样本,因为在缩放图像时它看起来像素化。有没有什么方法可以加载图像而不会出现内存不足的异常,并且它也应该适用于缩放功能。

4

1 回答 1

0

您需要调整图像大小检查此方法

public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
    int reqWidth, int reqHeight) {

// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);

// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}


 public static 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;
}
于 2015-03-11T12:56:16.760 回答