From aa03cd0a539c56dcc0c6d2f9a000d8374b394b41 Mon Sep 17 00:00:00 2001 From: Owen Lin Date: Wed, 7 Mar 2012 17:39:56 +0800 Subject: Reuse bitmap for all micro thumb images to prevent GC. Change-Id: I27d3002e5bb745a597f52962fe24744c8329441c --- .../com/android/gallery3d/common/BitmapUtils.java | 49 ++++++++-------------- .../src/com/android/gallery3d/common/Utils.java | 10 ++--- 2 files changed, 22 insertions(+), 37 deletions(-) (limited to 'gallerycommon/src/com/android/gallery3d/common') diff --git a/gallerycommon/src/com/android/gallery3d/common/BitmapUtils.java b/gallerycommon/src/com/android/gallery3d/common/BitmapUtils.java index 75ae25fc1..a671ed2b9 100644 --- a/gallerycommon/src/com/android/gallery3d/common/BitmapUtils.java +++ b/gallerycommon/src/com/android/gallery3d/common/BitmapUtils.java @@ -32,7 +32,7 @@ import java.lang.reflect.Method; public class BitmapUtils { private static final String TAG = "BitmapUtils"; - private static final int COMPRESS_JPEG_QUALITY = 90; + private static final int DEFAULT_JPEG_QUALITY = 90; public static final int UNCONSTRAINED = -1; private BitmapUtils(){} @@ -94,7 +94,7 @@ public class BitmapUtils { : initialSize / 8 * 8; } - // Fin the min x that 1 / x <= scale + // Find the min x that 1 / x >= scale public static int computeSampleSizeLarger(float scale) { int initialSize = (int) FloatMath.floor(1f / scale); if (initialSize <= 1) return 1; @@ -104,7 +104,7 @@ public class BitmapUtils { : initialSize / 8 * 8; } - // Find the max x that 1 / x >= scale. + // Find the max x that 1 / x <= scale. public static int computeSampleSize(float scale) { Utils.assertTrue(scale > 0); int initialSize = Math.max(1, (int) FloatMath.ceil(1 / scale)); @@ -146,27 +146,15 @@ public class BitmapUtils { return resizeBitmapByScale(bitmap, scale, recycle); } - // Resize the bitmap if each side is >= targetSize * 2 - public static Bitmap resizeDownIfTooBig( - Bitmap bitmap, int targetSize, boolean recycle) { - int srcWidth = bitmap.getWidth(); - int srcHeight = bitmap.getHeight(); - float scale = Math.max( - (float) targetSize / srcWidth, (float) targetSize / srcHeight); - if (scale > 0.5f) return bitmap; - return resizeBitmapByScale(bitmap, scale, recycle); - } - - public static Bitmap resizeDownAndCropCenter(Bitmap bitmap, int size, - boolean recycle) { + public static Bitmap resizeAndCropCenter(Bitmap bitmap, int size, boolean recycle) { int w = bitmap.getWidth(); int h = bitmap.getHeight(); - int minSide = Math.min(w, h); - if (w == h && minSide <= size) return bitmap; - size = Math.min(size, minSide); + if (w == size && h == size) return bitmap; + + // scale the image so that the shorter side equals to the target; + // the longer side will be center-cropped. + float scale = (float) size / Math.min(w, h); - float scale = Math.max((float) size / bitmap.getWidth(), - (float) size / bitmap.getHeight()); Bitmap target = Bitmap.createBitmap(size, size, getConfig(bitmap)); int width = Math.round(scale * bitmap.getWidth()); int height = Math.round(scale * bitmap.getHeight()); @@ -247,11 +235,14 @@ public class BitmapUtils { return null; } - public static byte[] compressBitmap(Bitmap bitmap) { - ByteArrayOutputStream os = new ByteArrayOutputStream(); - bitmap.compress(Bitmap.CompressFormat.JPEG, - COMPRESS_JPEG_QUALITY, os); - return os.toByteArray(); + public static byte[] compressToBytes(Bitmap bitmap) { + return compressToBytes(bitmap, DEFAULT_JPEG_QUALITY); + } + + public static byte[] compressToBytes(Bitmap bitmap, int quality) { + ByteArrayOutputStream baos = new ByteArrayOutputStream(65536); + bitmap.compress(CompressFormat.JPEG, quality, baos); + return baos.toByteArray(); } public static boolean isSupportedByRegionDecoder(String mimeType) { @@ -266,10 +257,4 @@ public class BitmapUtils { mimeType = mimeType.toLowerCase(); return mimeType.equals("image/jpeg"); } - - public static byte[] compressToBytes(Bitmap bitmap, int quality) { - ByteArrayOutputStream baos = new ByteArrayOutputStream(65536); - bitmap.compress(CompressFormat.JPEG, quality, baos); - return baos.toByteArray(); - } } diff --git a/gallerycommon/src/com/android/gallery3d/common/Utils.java b/gallerycommon/src/com/android/gallery3d/common/Utils.java index bbe2a5d55..391b22535 100644 --- a/gallerycommon/src/com/android/gallery3d/common/Utils.java +++ b/gallerycommon/src/com/android/gallery3d/common/Utils.java @@ -16,22 +16,17 @@ package com.android.gallery3d.common; -import android.app.PendingIntent; import android.content.Context; import android.content.pm.PackageInfo; import android.content.pm.PackageManager.NameNotFoundException; import android.database.Cursor; import android.os.Build; -import android.os.Environment; -import android.os.Parcel; import android.os.ParcelFileDescriptor; -import android.os.StatFs; import android.text.TextUtils; import android.util.Log; import java.io.Closeable; import java.io.InterruptedIOException; -import java.util.Random; public class Utils { private static final String TAG = "Utils"; @@ -336,4 +331,9 @@ public class Utils { int length = Math.min(s.length(), MASK_STRING.length()); return IS_DEBUG_BUILD ? s : MASK_STRING.substring(0, length); } + + // This method should be ONLY used for debugging. + public static void debug(String message, Object ... args) { + Log.v(DEBUG_TAG, String.format(message, args)); + } } -- cgit v1.2.3