From 19b63da9550dd4734f2df0e663dc8009b29705a0 Mon Sep 17 00:00:00 2001 From: Sunny Goyal Date: Tue, 7 Oct 2014 12:01:58 -0700 Subject: Cleaning up icon resizing code > For resizing icons, scaling them down instead of cropping them > Removing duplicate methods and variables Bug: 17663119 Change-Id: I0f270860c0edaaa939495e63e05da841a603f6e9 --- src/com/android/launcher3/IconCache.java | 4 +- src/com/android/launcher3/LauncherModel.java | 4 +- src/com/android/launcher3/LauncherProvider.java | 2 +- src/com/android/launcher3/Utilities.java | 61 +++++-------------------- 4 files changed, 15 insertions(+), 56 deletions(-) (limited to 'src/com/android') diff --git a/src/com/android/launcher3/IconCache.java b/src/com/android/launcher3/IconCache.java index 365f8ed2a..539efa407 100644 --- a/src/com/android/launcher3/IconCache.java +++ b/src/com/android/launcher3/IconCache.java @@ -29,7 +29,6 @@ import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; -import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.text.TextUtils; import android.util.Log; @@ -419,8 +418,7 @@ public class IconCache { entry.title = title; } if (icon != null) { - entry.icon = Utilities.createIconBitmap( - new BitmapDrawable(mContext.getResources(), icon), mContext); + entry.icon = Utilities.createIconBitmap(icon, mContext); } } diff --git a/src/com/android/launcher3/LauncherModel.java b/src/com/android/launcher3/LauncherModel.java index 224c535a4..26bbbe045 100644 --- a/src/com/android/launcher3/LauncherModel.java +++ b/src/com/android/launcher3/LauncherModel.java @@ -3899,8 +3899,8 @@ public class LauncherModel extends BroadcastReceiver boolean customIcon = false; ShortcutIconResource iconResource = null; - if (bitmap != null && bitmap instanceof Bitmap) { - icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context); + if (bitmap instanceof Bitmap) { + icon = Utilities.createIconBitmap((Bitmap) bitmap, context); customIcon = true; } else { Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE); diff --git a/src/com/android/launcher3/LauncherProvider.java b/src/com/android/launcher3/LauncherProvider.java index 6b42c1ad1..b0098662a 100644 --- a/src/com/android/launcher3/LauncherProvider.java +++ b/src/com/android/launcher3/LauncherProvider.java @@ -1130,7 +1130,7 @@ public class LauncherProvider extends ContentProvider { long id = c.getLong(idIndex); byte[] data = c.getBlob(iconIndex); try { - Bitmap bitmap = Utilities.resampleIconBitmap( + Bitmap bitmap = Utilities.createIconBitmap( BitmapFactory.decodeByteArray(data, 0, data.length), mContext); if (bitmap != null) { diff --git a/src/com/android/launcher3/Utilities.java b/src/com/android/launcher3/Utilities.java index 1e8bc2f73..a75ddf6a1 100644 --- a/src/com/android/launcher3/Utilities.java +++ b/src/com/android/launcher3/Utilities.java @@ -60,8 +60,6 @@ public final class Utilities { private static int sIconWidth = -1; private static int sIconHeight = -1; - public static int sIconTextureWidth = -1; - public static int sIconTextureHeight = -1; private static final Rect sOldBounds = new Rect(); private static final Canvas sCanvas = new Canvas(); @@ -137,7 +135,7 @@ public final class Utilities { * Resizes an icon drawable to the correct icon size. */ static void resizeIconDrawable(Drawable icon) { - icon.setBounds(0, 0, sIconTextureWidth, sIconTextureHeight); + icon.setBounds(0, 0, sIconWidth, sIconHeight); } private static boolean isPropertyEnabled(String propertyName) { @@ -158,29 +156,18 @@ public final class Utilities { } /** - * Returns a bitmap suitable for the all apps view. Used to convert pre-ICS - * icon bitmaps that are stored in the database (which were 74x74 pixels at hdpi size) - * to the proper size (48dp) + * Returns a bitmap which is of the appropriate size to be displayed as an icon */ static Bitmap createIconBitmap(Bitmap icon, Context context) { - int textureWidth = sIconTextureWidth; - int textureHeight = sIconTextureHeight; - int sourceWidth = icon.getWidth(); - int sourceHeight = icon.getHeight(); - if (sourceWidth > textureWidth && sourceHeight > textureHeight) { - // Icon is bigger than it should be; clip it (solves the GB->ICS migration case) - return Bitmap.createBitmap(icon, - (sourceWidth - textureWidth) / 2, - (sourceHeight - textureHeight) / 2, - textureWidth, textureHeight); - } else if (sourceWidth == textureWidth && sourceHeight == textureHeight) { - // Icon is the right size, no need to change it + synchronized (sCanvas) { // we share the statics :-( + if (sIconWidth == -1) { + initStatics(context); + } + } + if (sIconWidth == icon.getWidth() && sIconHeight == icon.getHeight()) { return icon; - } else { - // Icon is too small, render to a larger bitmap - final Resources resources = context.getResources(); - return createIconBitmap(new BitmapDrawable(resources, icon), context); } + return createIconBitmap(new BitmapDrawable(context.getResources(), icon), context); } /** @@ -220,8 +207,8 @@ public final class Utilities { } // no intrinsic size --> use default size - int textureWidth = sIconTextureWidth; - int textureHeight = sIconTextureHeight; + int textureWidth = sIconWidth; + int textureHeight = sIconHeight; final Bitmap bitmap = Bitmap.createBitmap(textureWidth, textureHeight, Bitmap.Config.ARGB_8888); @@ -252,30 +239,6 @@ public final class Utilities { } } - /** - * Returns a Bitmap representing the thumbnail of the specified Bitmap. - * - * @param bitmap The bitmap to get a thumbnail of. - * @param context The application's context. - * - * @return A thumbnail for the specified bitmap or the bitmap itself if the - * thumbnail could not be created. - */ - static Bitmap resampleIconBitmap(Bitmap bitmap, Context context) { - synchronized (sCanvas) { // we share the statics :-( - if (sIconWidth == -1) { - initStatics(context); - } - - if (bitmap.getWidth() == sIconWidth && bitmap.getHeight() == sIconHeight) { - return bitmap; - } else { - final Resources resources = context.getResources(); - return createIconBitmap(new BitmapDrawable(resources, bitmap), context); - } - } - } - /** * Given a coordinate relative to the descendant, find the coordinate in a parent view's * coordinates. @@ -378,12 +341,10 @@ public final class Utilities { private static void initStatics(Context context) { final Resources resources = context.getResources(); sIconWidth = sIconHeight = (int) resources.getDimension(R.dimen.app_icon_size); - sIconTextureWidth = sIconTextureHeight = sIconWidth; } public static void setIconSize(int widthPx) { sIconWidth = sIconHeight = widthPx; - sIconTextureWidth = sIconTextureHeight = widthPx; } public static void scaleRect(Rect r, float scale) { -- cgit v1.2.3