android / graphics-samples

Multiple samples showing the best practices in graphics on Android.
Apache License 2.0
311 stars 134 forks source link

DisplayingBitmaps: Fix calculation of inSampleSize #36

Closed yaraki closed 4 years ago

yaraki commented 4 years ago

Fixes #2

SundaramKrishna commented 4 years ago

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;

}

Above snippet found in old records, May i know what problem in this ?