Bitmapfun Sample: Minor updates to match zip file in training class.

Change-Id: I0fa13d5d8947532e9b5906b863ca75e5c85ac544
This commit is contained in:
Adam Koch
2013-10-25 15:32:39 -04:00
parent 15a65401a8
commit 47e3f029a4
2 changed files with 18 additions and 15 deletions

View File

@@ -506,20 +506,23 @@ public class ImageCache {
private static boolean canUseForInBitmap( private static boolean canUseForInBitmap(
Bitmap candidate, BitmapFactory.Options targetOptions) { Bitmap candidate, BitmapFactory.Options targetOptions) {
if (Utils.hasKitKat()) { if (!Utils.hasKitKat()) {
int width = targetOptions.outWidth / targetOptions.inSampleSize; // On earlier versions, the dimensions must match exactly and the inSampleSize must be 1
int height = targetOptions.outHeight / targetOptions.inSampleSize; return candidate.getWidth() == targetOptions.outWidth
int byteCount = width * height * getBytesPerPixel(candidate.getConfig()); && candidate.getHeight() == targetOptions.outHeight
return byteCount <= candidate.getAllocationByteCount(); && targetOptions.inSampleSize == 1;
} }
return candidate.getWidth() == targetOptions.outWidth // From Android 4.4 (KitKat) onward we can re-use if the byte size of the new bitmap
&& candidate.getHeight() == targetOptions.outHeight // is smaller than the reusable bitmap candidate allocation byte count.
&& targetOptions.inSampleSize <= 1; 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. * @param config The bitmap configuration.
* @return The byte usage per pixel. * @return The byte usage per pixel.
*/ */
@@ -528,6 +531,8 @@ public class ImageCache {
return 4; return 4;
} else if (config == Config.RGB_565) { } else if (config == Config.RGB_565) {
return 2; return 2;
} else if (config == Config.ARGB_4444) {
return 2;
} else if (config == Config.ALPHA_8) { } else if (config == Config.ALPHA_8) {
return 1; 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 * @param value
* @return size in bytes * @return size in bytes
*/ */

View File

@@ -207,12 +207,7 @@ public class ImageResizer extends ImageWorker {
Bitmap inBitmap = cache.getBitmapFromReusableSet(options); Bitmap inBitmap = cache.getBitmapFromReusableSet(options);
if (inBitmap != null) { if (inBitmap != null) {
if (BuildConfig.DEBUG) {
Log.d(TAG, "Found bitmap to use for inBitmap");
}
options.inBitmap = inBitmap; options.inBitmap = inBitmap;
} else {
Log.d(TAG, "Did NOT find bitmap to use for inBitmap");
} }
} }
} }