为什么图像的宽度和高度减半以计算图像的inSampleSize?

时间:2017-04-11 20:08:49

标签: android bitmap

以下代码取自Android开发者网站上的here

在代码中,calculateInSampleSize方法用于计算图像的inSampleSize以缩小图像并对其进行子采样。

我只是想知道在最终的heightwidth

中除以2的原因
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  
        // height and width larger than the requested height and width.
        while ((halfHeight / inSampleSize) >= reqHeight && (halfWidth / inSampleSize) >= reqWidth) {
            inSampleSize *= 2;
        }
    }   
    return inSampleSize;
}

0 个答案:

没有答案