diff options
author | Joe Onorato <joeo@android.com> | 2009-08-17 11:03:03 -0400 |
---|---|---|
committer | Joe Onorato <joeo@android.com> | 2009-08-24 14:58:44 -0400 |
commit | 9c1289cb3bfb74f86e53ec7ac6dd76bb39666b2d (patch) | |
tree | bb6f3ea57e53ccf695bd7cf2e3db33786506be5c /src/com/android/launcher2/Utilities.java | |
parent | 02e638e6bf6dac903396a67029d4d48e3cfa87d9 (diff) | |
download | android_packages_apps_Trebuchet-9c1289cb3bfb74f86e53ec7ac6dd76bb39666b2d.tar.gz android_packages_apps_Trebuchet-9c1289cb3bfb74f86e53ec7ac6dd76bb39666b2d.tar.bz2 android_packages_apps_Trebuchet-9c1289cb3bfb74f86e53ec7ac6dd76bb39666b2d.zip |
Redo the launcher loading code and put the real app icons into rollo.
Diffstat (limited to 'src/com/android/launcher2/Utilities.java')
-rw-r--r-- | src/com/android/launcher2/Utilities.java | 198 |
1 files changed, 108 insertions, 90 deletions
diff --git a/src/com/android/launcher2/Utilities.java b/src/com/android/launcher2/Utilities.java index 9e715d1b7..53e42e3cf 100644 --- a/src/com/android/launcher2/Utilities.java +++ b/src/com/android/launcher2/Utilities.java @@ -78,82 +78,101 @@ final class Utilities { * The size of the thumbnail is defined by the dimension * android.R.dimen.launcher_application_icon_size. * - * This method is not thread-safe and should be invoked on the UI thread only. - * * @param icon The icon to get a thumbnail of. * @param context The application's context. + * @param forceBitmap If this is true, the drawable will always be redrawn + * into a new bitmap if it isn't already a BitmapDrawable or a + * FastBitmapDrawable. * * @return A thumbnail for the specified icon or the icon itself if the * thumbnail could not be created. */ - static Drawable createIconThumbnail(Drawable icon, Context context) { - if (sIconWidth == -1) { - final Resources resources = context.getResources(); - sIconWidth = sIconHeight = (int) resources.getDimension(android.R.dimen.app_icon_size); - } - - int width = sIconWidth; - int height = sIconHeight; - - float scale = 1.0f; - if (icon instanceof PaintDrawable) { - PaintDrawable painter = (PaintDrawable) icon; - painter.setIntrinsicWidth(width); - painter.setIntrinsicHeight(height); - } else if (icon instanceof BitmapDrawable) { - // Ensure the bitmap has a density. - BitmapDrawable bitmapDrawable = (BitmapDrawable) icon; - Bitmap bitmap = bitmapDrawable.getBitmap(); - if (bitmap.getDensity() == Bitmap.DENSITY_NONE) { - bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics()); + static Drawable createIconThumbnail(Drawable icon, Context context, boolean forceBitmap) { + synchronized (sCanvas) { // we share the statics :-( + if (sIconWidth == -1) { + final Resources resources = context.getResources(); + sIconWidth = (int)resources.getDimension(android.R.dimen.app_icon_size); + sIconHeight = sIconWidth; } - } - int iconWidth = icon.getIntrinsicWidth(); - int iconHeight = icon.getIntrinsicHeight(); - if (width > 0 && height > 0) { - if (width < iconWidth || height < iconHeight || scale != 1.0f) { - final float ratio = (float) iconWidth / iconHeight; - - if (iconWidth > iconHeight) { - height = (int) (width / ratio); - } else if (iconHeight > iconWidth) { - width = (int) (height * ratio); + int width = sIconWidth; + int height = sIconHeight; + + float scale = 1.0f; + if (icon instanceof PaintDrawable) { + PaintDrawable painter = (PaintDrawable) icon; + painter.setIntrinsicWidth(width); + painter.setIntrinsicHeight(height); + } else if (icon instanceof BitmapDrawable) { + // Ensure the bitmap has a density. + BitmapDrawable bitmapDrawable = (BitmapDrawable) icon; + Bitmap bitmap = bitmapDrawable.getBitmap(); + if (bitmap.getDensity() == Bitmap.DENSITY_NONE) { + bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics()); } - - final Bitmap.Config c = icon.getOpacity() != PixelFormat.OPAQUE ? - Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565; - final Bitmap thumb = Bitmap.createBitmap(sIconWidth, sIconHeight, c); - final Canvas canvas = sCanvas; - canvas.setBitmap(thumb); - // Copy the old bounds to restore them later - // If we were to do oldBounds = icon.getBounds(), - // the call to setBounds() that follows would - // change the same instance and we would lose the - // old bounds - sOldBounds.set(icon.getBounds()); - final int x = (sIconWidth - width) / 2; - final int y = (sIconHeight - height) / 2; - icon.setBounds(x, y, x + width, y + height); - icon.draw(canvas); - icon.setBounds(sOldBounds); - icon = new FastBitmapDrawable(thumb); - } else if (iconWidth < width && iconHeight < height) { + } + int iconWidth = icon.getIntrinsicWidth(); + int iconHeight = icon.getIntrinsicHeight(); + + if (iconWidth > 0 && iconWidth > 0) { + if (width < iconWidth || height < iconHeight || scale != 1.0f) { + final float ratio = (float) iconWidth / iconHeight; + + if (iconWidth > iconHeight) { + height = (int) (width / ratio); + } else if (iconHeight > iconWidth) { + width = (int) (height * ratio); + } + + final Bitmap.Config c = icon.getOpacity() != PixelFormat.OPAQUE ? + Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565; + final Bitmap thumb = Bitmap.createBitmap(sIconWidth, sIconHeight, c); + final Canvas canvas = sCanvas; + canvas.setBitmap(thumb); + // Copy the old bounds to restore them later + // If we were to do oldBounds = icon.getBounds(), + // the call to setBounds() that follows would + // change the same instance and we would lose the + // old bounds + sOldBounds.set(icon.getBounds()); + final int x = (sIconWidth - width) / 2; + final int y = (sIconHeight - height) / 2; + icon.setBounds(x, y, x + width, y + height); + icon.draw(canvas); + icon.setBounds(sOldBounds); + icon = new FastBitmapDrawable(thumb); + } else if (iconWidth < width && iconHeight < height) { + final Bitmap.Config c = Bitmap.Config.ARGB_8888; + final Bitmap thumb = Bitmap.createBitmap(sIconWidth, sIconHeight, c); + final Canvas canvas = sCanvas; + canvas.setBitmap(thumb); + sOldBounds.set(icon.getBounds()); + final int x = (width - iconWidth) / 2; + final int y = (height - iconHeight) / 2; + icon.setBounds(x, y, x + iconWidth, y + iconHeight); + icon.draw(canvas); + icon.setBounds(sOldBounds); + icon = new FastBitmapDrawable(thumb); + } + } + + if (forceBitmap) { + // no intrinsic size --> use default size + int w = sIconWidth; + int h = sIconHeight; final Bitmap.Config c = Bitmap.Config.ARGB_8888; - final Bitmap thumb = Bitmap.createBitmap(sIconWidth, sIconHeight, c); + final Bitmap thumb = Bitmap.createBitmap(roundToPow2(w), roundToPow2(h), c); final Canvas canvas = sCanvas; canvas.setBitmap(thumb); sOldBounds.set(icon.getBounds()); - final int x = (width - iconWidth) / 2; - final int y = (height - iconHeight) / 2; - icon.setBounds(x, y, x + iconWidth, y + iconHeight); + icon.setBounds(0, 0, w, h); icon.draw(canvas); icon.setBounds(sOldBounds); icon = new FastBitmapDrawable(thumb); } - } - return icon; + return icon; + } } /** @@ -161,8 +180,6 @@ final class Utilities { * The size of the thumbnail is defined by the dimension * android.R.dimen.launcher_application_icon_size. * - * This method is not thread-safe and should be invoked on the UI thread only. - * * @param bitmap The bitmap to get a thumbnail of. * @param context The application's context. * @@ -170,42 +187,44 @@ final class Utilities { * thumbnail could not be created. */ static Bitmap createBitmapThumbnail(Bitmap bitmap, Context context) { - if (sIconWidth == -1) { - final Resources resources = context.getResources(); - sIconWidth = sIconHeight = (int) resources.getDimension( - android.R.dimen.app_icon_size); - } + synchronized (sCanvas) { // we share the statics :-( + if (sIconWidth == -1) { + final Resources resources = context.getResources(); + sIconWidth = sIconHeight = (int) resources.getDimension( + android.R.dimen.app_icon_size); + } - int width = sIconWidth; - int height = sIconHeight; + int width = sIconWidth; + int height = sIconHeight; - final int bitmapWidth = bitmap.getWidth(); - final int bitmapHeight = bitmap.getHeight(); + final int bitmapWidth = bitmap.getWidth(); + final int bitmapHeight = bitmap.getHeight(); - if (width > 0 && height > 0 && (width < bitmapWidth || height < bitmapHeight)) { - final float ratio = (float) bitmapWidth / bitmapHeight; + if (width > 0 && height > 0 && (width < bitmapWidth || height < bitmapHeight)) { + final float ratio = (float) bitmapWidth / bitmapHeight; - if (bitmapWidth > bitmapHeight) { - height = (int) (width / ratio); - } else if (bitmapHeight > bitmapWidth) { - width = (int) (height * ratio); + if (bitmapWidth > bitmapHeight) { + height = (int) (width / ratio); + } else if (bitmapHeight > bitmapWidth) { + width = (int) (height * ratio); + } + + final Bitmap.Config c = (width == sIconWidth && height == sIconHeight) ? + bitmap.getConfig() : Bitmap.Config.ARGB_8888; + final Bitmap thumb = Bitmap.createBitmap(sIconWidth, sIconHeight, c); + final Canvas canvas = sCanvas; + final Paint paint = sPaint; + canvas.setBitmap(thumb); + paint.setDither(false); + paint.setFilterBitmap(true); + sBounds.set((sIconWidth - width) / 2, (sIconHeight - height) / 2, width, height); + sOldBounds.set(0, 0, bitmapWidth, bitmapHeight); + canvas.drawBitmap(bitmap, sOldBounds, sBounds, paint); + return thumb; } - final Bitmap.Config c = (width == sIconWidth && height == sIconHeight) ? - bitmap.getConfig() : Bitmap.Config.ARGB_8888; - final Bitmap thumb = Bitmap.createBitmap(sIconWidth, sIconHeight, c); - final Canvas canvas = sCanvas; - final Paint paint = sPaint; - canvas.setBitmap(thumb); - paint.setDither(false); - paint.setFilterBitmap(true); - sBounds.set((sIconWidth - width) / 2, (sIconHeight - height) / 2, width, height); - sOldBounds.set(0, 0, bitmapWidth, bitmapHeight); - canvas.drawBitmap(bitmap, sOldBounds, sBounds, paint); - return thumb; + return bitmap; } - - return bitmap; } static class BubbleText { @@ -261,7 +280,6 @@ final class Utilities { mFirstLineY = (int)(leading + ascent + 0.5f); mLineHeight = (int)(leading + ascent + descent + 0.5f); - roundToPow2(64); mBitmapWidth = roundToPow2((int)(mBubbleRect.width() + 0.5f)); mBitmapHeight = roundToPow2((int)((MAX_LINES * mLineHeight) + leading + 0.5f)); |