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 | |
Romain Guy | edcce09 | 2010-03-04 13:03:17 -0800 | [diff] [blame] | 41 | import com.android.launcher.R; |
| 42 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 43 | /** |
| 44 | * Various utilities shared amongst the Launcher's classes. |
| 45 | */ |
| 46 | final class Utilities { |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 47 | private static final String TAG = "Launcher.Utilities"; |
| 48 | |
Joe Onorato | 9392a75 | 2009-09-15 17:13:09 -0400 | [diff] [blame] | 49 | private static final boolean TEXT_BURN = false; |
| 50 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 51 | private static int sIconWidth = -1; |
| 52 | private static int sIconHeight = -1; |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 53 | private static int sIconTextureWidth = -1; |
| 54 | private static int sIconTextureHeight = -1; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 55 | |
| 56 | private static final Paint sPaint = new Paint(); |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 57 | private static final Paint sBlurPaint = new Paint(); |
Joe Onorato | eb8325a | 2009-11-08 13:20:30 -0500 | [diff] [blame] | 58 | private static final Paint sGlowColorPressedPaint = new Paint(); |
Joe Onorato | c61cff9 | 2009-11-08 11:54:39 -0500 | [diff] [blame] | 59 | private static final Paint sGlowColorFocusedPaint = new Paint(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 60 | private static final Rect sBounds = new Rect(); |
| 61 | private static final Rect sOldBounds = new Rect(); |
Romain Guy | 89911d2 | 2009-09-28 18:48:49 -0700 | [diff] [blame] | 62 | private static final Canvas sCanvas = new Canvas(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 63 | |
| 64 | static { |
| 65 | sCanvas.setDrawFilter(new PaintFlagsDrawFilter(Paint.DITHER_FLAG, |
| 66 | Paint.FILTER_BITMAP_FLAG)); |
| 67 | } |
| 68 | |
| 69 | static Bitmap centerToFit(Bitmap bitmap, int width, int height, Context context) { |
| 70 | final int bitmapWidth = bitmap.getWidth(); |
| 71 | final int bitmapHeight = bitmap.getHeight(); |
| 72 | |
| 73 | if (bitmapWidth < width || bitmapHeight < height) { |
| 74 | int color = context.getResources().getColor(R.color.window_background); |
| 75 | |
| 76 | Bitmap centered = Bitmap.createBitmap(bitmapWidth < width ? width : bitmapWidth, |
| 77 | bitmapHeight < height ? height : bitmapHeight, Bitmap.Config.RGB_565); |
Dianne Hackborn | 32ce7f1 | 2009-07-22 21:56:50 -0700 | [diff] [blame] | 78 | centered.setDensity(bitmap.getDensity()); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 79 | Canvas canvas = new Canvas(centered); |
| 80 | canvas.drawColor(color); |
| 81 | canvas.drawBitmap(bitmap, (width - bitmapWidth) / 2.0f, (height - bitmapHeight) / 2.0f, |
| 82 | null); |
| 83 | |
| 84 | bitmap = centered; |
| 85 | } |
| 86 | |
| 87 | return bitmap; |
| 88 | } |
| 89 | |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 90 | static int sColors[] = { 0xffff0000, 0xff00ff00, 0xff0000ff }; |
| 91 | static int sColorIndex = 0; |
| 92 | |
| 93 | /** |
| 94 | * Returns a bitmap suitable for the all apps view. The bitmap will be a power |
| 95 | * of two sized ARGB_8888 bitmap that can be used as a gl texture. |
| 96 | */ |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 97 | static Bitmap createIconBitmap(Drawable icon, Context context) { |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 98 | synchronized (sCanvas) { // we share the statics :-( |
| 99 | if (sIconWidth == -1) { |
| 100 | initStatics(context); |
| 101 | } |
| 102 | |
| 103 | int width = sIconWidth; |
| 104 | int height = sIconHeight; |
| 105 | |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 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()); |
| 116 | } |
| 117 | } |
| 118 | int sourceWidth = icon.getIntrinsicWidth(); |
| 119 | int sourceHeight = icon.getIntrinsicHeight(); |
| 120 | |
| 121 | if (sourceWidth > 0 && sourceWidth > 0) { |
| 122 | // There are intrinsic sizes. |
Romain Guy | 89911d2 | 2009-09-28 18:48:49 -0700 | [diff] [blame] | 123 | if (width < sourceWidth || height < sourceHeight) { |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 124 | // It's too big, scale it down. |
| 125 | final float ratio = (float) sourceWidth / sourceHeight; |
| 126 | if (sourceWidth > sourceHeight) { |
| 127 | height = (int) (width / ratio); |
| 128 | } else if (sourceHeight > sourceWidth) { |
| 129 | width = (int) (height * ratio); |
| 130 | } |
| 131 | } else if (sourceWidth < width && sourceHeight < height) { |
| 132 | // It's small, use the size they gave us. |
| 133 | width = sourceWidth; |
Romain Guy | 89911d2 | 2009-09-28 18:48:49 -0700 | [diff] [blame] | 134 | height = sourceHeight; |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | |
| 138 | // no intrinsic size --> use default size |
| 139 | int textureWidth = sIconTextureWidth; |
| 140 | int textureHeight = sIconTextureHeight; |
| 141 | |
| 142 | final Bitmap bitmap = Bitmap.createBitmap(textureWidth, textureHeight, |
| 143 | Bitmap.Config.ARGB_8888); |
| 144 | final Canvas canvas = sCanvas; |
| 145 | canvas.setBitmap(bitmap); |
| 146 | |
| 147 | final int left = (textureWidth-width) / 2; |
| 148 | final int top = (textureHeight-height) / 2; |
| 149 | |
| 150 | if (false) { |
| 151 | // draw a big box for the icon for debugging |
| 152 | canvas.drawColor(sColors[sColorIndex]); |
| 153 | if (++sColorIndex >= sColors.length) sColorIndex = 0; |
| 154 | Paint debugPaint = new Paint(); |
| 155 | debugPaint.setColor(0xffcccc00); |
| 156 | canvas.drawRect(left, top, left+width, top+height, debugPaint); |
| 157 | } |
| 158 | |
| 159 | sOldBounds.set(icon.getBounds()); |
| 160 | icon.setBounds(left, top, left+width, top+height); |
| 161 | icon.draw(canvas); |
| 162 | icon.setBounds(sOldBounds); |
| 163 | |
| 164 | return bitmap; |
| 165 | } |
| 166 | } |
| 167 | |
Joe Onorato | c61cff9 | 2009-11-08 11:54:39 -0500 | [diff] [blame] | 168 | static void drawSelectedAllAppsBitmap(Canvas dest, int destWidth, int destHeight, |
Joe Onorato | eb8325a | 2009-11-08 13:20:30 -0500 | [diff] [blame] | 169 | boolean pressed, Bitmap src) { |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 170 | synchronized (sCanvas) { // we share the statics :-( |
| 171 | if (sIconWidth == -1) { |
| 172 | // We can't have gotten to here without src being initialized, which |
| 173 | // comes from this file already. So just assert. |
| 174 | //initStatics(context); |
| 175 | throw new RuntimeException("Assertion failed: Utilities not initialized"); |
| 176 | } |
| 177 | |
| 178 | dest.drawColor(0, PorterDuff.Mode.CLEAR); |
Jason Sams | b4ecab2 | 2010-01-19 16:43:26 -0800 | [diff] [blame] | 179 | |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 180 | int[] xy = new int[2]; |
Mike Reed | cdd1179 | 2009-10-29 17:27:55 -0400 | [diff] [blame] | 181 | Bitmap mask = src.extractAlpha(sBlurPaint, xy); |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 182 | |
Mike Reed | cdd1179 | 2009-10-29 17:27:55 -0400 | [diff] [blame] | 183 | float px = (destWidth - src.getWidth()) / 2; |
| 184 | float py = (destHeight - src.getHeight()) / 2; |
Joe Onorato | c61cff9 | 2009-11-08 11:54:39 -0500 | [diff] [blame] | 185 | dest.drawBitmap(mask, px + xy[0], py + xy[1], |
Joe Onorato | eb8325a | 2009-11-08 13:20:30 -0500 | [diff] [blame] | 186 | pressed ? sGlowColorPressedPaint : sGlowColorFocusedPaint); |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 187 | |
| 188 | mask.recycle(); |
| 189 | } |
| 190 | } |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 191 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 192 | /** |
| 193 | * Returns a Bitmap representing the thumbnail of the specified Bitmap. |
| 194 | * The size of the thumbnail is defined by the dimension |
| 195 | * android.R.dimen.launcher_application_icon_size. |
| 196 | * |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 197 | * @param bitmap The bitmap to get a thumbnail of. |
| 198 | * @param context The application's context. |
| 199 | * |
| 200 | * @return A thumbnail for the specified bitmap or the bitmap itself if the |
| 201 | * thumbnail could not be created. |
| 202 | */ |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 203 | static Bitmap resampleIconBitmap(Bitmap bitmap, Context context) { |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 204 | synchronized (sCanvas) { // we share the statics :-( |
| 205 | if (sIconWidth == -1) { |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 206 | initStatics(context); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 207 | } |
| 208 | |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 209 | if (bitmap.getWidth() == sIconWidth && bitmap.getHeight() == sIconHeight) { |
| 210 | return bitmap; |
| 211 | } else { |
| 212 | return createIconBitmap(new BitmapDrawable(bitmap), context); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 213 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 214 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 215 | } |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 216 | |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 217 | private static void initStatics(Context context) { |
| 218 | final Resources resources = context.getResources(); |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 219 | final DisplayMetrics metrics = resources.getDisplayMetrics(); |
| 220 | final float density = metrics.density; |
| 221 | |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 222 | sIconWidth = sIconHeight = (int) resources.getDimension(android.R.dimen.app_icon_size); |
Jason Sams | b4ecab2 | 2010-01-19 16:43:26 -0800 | [diff] [blame] | 223 | sIconTextureWidth = sIconTextureHeight = sIconWidth + 2; |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 224 | |
Joe Onorato | a4c0cb9 | 2009-11-02 10:42:02 -0500 | [diff] [blame] | 225 | sBlurPaint.setMaskFilter(new BlurMaskFilter(5 * density, BlurMaskFilter.Blur.NORMAL)); |
Joe Onorato | eb8325a | 2009-11-08 13:20:30 -0500 | [diff] [blame] | 226 | sGlowColorPressedPaint.setColor(0xffffc300); |
| 227 | sGlowColorPressedPaint.setMaskFilter(TableMaskFilter.CreateClipTable(0, 30)); |
Joe Onorato | c61cff9 | 2009-11-08 11:54:39 -0500 | [diff] [blame] | 228 | sGlowColorFocusedPaint.setColor(0xffff8e00); |
| 229 | sGlowColorFocusedPaint.setMaskFilter(TableMaskFilter.CreateClipTable(0, 30)); |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 232 | static class BubbleText { |
| 233 | private static final int MAX_LINES = 2; |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 234 | |
Romain Guy | 88f38d1 | 2010-01-26 14:50:34 -0800 | [diff] [blame] | 235 | private final TextPaint mTextPaint; |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 236 | |
Romain Guy | 88f38d1 | 2010-01-26 14:50:34 -0800 | [diff] [blame] | 237 | private final RectF mBubbleRect = new RectF(); |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 238 | |
Romain Guy | 88f38d1 | 2010-01-26 14:50:34 -0800 | [diff] [blame] | 239 | private final float mTextWidth; |
| 240 | private final int mLeading; |
| 241 | private final int mFirstLineY; |
| 242 | private final int mLineHeight; |
| 243 | |
| 244 | private final int mBitmapWidth; |
| 245 | private final int mBitmapHeight; |
| 246 | private final int mDensity; |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 247 | |
| 248 | BubbleText(Context context) { |
| 249 | final Resources resources = context.getResources(); |
| 250 | |
Romain Guy | 88f38d1 | 2010-01-26 14:50:34 -0800 | [diff] [blame] | 251 | final DisplayMetrics metrics = resources.getDisplayMetrics(); |
| 252 | final float scale = metrics.density; |
| 253 | mDensity = metrics.densityDpi; |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 254 | |
Kenny Root | 9705164 | 2010-03-05 13:24:08 -0800 | [diff] [blame^] | 255 | final float paddingLeft = 2.0f * scale; |
| 256 | final float paddingRight = 2.0f * scale; |
Romain Guy | 442eda2 | 2010-02-04 15:59:37 -0800 | [diff] [blame] | 257 | final float cellWidth = resources.getDimension(R.dimen.title_texture_width); |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 258 | |
| 259 | RectF bubbleRect = mBubbleRect; |
| 260 | bubbleRect.left = 0; |
| 261 | bubbleRect.top = 0; |
Romain Guy | c0c4fe3 | 2010-02-12 23:20:25 +0100 | [diff] [blame] | 262 | bubbleRect.right = (int) cellWidth; |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 263 | |
Kenny Root | 9705164 | 2010-03-05 13:24:08 -0800 | [diff] [blame^] | 264 | mTextWidth = cellWidth - paddingLeft - paddingRight; |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 265 | |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 266 | TextPaint textPaint = mTextPaint = new TextPaint(); |
Joe Onorato | efabe00 | 2009-08-28 09:38:18 -0700 | [diff] [blame] | 267 | textPaint.setTypeface(Typeface.DEFAULT); |
| 268 | textPaint.setTextSize(13*scale); |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 269 | textPaint.setColor(0xffffffff); |
Joe Onorato | 43e7bcf | 2009-08-08 18:53:53 -0700 | [diff] [blame] | 270 | textPaint.setAntiAlias(true); |
Joe Onorato | 9392a75 | 2009-09-15 17:13:09 -0400 | [diff] [blame] | 271 | if (TEXT_BURN) { |
| 272 | textPaint.setShadowLayer(8, 0, 0, 0xff000000); |
| 273 | } |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 274 | |
| 275 | float ascent = -textPaint.ascent(); |
| 276 | float descent = textPaint.descent(); |
Joe Onorato | 43e7bcf | 2009-08-08 18:53:53 -0700 | [diff] [blame] | 277 | float leading = 0.0f;//(ascent+descent) * 0.1f; |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 278 | mLeading = (int)(leading + 0.5f); |
| 279 | mFirstLineY = (int)(leading + ascent + 0.5f); |
| 280 | mLineHeight = (int)(leading + ascent + descent + 0.5f); |
| 281 | |
Romain Guy | 442eda2 | 2010-02-04 15:59:37 -0800 | [diff] [blame] | 282 | mBitmapWidth = (int)(mBubbleRect.width() + 0.5f); |
Joe Onorato | 43e7bcf | 2009-08-08 18:53:53 -0700 | [diff] [blame] | 283 | mBitmapHeight = roundToPow2((int)((MAX_LINES * mLineHeight) + leading + 0.5f)); |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 284 | |
Joe Onorato | fccc8df | 2009-10-01 14:30:16 -0700 | [diff] [blame] | 285 | mBubbleRect.offsetTo((mBitmapWidth-mBubbleRect.width())/2, 0); |
| 286 | |
Joe Onorato | 080d9b6 | 2009-11-02 12:01:11 -0500 | [diff] [blame] | 287 | if (false) { |
| 288 | Log.d(TAG, "mBitmapWidth=" + mBitmapWidth + " mBitmapHeight=" |
| 289 | + mBitmapHeight + " w=" + ((int)(mBubbleRect.width() + 0.5f)) |
| 290 | + " h=" + ((int)((MAX_LINES * mLineHeight) + leading + 0.5f))); |
| 291 | } |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | /** You own the bitmap after this and you must call recycle on it. */ |
| 295 | Bitmap createTextBitmap(String text) { |
Jason Sams | 6ec11bc | 2010-01-19 17:56:52 -0800 | [diff] [blame] | 296 | Bitmap b = Bitmap.createBitmap(mBitmapWidth, mBitmapHeight, Bitmap.Config.ALPHA_8); |
Romain Guy | 88f38d1 | 2010-01-26 14:50:34 -0800 | [diff] [blame] | 297 | b.setDensity(mDensity); |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 298 | Canvas c = new Canvas(b); |
| 299 | |
| 300 | StaticLayout layout = new StaticLayout(text, mTextPaint, (int)mTextWidth, |
| 301 | Alignment.ALIGN_CENTER, 1, 0, true); |
| 302 | int lineCount = layout.getLineCount(); |
| 303 | if (lineCount > MAX_LINES) { |
| 304 | lineCount = MAX_LINES; |
| 305 | } |
Jason Sams | fd22dac | 2009-09-20 17:24:16 -0700 | [diff] [blame] | 306 | //if (!TEXT_BURN && lineCount > 0) { |
| 307 | //RectF bubbleRect = mBubbleRect; |
| 308 | //bubbleRect.bottom = height(lineCount); |
| 309 | //c.drawRoundRect(bubbleRect, mCornerRadius, mCornerRadius, mRectPaint); |
| 310 | //} |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 311 | for (int i=0; i<lineCount; i++) { |
Joe Onorato | fccc8df | 2009-10-01 14:30:16 -0700 | [diff] [blame] | 312 | //int x = (int)((mBubbleRect.width() - layout.getLineMax(i)) / 2.0f); |
| 313 | //int y = mFirstLineY + (i * mLineHeight); |
Romain Guy | 88f38d1 | 2010-01-26 14:50:34 -0800 | [diff] [blame] | 314 | final String lineText = text.substring(layout.getLineStart(i), layout.getLineEnd(i)); |
Joe Onorato | fccc8df | 2009-10-01 14:30:16 -0700 | [diff] [blame] | 315 | int x = (int)(mBubbleRect.left |
Romain Guy | 88f38d1 | 2010-01-26 14:50:34 -0800 | [diff] [blame] | 316 | + ((mBubbleRect.width() - mTextPaint.measureText(lineText)) * 0.5f)); |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 317 | int y = mFirstLineY + (i * mLineHeight); |
Romain Guy | 88f38d1 | 2010-01-26 14:50:34 -0800 | [diff] [blame] | 318 | c.drawText(lineText, x, y, mTextPaint); |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | return b; |
| 322 | } |
Joe Onorato | 43e7bcf | 2009-08-08 18:53:53 -0700 | [diff] [blame] | 323 | |
| 324 | private int height(int lineCount) { |
| 325 | return (int)((lineCount * mLineHeight) + mLeading + mLeading + 0.0f); |
| 326 | } |
| 327 | |
| 328 | int getBubbleWidth() { |
| 329 | return (int)(mBubbleRect.width() + 0.5f); |
| 330 | } |
| 331 | |
| 332 | int getMaxBubbleHeight() { |
| 333 | return height(MAX_LINES); |
| 334 | } |
| 335 | |
| 336 | int getBitmapWidth() { |
| 337 | return mBitmapWidth; |
| 338 | } |
| 339 | |
| 340 | int getBitmapHeight() { |
| 341 | return mBitmapHeight; |
| 342 | } |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | /** Only works for positive numbers. */ |
| 346 | static int roundToPow2(int n) { |
| 347 | int orig = n; |
| 348 | n >>= 1; |
| 349 | int mask = 0x8000000; |
| 350 | while (mask != 0 && (n & mask) == 0) { |
| 351 | mask >>= 1; |
| 352 | } |
| 353 | while (mask != 0) { |
| 354 | n |= mask; |
| 355 | mask >>= 1; |
| 356 | } |
| 357 | n += 1; |
| 358 | if (n != orig) { |
| 359 | n <<= 1; |
| 360 | } |
| 361 | return n; |
| 362 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 363 | } |