加载大图像没有子采样android

时间:2015-03-11 12:52:10

标签: android imageview touchimageview

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

我在网上搜索过,发现我必须对图像进行子采样,以便在图像视图中加载。但我不想分样,因为在缩放图像时它看起来像素化。是否有任何方法可以加载图像而不会出现内存异常,也可以用于缩放功能。

1 个答案:

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