OSDN Git Service

Displaying Bitmaps Efficiently Training - Fix inSampleSize selection
authorAdam Koch <akoch@google.com>
Wed, 9 Jan 2013 21:58:59 +0000 (16:58 -0500)
committerAdam Koch <akoch@google.com>
Wed, 9 Jan 2013 21:58:59 +0000 (16:58 -0500)
When computing inSampleSize, calculateInSampleSize() needs to compare
height/width ratios, rather than raw values.

Bug: 7951398
Change-Id: I207f9abc2aae4cc569b406bac237e221d8e64d1e

docs/downloads/training/BitmapFun.zip
docs/html/training/displaying-bitmaps/load-bitmap.jd

index e48bfd3..c4ea7aa 100644 (file)
Binary files a/docs/downloads/training/BitmapFun.zip and b/docs/downloads/training/BitmapFun.zip differ
index c0a5709..283f272 100644 (file)
@@ -110,12 +110,17 @@ public static int calculateInSampleSize(
     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);
-        }
+
+        // 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;
 }
 </pre>