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