The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Joe Onorato | a590252 | 2009-07-30 13:37:37 -0700 | [diff] [blame] | 17 | package com.android.launcher2; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 18 | |
Mitsuru Oshima | 583ed3b | 2009-05-12 19:19:10 -0700 | [diff] [blame] | 19 | import android.graphics.drawable.BitmapDrawable; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 20 | import android.graphics.drawable.Drawable; |
| 21 | import android.graphics.drawable.PaintDrawable; |
| 22 | import android.graphics.Bitmap; |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 23 | import android.graphics.BlurMaskFilter; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 24 | import android.graphics.Canvas; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 25 | import android.graphics.Paint; |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 26 | import android.graphics.PaintFlagsDrawFilter; |
| 27 | import android.graphics.PixelFormat; |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 28 | import android.graphics.PorterDuff; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 29 | import android.graphics.Rect; |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 30 | import android.graphics.RectF; |
Mike Reed | cdd1179 | 2009-10-29 17:27:55 -0400 | [diff] [blame] | 31 | import android.graphics.TableMaskFilter; |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 32 | import android.graphics.Typeface; |
| 33 | import android.text.Layout.Alignment; |
| 34 | import android.text.StaticLayout; |
| 35 | import android.text.TextPaint; |
Dianne Hackborn | 32ce7f1 | 2009-07-22 21:56:50 -0700 | [diff] [blame] | 36 | import android.util.DisplayMetrics; |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 37 | import android.util.Log; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 38 | import android.content.res.Resources; |
| 39 | import android.content.Context; |
| 40 | |
| 41 | /** |
| 42 | * Various utilities shared amongst the Launcher's classes. |
| 43 | */ |
| 44 | final class Utilities { |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 45 | private static final String TAG = "Launcher.Utilities"; |
| 46 | |
Joe Onorato | 9392a75 | 2009-09-15 17:13:09 -0400 | [diff] [blame] | 47 | private static final boolean TEXT_BURN = false; |
| 48 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 49 | private static int sIconWidth = -1; |
| 50 | private static int sIconHeight = -1; |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 51 | private static int sIconTextureWidth = -1; |
| 52 | private static int sIconTextureHeight = -1; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 53 | |
| 54 | private static final Paint sPaint = new Paint(); |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 55 | private static final Paint sBlurPaint = new Paint(); |
Joe Onorato | eb8325a | 2009-11-08 13:20:30 -0500 | [diff] [blame] | 56 | private static final Paint sGlowColorPressedPaint = new Paint(); |
Joe Onorato | c61cff9 | 2009-11-08 11:54:39 -0500 | [diff] [blame] | 57 | private static final Paint sGlowColorFocusedPaint = new Paint(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 58 | private static final Rect sBounds = new Rect(); |
| 59 | private static final Rect sOldBounds = new Rect(); |
Romain Guy | 89911d2 | 2009-09-28 18:48:49 -0700 | [diff] [blame] | 60 | private static final Canvas sCanvas = new Canvas(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 61 | |
| 62 | static { |
| 63 | sCanvas.setDrawFilter(new PaintFlagsDrawFilter(Paint.DITHER_FLAG, |
| 64 | Paint.FILTER_BITMAP_FLAG)); |
| 65 | } |
| 66 | |
| 67 | static Bitmap centerToFit(Bitmap bitmap, int width, int height, Context context) { |
| 68 | final int bitmapWidth = bitmap.getWidth(); |
| 69 | final int bitmapHeight = bitmap.getHeight(); |
| 70 | |
| 71 | if (bitmapWidth < width || bitmapHeight < height) { |
| 72 | int color = context.getResources().getColor(R.color.window_background); |
| 73 | |
| 74 | Bitmap centered = Bitmap.createBitmap(bitmapWidth < width ? width : bitmapWidth, |
| 75 | bitmapHeight < height ? height : bitmapHeight, Bitmap.Config.RGB_565); |
Dianne Hackborn | 32ce7f1 | 2009-07-22 21:56:50 -0700 | [diff] [blame] | 76 | centered.setDensity(bitmap.getDensity()); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 77 | Canvas canvas = new Canvas(centered); |
| 78 | canvas.drawColor(color); |
| 79 | canvas.drawBitmap(bitmap, (width - bitmapWidth) / 2.0f, (height - bitmapHeight) / 2.0f, |
| 80 | null); |
| 81 | |
| 82 | bitmap = centered; |
| 83 | } |
| 84 | |
| 85 | return bitmap; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Returns a Drawable representing the thumbnail of the specified Drawable. |
| 90 | * The size of the thumbnail is defined by the dimension |
| 91 | * android.R.dimen.launcher_application_icon_size. |
| 92 | * |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 93 | * @param icon The icon to get a thumbnail of. |
| 94 | * @param context The application's context. |
| 95 | * |
| 96 | * @return A thumbnail for the specified icon or the icon itself if the |
Jason Sams | fd22dac | 2009-09-20 17:24:16 -0700 | [diff] [blame] | 97 | * thumbnail could not be created. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 98 | */ |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 99 | static Drawable createIconThumbnail(Drawable icon, Context context) { |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 100 | synchronized (sCanvas) { // we share the statics :-( |
| 101 | if (sIconWidth == -1) { |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 102 | initStatics(context); |
Mitsuru Oshima | 583ed3b | 2009-05-12 19:19:10 -0700 | [diff] [blame] | 103 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 104 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 105 | int width = sIconWidth; |
| 106 | int height = sIconHeight; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 107 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 108 | if (icon instanceof PaintDrawable) { |
| 109 | PaintDrawable painter = (PaintDrawable) icon; |
| 110 | painter.setIntrinsicWidth(width); |
| 111 | painter.setIntrinsicHeight(height); |
| 112 | } else if (icon instanceof BitmapDrawable) { |
| 113 | // Ensure the bitmap has a density. |
| 114 | BitmapDrawable bitmapDrawable = (BitmapDrawable) icon; |
| 115 | Bitmap bitmap = bitmapDrawable.getBitmap(); |
| 116 | if (bitmap.getDensity() == Bitmap.DENSITY_NONE) { |
| 117 | bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics()); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 118 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 119 | } |
| 120 | int iconWidth = icon.getIntrinsicWidth(); |
| 121 | int iconHeight = icon.getIntrinsicHeight(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 122 | |
Romain Guy | 89911d2 | 2009-09-28 18:48:49 -0700 | [diff] [blame] | 123 | if (iconWidth > 0 && iconHeight > 0) { |
| 124 | if (width < iconWidth || height < iconHeight) { |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 125 | final float ratio = (float) iconWidth / iconHeight; |
| 126 | |
| 127 | if (iconWidth > iconHeight) { |
| 128 | height = (int) (width / ratio); |
| 129 | } else if (iconHeight > iconWidth) { |
| 130 | width = (int) (height * ratio); |
| 131 | } |
| 132 | |
| 133 | final Bitmap.Config c = icon.getOpacity() != PixelFormat.OPAQUE ? |
| 134 | Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565; |
| 135 | final Bitmap thumb = Bitmap.createBitmap(sIconWidth, sIconHeight, c); |
| 136 | final Canvas canvas = sCanvas; |
| 137 | canvas.setBitmap(thumb); |
| 138 | // Copy the old bounds to restore them later |
| 139 | // If we were to do oldBounds = icon.getBounds(), |
| 140 | // the call to setBounds() that follows would |
| 141 | // change the same instance and we would lose the |
| 142 | // old bounds |
| 143 | sOldBounds.set(icon.getBounds()); |
| 144 | final int x = (sIconWidth - width) / 2; |
| 145 | final int y = (sIconHeight - height) / 2; |
| 146 | icon.setBounds(x, y, x + width, y + height); |
| 147 | icon.draw(canvas); |
| 148 | icon.setBounds(sOldBounds); |
| 149 | icon = new FastBitmapDrawable(thumb); |
| 150 | } else if (iconWidth < width && iconHeight < height) { |
| 151 | final Bitmap.Config c = Bitmap.Config.ARGB_8888; |
| 152 | final Bitmap thumb = Bitmap.createBitmap(sIconWidth, sIconHeight, c); |
| 153 | final Canvas canvas = sCanvas; |
| 154 | canvas.setBitmap(thumb); |
| 155 | sOldBounds.set(icon.getBounds()); |
| 156 | final int x = (width - iconWidth) / 2; |
| 157 | final int y = (height - iconHeight) / 2; |
| 158 | icon.setBounds(x, y, x + iconWidth, y + iconHeight); |
| 159 | icon.draw(canvas); |
| 160 | icon.setBounds(sOldBounds); |
| 161 | icon = new FastBitmapDrawable(thumb); |
| 162 | } |
| 163 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 164 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 165 | return icon; |
| 166 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 167 | } |
| 168 | |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 169 | static int sColors[] = { 0xffff0000, 0xff00ff00, 0xff0000ff }; |
| 170 | static int sColorIndex = 0; |
| 171 | |
| 172 | /** |
| 173 | * Returns a bitmap suitable for the all apps view. The bitmap will be a power |
| 174 | * of two sized ARGB_8888 bitmap that can be used as a gl texture. |
| 175 | */ |
| 176 | static Bitmap createAllAppsBitmap(Drawable icon, Context context) { |
| 177 | synchronized (sCanvas) { // we share the statics :-( |
| 178 | if (sIconWidth == -1) { |
| 179 | initStatics(context); |
| 180 | } |
| 181 | |
| 182 | int width = sIconWidth; |
| 183 | int height = sIconHeight; |
| 184 | |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 185 | if (icon instanceof PaintDrawable) { |
| 186 | PaintDrawable painter = (PaintDrawable) icon; |
| 187 | painter.setIntrinsicWidth(width); |
| 188 | painter.setIntrinsicHeight(height); |
| 189 | } else if (icon instanceof BitmapDrawable) { |
| 190 | // Ensure the bitmap has a density. |
| 191 | BitmapDrawable bitmapDrawable = (BitmapDrawable) icon; |
| 192 | Bitmap bitmap = bitmapDrawable.getBitmap(); |
| 193 | if (bitmap.getDensity() == Bitmap.DENSITY_NONE) { |
| 194 | bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics()); |
| 195 | } |
| 196 | } |
| 197 | int sourceWidth = icon.getIntrinsicWidth(); |
| 198 | int sourceHeight = icon.getIntrinsicHeight(); |
| 199 | |
| 200 | if (sourceWidth > 0 && sourceWidth > 0) { |
| 201 | // There are intrinsic sizes. |
Romain Guy | 89911d2 | 2009-09-28 18:48:49 -0700 | [diff] [blame] | 202 | if (width < sourceWidth || height < sourceHeight) { |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 203 | // It's too big, scale it down. |
| 204 | final float ratio = (float) sourceWidth / sourceHeight; |
| 205 | if (sourceWidth > sourceHeight) { |
| 206 | height = (int) (width / ratio); |
| 207 | } else if (sourceHeight > sourceWidth) { |
| 208 | width = (int) (height * ratio); |
| 209 | } |
| 210 | } else if (sourceWidth < width && sourceHeight < height) { |
| 211 | // It's small, use the size they gave us. |
| 212 | width = sourceWidth; |
Romain Guy | 89911d2 | 2009-09-28 18:48:49 -0700 | [diff] [blame] | 213 | height = sourceHeight; |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | |
| 217 | // no intrinsic size --> use default size |
| 218 | int textureWidth = sIconTextureWidth; |
| 219 | int textureHeight = sIconTextureHeight; |
| 220 | |
| 221 | final Bitmap bitmap = Bitmap.createBitmap(textureWidth, textureHeight, |
| 222 | Bitmap.Config.ARGB_8888); |
| 223 | final Canvas canvas = sCanvas; |
| 224 | canvas.setBitmap(bitmap); |
| 225 | |
| 226 | final int left = (textureWidth-width) / 2; |
| 227 | final int top = (textureHeight-height) / 2; |
| 228 | |
| 229 | if (false) { |
| 230 | // draw a big box for the icon for debugging |
| 231 | canvas.drawColor(sColors[sColorIndex]); |
| 232 | if (++sColorIndex >= sColors.length) sColorIndex = 0; |
| 233 | Paint debugPaint = new Paint(); |
| 234 | debugPaint.setColor(0xffcccc00); |
| 235 | canvas.drawRect(left, top, left+width, top+height, debugPaint); |
| 236 | } |
| 237 | |
| 238 | sOldBounds.set(icon.getBounds()); |
| 239 | icon.setBounds(left, top, left+width, top+height); |
| 240 | icon.draw(canvas); |
| 241 | icon.setBounds(sOldBounds); |
| 242 | |
| 243 | return bitmap; |
| 244 | } |
| 245 | } |
| 246 | |
Joe Onorato | c61cff9 | 2009-11-08 11:54:39 -0500 | [diff] [blame] | 247 | static void drawSelectedAllAppsBitmap(Canvas dest, int destWidth, int destHeight, |
Joe Onorato | eb8325a | 2009-11-08 13:20:30 -0500 | [diff] [blame] | 248 | boolean pressed, Bitmap src) { |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 249 | synchronized (sCanvas) { // we share the statics :-( |
| 250 | if (sIconWidth == -1) { |
| 251 | // We can't have gotten to here without src being initialized, which |
| 252 | // comes from this file already. So just assert. |
| 253 | //initStatics(context); |
| 254 | throw new RuntimeException("Assertion failed: Utilities not initialized"); |
| 255 | } |
| 256 | |
| 257 | dest.drawColor(0, PorterDuff.Mode.CLEAR); |
Jason Sams | b4ecab2 | 2010-01-19 16:43:26 -0800 | [diff] [blame] | 258 | |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 259 | int[] xy = new int[2]; |
Mike Reed | cdd1179 | 2009-10-29 17:27:55 -0400 | [diff] [blame] | 260 | Bitmap mask = src.extractAlpha(sBlurPaint, xy); |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 261 | |
Mike Reed | cdd1179 | 2009-10-29 17:27:55 -0400 | [diff] [blame] | 262 | float px = (destWidth - src.getWidth()) / 2; |
| 263 | float py = (destHeight - src.getHeight()) / 2; |
Joe Onorato | c61cff9 | 2009-11-08 11:54:39 -0500 | [diff] [blame] | 264 | dest.drawBitmap(mask, px + xy[0], py + xy[1], |
Joe Onorato | eb8325a | 2009-11-08 13:20:30 -0500 | [diff] [blame] | 265 | pressed ? sGlowColorPressedPaint : sGlowColorFocusedPaint); |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 266 | |
| 267 | mask.recycle(); |
| 268 | } |
| 269 | } |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 270 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 271 | /** |
| 272 | * Returns a Bitmap representing the thumbnail of the specified Bitmap. |
| 273 | * The size of the thumbnail is defined by the dimension |
| 274 | * android.R.dimen.launcher_application_icon_size. |
| 275 | * |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 276 | * @param bitmap The bitmap to get a thumbnail of. |
| 277 | * @param context The application's context. |
| 278 | * |
| 279 | * @return A thumbnail for the specified bitmap or the bitmap itself if the |
| 280 | * thumbnail could not be created. |
| 281 | */ |
| 282 | static Bitmap createBitmapThumbnail(Bitmap bitmap, Context context) { |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 283 | synchronized (sCanvas) { // we share the statics :-( |
| 284 | if (sIconWidth == -1) { |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 285 | initStatics(context); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 286 | } |
| 287 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 288 | int width = sIconWidth; |
| 289 | int height = sIconHeight; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 290 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 291 | final int bitmapWidth = bitmap.getWidth(); |
| 292 | final int bitmapHeight = bitmap.getHeight(); |
| 293 | |
Romain Guy | 89911d2 | 2009-09-28 18:48:49 -0700 | [diff] [blame] | 294 | if (width > 0 && height > 0) { |
| 295 | if (width < bitmapWidth || height < bitmapHeight) { |
| 296 | final float ratio = (float) bitmapWidth / bitmapHeight; |
Jason Sams | b4ecab2 | 2010-01-19 16:43:26 -0800 | [diff] [blame] | 297 | |
Romain Guy | 89911d2 | 2009-09-28 18:48:49 -0700 | [diff] [blame] | 298 | if (bitmapWidth > bitmapHeight) { |
| 299 | height = (int) (width / ratio); |
| 300 | } else if (bitmapHeight > bitmapWidth) { |
| 301 | width = (int) (height * ratio); |
| 302 | } |
Jason Sams | b4ecab2 | 2010-01-19 16:43:26 -0800 | [diff] [blame] | 303 | |
Romain Guy | 89911d2 | 2009-09-28 18:48:49 -0700 | [diff] [blame] | 304 | final Bitmap.Config c = (width == sIconWidth && height == sIconHeight) ? |
| 305 | bitmap.getConfig() : Bitmap.Config.ARGB_8888; |
| 306 | final Bitmap thumb = Bitmap.createBitmap(sIconWidth, sIconHeight, c); |
| 307 | final Canvas canvas = sCanvas; |
| 308 | final Paint paint = sPaint; |
| 309 | canvas.setBitmap(thumb); |
| 310 | paint.setDither(false); |
| 311 | paint.setFilterBitmap(true); |
| 312 | sBounds.set((sIconWidth - width) / 2, (sIconHeight - height) / 2, width, height); |
| 313 | sOldBounds.set(0, 0, bitmapWidth, bitmapHeight); |
| 314 | canvas.drawBitmap(bitmap, sOldBounds, sBounds, paint); |
| 315 | return thumb; |
| 316 | } else if (bitmapWidth < width || bitmapHeight < height) { |
| 317 | final Bitmap.Config c = Bitmap.Config.ARGB_8888; |
| 318 | final Bitmap thumb = Bitmap.createBitmap(sIconWidth, sIconHeight, c); |
| 319 | final Canvas canvas = sCanvas; |
| 320 | final Paint paint = sPaint; |
| 321 | canvas.setBitmap(thumb); |
| 322 | paint.setDither(false); |
| 323 | paint.setFilterBitmap(true); |
| 324 | canvas.drawBitmap(bitmap, (sIconWidth - bitmapWidth) / 2, |
| 325 | (sIconHeight - bitmapHeight) / 2, paint); |
| 326 | return thumb; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 327 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | return bitmap; |
| 331 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 332 | } |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 333 | |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 334 | private static void initStatics(Context context) { |
| 335 | final Resources resources = context.getResources(); |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 336 | final DisplayMetrics metrics = resources.getDisplayMetrics(); |
| 337 | final float density = metrics.density; |
| 338 | |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 339 | sIconWidth = sIconHeight = (int) resources.getDimension(android.R.dimen.app_icon_size); |
Jason Sams | b4ecab2 | 2010-01-19 16:43:26 -0800 | [diff] [blame] | 340 | sIconTextureWidth = sIconTextureHeight = sIconWidth + 2; |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 341 | |
Joe Onorato | a4c0cb9 | 2009-11-02 10:42:02 -0500 | [diff] [blame] | 342 | sBlurPaint.setMaskFilter(new BlurMaskFilter(5 * density, BlurMaskFilter.Blur.NORMAL)); |
Joe Onorato | eb8325a | 2009-11-08 13:20:30 -0500 | [diff] [blame] | 343 | sGlowColorPressedPaint.setColor(0xffffc300); |
| 344 | sGlowColorPressedPaint.setMaskFilter(TableMaskFilter.CreateClipTable(0, 30)); |
Joe Onorato | c61cff9 | 2009-11-08 11:54:39 -0500 | [diff] [blame] | 345 | sGlowColorFocusedPaint.setColor(0xffff8e00); |
| 346 | sGlowColorFocusedPaint.setMaskFilter(TableMaskFilter.CreateClipTable(0, 30)); |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 347 | } |
| 348 | |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 349 | static class BubbleText { |
| 350 | private static final int MAX_LINES = 2; |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 351 | |
Romain Guy | 88f38d1 | 2010-01-26 14:50:34 -0800 | [diff] [blame] | 352 | private final TextPaint mTextPaint; |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 353 | |
Romain Guy | 88f38d1 | 2010-01-26 14:50:34 -0800 | [diff] [blame] | 354 | private final float mBubblePadding; |
| 355 | private final RectF mBubbleRect = new RectF(); |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 356 | |
Romain Guy | 88f38d1 | 2010-01-26 14:50:34 -0800 | [diff] [blame] | 357 | private final float mTextWidth; |
| 358 | private final int mLeading; |
| 359 | private final int mFirstLineY; |
| 360 | private final int mLineHeight; |
| 361 | |
| 362 | private final int mBitmapWidth; |
| 363 | private final int mBitmapHeight; |
| 364 | private final int mDensity; |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 365 | |
| 366 | BubbleText(Context context) { |
| 367 | final Resources resources = context.getResources(); |
| 368 | |
Romain Guy | 88f38d1 | 2010-01-26 14:50:34 -0800 | [diff] [blame] | 369 | final DisplayMetrics metrics = resources.getDisplayMetrics(); |
| 370 | final float scale = metrics.density; |
| 371 | mDensity = metrics.densityDpi; |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 372 | |
| 373 | final float paddingLeft = 5.0f * scale; |
| 374 | final float paddingRight = 5.0f * scale; |
Romain Guy | 442eda2 | 2010-02-04 15:59:37 -0800 | [diff] [blame] | 375 | final float cellWidth = resources.getDimension(R.dimen.title_texture_width); |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 376 | final float bubbleWidth = cellWidth - paddingLeft - paddingRight; |
Joe Onorato | 43e7bcf | 2009-08-08 18:53:53 -0700 | [diff] [blame] | 377 | mBubblePadding = 3.0f * scale; |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 378 | |
| 379 | RectF bubbleRect = mBubbleRect; |
| 380 | bubbleRect.left = 0; |
| 381 | bubbleRect.top = 0; |
| 382 | bubbleRect.right = (int)(bubbleWidth+0.5f); |
| 383 | |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 384 | mTextWidth = bubbleWidth - mBubblePadding - mBubblePadding; |
| 385 | |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 386 | TextPaint textPaint = mTextPaint = new TextPaint(); |
Joe Onorato | efabe00 | 2009-08-28 09:38:18 -0700 | [diff] [blame] | 387 | textPaint.setTypeface(Typeface.DEFAULT); |
| 388 | textPaint.setTextSize(13*scale); |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 389 | textPaint.setColor(0xffffffff); |
Joe Onorato | 43e7bcf | 2009-08-08 18:53:53 -0700 | [diff] [blame] | 390 | textPaint.setAntiAlias(true); |
Joe Onorato | 9392a75 | 2009-09-15 17:13:09 -0400 | [diff] [blame] | 391 | if (TEXT_BURN) { |
| 392 | textPaint.setShadowLayer(8, 0, 0, 0xff000000); |
| 393 | } |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 394 | |
| 395 | float ascent = -textPaint.ascent(); |
| 396 | float descent = textPaint.descent(); |
Joe Onorato | 43e7bcf | 2009-08-08 18:53:53 -0700 | [diff] [blame] | 397 | float leading = 0.0f;//(ascent+descent) * 0.1f; |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 398 | mLeading = (int)(leading + 0.5f); |
| 399 | mFirstLineY = (int)(leading + ascent + 0.5f); |
| 400 | mLineHeight = (int)(leading + ascent + descent + 0.5f); |
| 401 | |
Romain Guy | 442eda2 | 2010-02-04 15:59:37 -0800 | [diff] [blame] | 402 | mBitmapWidth = (int)(mBubbleRect.width() + 0.5f); |
Joe Onorato | 43e7bcf | 2009-08-08 18:53:53 -0700 | [diff] [blame] | 403 | mBitmapHeight = roundToPow2((int)((MAX_LINES * mLineHeight) + leading + 0.5f)); |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 404 | |
Joe Onorato | fccc8df | 2009-10-01 14:30:16 -0700 | [diff] [blame] | 405 | mBubbleRect.offsetTo((mBitmapWidth-mBubbleRect.width())/2, 0); |
| 406 | |
Joe Onorato | 080d9b6 | 2009-11-02 12:01:11 -0500 | [diff] [blame] | 407 | if (false) { |
| 408 | Log.d(TAG, "mBitmapWidth=" + mBitmapWidth + " mBitmapHeight=" |
| 409 | + mBitmapHeight + " w=" + ((int)(mBubbleRect.width() + 0.5f)) |
| 410 | + " h=" + ((int)((MAX_LINES * mLineHeight) + leading + 0.5f))); |
| 411 | } |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | /** You own the bitmap after this and you must call recycle on it. */ |
| 415 | Bitmap createTextBitmap(String text) { |
Jason Sams | 6ec11bc | 2010-01-19 17:56:52 -0800 | [diff] [blame] | 416 | Bitmap b = Bitmap.createBitmap(mBitmapWidth, mBitmapHeight, Bitmap.Config.ALPHA_8); |
Romain Guy | 88f38d1 | 2010-01-26 14:50:34 -0800 | [diff] [blame] | 417 | b.setDensity(mDensity); |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 418 | Canvas c = new Canvas(b); |
| 419 | |
| 420 | StaticLayout layout = new StaticLayout(text, mTextPaint, (int)mTextWidth, |
| 421 | Alignment.ALIGN_CENTER, 1, 0, true); |
| 422 | int lineCount = layout.getLineCount(); |
| 423 | if (lineCount > MAX_LINES) { |
| 424 | lineCount = MAX_LINES; |
| 425 | } |
Jason Sams | fd22dac | 2009-09-20 17:24:16 -0700 | [diff] [blame] | 426 | //if (!TEXT_BURN && lineCount > 0) { |
| 427 | //RectF bubbleRect = mBubbleRect; |
| 428 | //bubbleRect.bottom = height(lineCount); |
| 429 | //c.drawRoundRect(bubbleRect, mCornerRadius, mCornerRadius, mRectPaint); |
| 430 | //} |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 431 | for (int i=0; i<lineCount; i++) { |
Joe Onorato | fccc8df | 2009-10-01 14:30:16 -0700 | [diff] [blame] | 432 | //int x = (int)((mBubbleRect.width() - layout.getLineMax(i)) / 2.0f); |
| 433 | //int y = mFirstLineY + (i * mLineHeight); |
Romain Guy | 88f38d1 | 2010-01-26 14:50:34 -0800 | [diff] [blame] | 434 | final String lineText = text.substring(layout.getLineStart(i), layout.getLineEnd(i)); |
Joe Onorato | fccc8df | 2009-10-01 14:30:16 -0700 | [diff] [blame] | 435 | int x = (int)(mBubbleRect.left |
Romain Guy | 88f38d1 | 2010-01-26 14:50:34 -0800 | [diff] [blame] | 436 | + ((mBubbleRect.width() - mTextPaint.measureText(lineText)) * 0.5f)); |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 437 | int y = mFirstLineY + (i * mLineHeight); |
Romain Guy | 88f38d1 | 2010-01-26 14:50:34 -0800 | [diff] [blame] | 438 | c.drawText(lineText, x, y, mTextPaint); |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | return b; |
| 442 | } |
Joe Onorato | 43e7bcf | 2009-08-08 18:53:53 -0700 | [diff] [blame] | 443 | |
| 444 | private int height(int lineCount) { |
| 445 | return (int)((lineCount * mLineHeight) + mLeading + mLeading + 0.0f); |
| 446 | } |
| 447 | |
| 448 | int getBubbleWidth() { |
| 449 | return (int)(mBubbleRect.width() + 0.5f); |
| 450 | } |
| 451 | |
| 452 | int getMaxBubbleHeight() { |
| 453 | return height(MAX_LINES); |
| 454 | } |
| 455 | |
| 456 | int getBitmapWidth() { |
| 457 | return mBitmapWidth; |
| 458 | } |
| 459 | |
| 460 | int getBitmapHeight() { |
| 461 | return mBitmapHeight; |
| 462 | } |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | /** Only works for positive numbers. */ |
| 466 | static int roundToPow2(int n) { |
| 467 | int orig = n; |
| 468 | n >>= 1; |
| 469 | int mask = 0x8000000; |
| 470 | while (mask != 0 && (n & mask) == 0) { |
| 471 | mask >>= 1; |
| 472 | } |
| 473 | while (mask != 0) { |
| 474 | n |= mask; |
| 475 | mask >>= 1; |
| 476 | } |
| 477 | n += 1; |
| 478 | if (n != orig) { |
| 479 | n <<= 1; |
| 480 | } |
| 481 | return n; |
| 482 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 483 | } |