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