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