From 47e3f029a4ff80d537011d6738f7cf8572956be8 Mon Sep 17 00:00:00 2001 From: Adam Koch Date: Fri, 25 Oct 2013 15:32:39 -0400 Subject: [PATCH] Bitmapfun Sample: Minor updates to match zip file in training class. Change-Id: I0fa13d5d8947532e9b5906b863ca75e5c85ac544 --- .../android/bitmapfun/util/ImageCache.java | 28 ++++++++++++------- .../android/bitmapfun/util/ImageResizer.java | 5 ---- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/samples/training/bitmapfun/BitmapFun/src/main/java/com/example/android/bitmapfun/util/ImageCache.java b/samples/training/bitmapfun/BitmapFun/src/main/java/com/example/android/bitmapfun/util/ImageCache.java index 7dd8e25ec..145c88188 100644 --- a/samples/training/bitmapfun/BitmapFun/src/main/java/com/example/android/bitmapfun/util/ImageCache.java +++ b/samples/training/bitmapfun/BitmapFun/src/main/java/com/example/android/bitmapfun/util/ImageCache.java @@ -506,20 +506,23 @@ public class ImageCache { private static boolean canUseForInBitmap( Bitmap candidate, BitmapFactory.Options targetOptions) { - if (Utils.hasKitKat()) { - int width = targetOptions.outWidth / targetOptions.inSampleSize; - int height = targetOptions.outHeight / targetOptions.inSampleSize; - int byteCount = width * height * getBytesPerPixel(candidate.getConfig()); - return byteCount <= candidate.getAllocationByteCount(); + if (!Utils.hasKitKat()) { + // On earlier versions, the dimensions must match exactly and the inSampleSize must be 1 + return candidate.getWidth() == targetOptions.outWidth + && candidate.getHeight() == targetOptions.outHeight + && targetOptions.inSampleSize == 1; } - return candidate.getWidth() == targetOptions.outWidth - && candidate.getHeight() == targetOptions.outHeight - && targetOptions.inSampleSize <= 1; + // From Android 4.4 (KitKat) onward we can re-use if the byte size of the new bitmap + // is smaller than the reusable bitmap candidate allocation byte count. + int width = targetOptions.outWidth / targetOptions.inSampleSize; + int height = targetOptions.outHeight / targetOptions.inSampleSize; + int byteCount = width * height * getBytesPerPixel(candidate.getConfig()); + return byteCount <= candidate.getAllocationByteCount(); } /** - * Return the byte usage per pixel of a bitmap based on it's configuration. + * Return the byte usage per pixel of a bitmap based on its configuration. * @param config The bitmap configuration. * @return The byte usage per pixel. */ @@ -528,6 +531,8 @@ public class ImageCache { return 4; } else if (config == Config.RGB_565) { return 2; + } else if (config == Config.ARGB_4444) { + return 2; } else if (config == Config.ALPHA_8) { return 1; } @@ -582,7 +587,10 @@ public class ImageCache { } /** - * Get the size in bytes of a bitmap in a BitmapDrawable. + * Get the size in bytes of a bitmap in a BitmapDrawable. Note that from Android 4.4 (KitKat) + * onward this returns the allocated memory size of the bitmap which can be larger than the + * actual bitmap data byte count (in the case it was re-used). + * * @param value * @return size in bytes */ diff --git a/samples/training/bitmapfun/BitmapFun/src/main/java/com/example/android/bitmapfun/util/ImageResizer.java b/samples/training/bitmapfun/BitmapFun/src/main/java/com/example/android/bitmapfun/util/ImageResizer.java index 596a991e7..e8ce4e12e 100644 --- a/samples/training/bitmapfun/BitmapFun/src/main/java/com/example/android/bitmapfun/util/ImageResizer.java +++ b/samples/training/bitmapfun/BitmapFun/src/main/java/com/example/android/bitmapfun/util/ImageResizer.java @@ -207,12 +207,7 @@ public class ImageResizer extends ImageWorker { Bitmap inBitmap = cache.getBitmapFromReusableSet(options); if (inBitmap != null) { - if (BuildConfig.DEBUG) { - Log.d(TAG, "Found bitmap to use for inBitmap"); - } options.inBitmap = inBitmap; - } else { - Log.d(TAG, "Did NOT find bitmap to use for inBitmap"); } } }