无法理解这种比例位图代码

时间:2012-05-19 09:51:52

标签: android bitmap

在其中一个教程中,Google提出了以下缩放位图算法,以使位图适合给定的窗口:

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) {
        if (width > height) {
            inSampleSize = Math.round((float)height / (float)reqHeight);
        } else {
            inSampleSize = Math.round((float)width / (float)reqWidth);
        }
    }
    return inSampleSize;
}

http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

我不理解if(width > height)部分

假设位图的高度为5,宽度为2.然后假设窗口的高度为1,宽度为1.看起来位图不适合窗口。

1 个答案:

答案 0 :(得分:1)

确保尺寸比率得到尊重