首页 文章

缩小大图像

提问于
浏览
-1

我正在关注this链接以加载大图像 . 所以根据这个链接我需要确定一个目标高度和宽度,即目标分辨率和图像分辨率不应超过这个目标分辨率,如果它超过那么我需要缩小它 . 所以根据函数 calculateInSampleSize

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

    final int halfHeight = height / 2;
    final int halfWidth = width / 2;

    // Calculate the largest inSampleSize value that is a power of 2 and keeps both
    // height and width larger than the requested height and width.
    while ((halfHeight / inSampleSize) >= reqHeight
            && (halfWidth / inSampleSize) >= reqWidth) {
        inSampleSize *= 2;
    }
}

return inSampleSize;

}

inSampleSize使用半高和宽度计算 . 我的问题是为什么我们在半高和haldf宽度的基础上进行计算,为什么不在这样的宽度上进行计算:

while ((height / inSampleSize) >= reqHeight
            && (width / inSampleSize) >= reqWidth) {
        inSampleSize *= 2;
    }

EDIT: 我认为我的问题被误解了 . 我想问一下为什么我们不在上面的例子中将reqHeight和reqWidth与图像的完整高度和宽度进行比较 . 为什么Android开发者页面在上面的例子中使用了halfWidth和halfImage?

1 回答

  • 0

    你可以在这里找到documentation,当采样发生时,它返回的图像是原始图像宽度/高度的1/2 ^ n . 因此,采样图像取决于高度和宽度来对其进行采样 .

    我们在高度和宽度的一半上工作,就像我们将高度和宽度除以2一样,就像我们正在用2进行采样,当这一半的图像小于要求的高度和宽度时,我们不需要对原始图像进行采样,就好像我们这样做一样,采样图像会被“模糊” .

相关问题