blob: 30108af0bd91b05a635b3efe6fb8efe196272479 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
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 Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Mitsuru Oshima583ed3b2009-05-12 19:19:10 -070019import android.graphics.drawable.BitmapDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080020import android.graphics.drawable.Drawable;
21import android.graphics.drawable.PaintDrawable;
22import android.graphics.Bitmap;
Joe Onorato1291a8c2009-09-15 15:07:25 -040023import android.graphics.BlurMaskFilter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080024import android.graphics.Canvas;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080025import android.graphics.Paint;
Joe Onoratobf15cb42009-08-07 14:33:40 -070026import android.graphics.PaintFlagsDrawFilter;
27import android.graphics.PixelFormat;
Joe Onorato1291a8c2009-09-15 15:07:25 -040028import android.graphics.PorterDuff;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.graphics.Rect;
Joe Onoratobf15cb42009-08-07 14:33:40 -070030import android.graphics.RectF;
Mike Reedcdd11792009-10-29 17:27:55 -040031import android.graphics.TableMaskFilter;
Joe Onoratobf15cb42009-08-07 14:33:40 -070032import android.graphics.Typeface;
33import android.text.Layout.Alignment;
34import android.text.StaticLayout;
35import android.text.TextPaint;
Dianne Hackborn32ce7f12009-07-22 21:56:50 -070036import android.util.DisplayMetrics;
Joe Onoratobf15cb42009-08-07 14:33:40 -070037import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.content.res.Resources;
39import android.content.Context;
40
41/**
42 * Various utilities shared amongst the Launcher's classes.
43 */
44final class Utilities {
Joe Onorato1291a8c2009-09-15 15:07:25 -040045 private static final String TAG = "Launcher.Utilities";
46
Joe Onorato9392a752009-09-15 17:13:09 -040047 private static final boolean TEXT_BURN = false;
48
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049 private static int sIconWidth = -1;
50 private static int sIconHeight = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -070051 private static int sIconTextureWidth = -1;
52 private static int sIconTextureHeight = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053
54 private static final Paint sPaint = new Paint();
Joe Onorato1291a8c2009-09-15 15:07:25 -040055 private static final Paint sBlurPaint = new Paint();
Joe Onoratoeb8325a2009-11-08 13:20:30 -050056 private static final Paint sGlowColorPressedPaint = new Paint();
Joe Onoratoc61cff92009-11-08 11:54:39 -050057 private static final Paint sGlowColorFocusedPaint = new Paint();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058 private static final Rect sBounds = new Rect();
59 private static final Rect sOldBounds = new Rect();
Romain Guy89911d22009-09-28 18:48:49 -070060 private static final Canvas sCanvas = new Canvas();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061
62 static {
63 sCanvas.setDrawFilter(new PaintFlagsDrawFilter(Paint.DITHER_FLAG,
64 Paint.FILTER_BITMAP_FLAG));
65 }
66
67 static Bitmap centerToFit(Bitmap bitmap, int width, int height, Context context) {
68 final int bitmapWidth = bitmap.getWidth();
69 final int bitmapHeight = bitmap.getHeight();
70
71 if (bitmapWidth < width || bitmapHeight < height) {
72 int color = context.getResources().getColor(R.color.window_background);
73
74 Bitmap centered = Bitmap.createBitmap(bitmapWidth < width ? width : bitmapWidth,
75 bitmapHeight < height ? height : bitmapHeight, Bitmap.Config.RGB_565);
Dianne Hackborn32ce7f12009-07-22 21:56:50 -070076 centered.setDensity(bitmap.getDensity());
The Android Open Source Project31dd5032009-03-03 19:32:27 -080077 Canvas canvas = new Canvas(centered);
78 canvas.drawColor(color);
79 canvas.drawBitmap(bitmap, (width - bitmapWidth) / 2.0f, (height - bitmapHeight) / 2.0f,
80 null);
81
82 bitmap = centered;
83 }
84
85 return bitmap;
86 }
87
88 /**
89 * Returns a Drawable representing the thumbnail of the specified Drawable.
90 * The size of the thumbnail is defined by the dimension
91 * android.R.dimen.launcher_application_icon_size.
92 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -080093 * @param icon The icon to get a thumbnail of.
94 * @param context The application's context.
95 *
96 * @return A thumbnail for the specified icon or the icon itself if the
Jason Samsfd22dac2009-09-20 17:24:16 -070097 * thumbnail could not be created.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080098 */
Joe Onorato6665c0f2009-09-02 15:27:24 -070099 static Drawable createIconThumbnail(Drawable icon, Context context) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400100 synchronized (sCanvas) { // we share the statics :-(
101 if (sIconWidth == -1) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700102 initStatics(context);
Mitsuru Oshima583ed3b2009-05-12 19:19:10 -0700103 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800104
Joe Onorato9c1289c2009-08-17 11:03:03 -0400105 int width = sIconWidth;
106 int height = sIconHeight;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800107
Joe Onorato9c1289c2009-08-17 11:03:03 -0400108 if (icon instanceof PaintDrawable) {
109 PaintDrawable painter = (PaintDrawable) icon;
110 painter.setIntrinsicWidth(width);
111 painter.setIntrinsicHeight(height);
112 } else if (icon instanceof BitmapDrawable) {
113 // Ensure the bitmap has a density.
114 BitmapDrawable bitmapDrawable = (BitmapDrawable) icon;
115 Bitmap bitmap = bitmapDrawable.getBitmap();
116 if (bitmap.getDensity() == Bitmap.DENSITY_NONE) {
117 bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800118 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400119 }
120 int iconWidth = icon.getIntrinsicWidth();
121 int iconHeight = icon.getIntrinsicHeight();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800122
Romain Guy89911d22009-09-28 18:48:49 -0700123 if (iconWidth > 0 && iconHeight > 0) {
124 if (width < iconWidth || height < iconHeight) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400125 final float ratio = (float) iconWidth / iconHeight;
126
127 if (iconWidth > iconHeight) {
128 height = (int) (width / ratio);
129 } else if (iconHeight > iconWidth) {
130 width = (int) (height * ratio);
131 }
132
133 final Bitmap.Config c = icon.getOpacity() != PixelFormat.OPAQUE ?
134 Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
135 final Bitmap thumb = Bitmap.createBitmap(sIconWidth, sIconHeight, c);
136 final Canvas canvas = sCanvas;
137 canvas.setBitmap(thumb);
138 // Copy the old bounds to restore them later
139 // If we were to do oldBounds = icon.getBounds(),
140 // the call to setBounds() that follows would
141 // change the same instance and we would lose the
142 // old bounds
143 sOldBounds.set(icon.getBounds());
144 final int x = (sIconWidth - width) / 2;
145 final int y = (sIconHeight - height) / 2;
146 icon.setBounds(x, y, x + width, y + height);
147 icon.draw(canvas);
148 icon.setBounds(sOldBounds);
149 icon = new FastBitmapDrawable(thumb);
150 } else if (iconWidth < width && iconHeight < height) {
151 final Bitmap.Config c = Bitmap.Config.ARGB_8888;
152 final Bitmap thumb = Bitmap.createBitmap(sIconWidth, sIconHeight, c);
153 final Canvas canvas = sCanvas;
154 canvas.setBitmap(thumb);
155 sOldBounds.set(icon.getBounds());
156 final int x = (width - iconWidth) / 2;
157 final int y = (height - iconHeight) / 2;
158 icon.setBounds(x, y, x + iconWidth, y + iconHeight);
159 icon.draw(canvas);
160 icon.setBounds(sOldBounds);
161 icon = new FastBitmapDrawable(thumb);
162 }
163 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164
Joe Onorato9c1289c2009-08-17 11:03:03 -0400165 return icon;
166 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167 }
168
Joe Onorato6665c0f2009-09-02 15:27:24 -0700169 static int sColors[] = { 0xffff0000, 0xff00ff00, 0xff0000ff };
170 static int sColorIndex = 0;
171
172 /**
173 * Returns a bitmap suitable for the all apps view. The bitmap will be a power
174 * of two sized ARGB_8888 bitmap that can be used as a gl texture.
175 */
176 static Bitmap createAllAppsBitmap(Drawable icon, Context context) {
177 synchronized (sCanvas) { // we share the statics :-(
178 if (sIconWidth == -1) {
179 initStatics(context);
180 }
181
182 int width = sIconWidth;
183 int height = sIconHeight;
184
Joe Onorato6665c0f2009-09-02 15:27:24 -0700185 if (icon instanceof PaintDrawable) {
186 PaintDrawable painter = (PaintDrawable) icon;
187 painter.setIntrinsicWidth(width);
188 painter.setIntrinsicHeight(height);
189 } else if (icon instanceof BitmapDrawable) {
190 // Ensure the bitmap has a density.
191 BitmapDrawable bitmapDrawable = (BitmapDrawable) icon;
192 Bitmap bitmap = bitmapDrawable.getBitmap();
193 if (bitmap.getDensity() == Bitmap.DENSITY_NONE) {
194 bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics());
195 }
196 }
197 int sourceWidth = icon.getIntrinsicWidth();
198 int sourceHeight = icon.getIntrinsicHeight();
199
200 if (sourceWidth > 0 && sourceWidth > 0) {
201 // There are intrinsic sizes.
Romain Guy89911d22009-09-28 18:48:49 -0700202 if (width < sourceWidth || height < sourceHeight) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700203 // It's too big, scale it down.
204 final float ratio = (float) sourceWidth / sourceHeight;
205 if (sourceWidth > sourceHeight) {
206 height = (int) (width / ratio);
207 } else if (sourceHeight > sourceWidth) {
208 width = (int) (height * ratio);
209 }
210 } else if (sourceWidth < width && sourceHeight < height) {
211 // It's small, use the size they gave us.
212 width = sourceWidth;
Romain Guy89911d22009-09-28 18:48:49 -0700213 height = sourceHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700214 }
215 }
216
217 // no intrinsic size --> use default size
218 int textureWidth = sIconTextureWidth;
219 int textureHeight = sIconTextureHeight;
220
221 final Bitmap bitmap = Bitmap.createBitmap(textureWidth, textureHeight,
222 Bitmap.Config.ARGB_8888);
223 final Canvas canvas = sCanvas;
224 canvas.setBitmap(bitmap);
225
226 final int left = (textureWidth-width) / 2;
227 final int top = (textureHeight-height) / 2;
228
229 if (false) {
230 // draw a big box for the icon for debugging
231 canvas.drawColor(sColors[sColorIndex]);
232 if (++sColorIndex >= sColors.length) sColorIndex = 0;
233 Paint debugPaint = new Paint();
234 debugPaint.setColor(0xffcccc00);
235 canvas.drawRect(left, top, left+width, top+height, debugPaint);
236 }
237
238 sOldBounds.set(icon.getBounds());
239 icon.setBounds(left, top, left+width, top+height);
240 icon.draw(canvas);
241 icon.setBounds(sOldBounds);
242
243 return bitmap;
244 }
245 }
246
Joe Onoratoc61cff92009-11-08 11:54:39 -0500247 static void drawSelectedAllAppsBitmap(Canvas dest, int destWidth, int destHeight,
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500248 boolean pressed, Bitmap src) {
Joe Onorato1291a8c2009-09-15 15:07:25 -0400249 synchronized (sCanvas) { // we share the statics :-(
250 if (sIconWidth == -1) {
251 // We can't have gotten to here without src being initialized, which
252 // comes from this file already. So just assert.
253 //initStatics(context);
254 throw new RuntimeException("Assertion failed: Utilities not initialized");
255 }
256
257 dest.drawColor(0, PorterDuff.Mode.CLEAR);
Jason Samsb4ecab22010-01-19 16:43:26 -0800258
Joe Onorato1291a8c2009-09-15 15:07:25 -0400259 int[] xy = new int[2];
Mike Reedcdd11792009-10-29 17:27:55 -0400260 Bitmap mask = src.extractAlpha(sBlurPaint, xy);
Joe Onorato1291a8c2009-09-15 15:07:25 -0400261
Mike Reedcdd11792009-10-29 17:27:55 -0400262 float px = (destWidth - src.getWidth()) / 2;
263 float py = (destHeight - src.getHeight()) / 2;
Joe Onoratoc61cff92009-11-08 11:54:39 -0500264 dest.drawBitmap(mask, px + xy[0], py + xy[1],
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500265 pressed ? sGlowColorPressedPaint : sGlowColorFocusedPaint);
Joe Onorato1291a8c2009-09-15 15:07:25 -0400266
267 mask.recycle();
268 }
269 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700270
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800271 /**
272 * Returns a Bitmap representing the thumbnail of the specified Bitmap.
273 * The size of the thumbnail is defined by the dimension
274 * android.R.dimen.launcher_application_icon_size.
275 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800276 * @param bitmap The bitmap to get a thumbnail of.
277 * @param context The application's context.
278 *
279 * @return A thumbnail for the specified bitmap or the bitmap itself if the
280 * thumbnail could not be created.
281 */
282 static Bitmap createBitmapThumbnail(Bitmap bitmap, Context context) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400283 synchronized (sCanvas) { // we share the statics :-(
284 if (sIconWidth == -1) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700285 initStatics(context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800286 }
287
Joe Onorato9c1289c2009-08-17 11:03:03 -0400288 int width = sIconWidth;
289 int height = sIconHeight;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800290
Joe Onorato9c1289c2009-08-17 11:03:03 -0400291 final int bitmapWidth = bitmap.getWidth();
292 final int bitmapHeight = bitmap.getHeight();
293
Romain Guy89911d22009-09-28 18:48:49 -0700294 if (width > 0 && height > 0) {
295 if (width < bitmapWidth || height < bitmapHeight) {
296 final float ratio = (float) bitmapWidth / bitmapHeight;
Jason Samsb4ecab22010-01-19 16:43:26 -0800297
Romain Guy89911d22009-09-28 18:48:49 -0700298 if (bitmapWidth > bitmapHeight) {
299 height = (int) (width / ratio);
300 } else if (bitmapHeight > bitmapWidth) {
301 width = (int) (height * ratio);
302 }
Jason Samsb4ecab22010-01-19 16:43:26 -0800303
Romain Guy89911d22009-09-28 18:48:49 -0700304 final Bitmap.Config c = (width == sIconWidth && height == sIconHeight) ?
305 bitmap.getConfig() : Bitmap.Config.ARGB_8888;
306 final Bitmap thumb = Bitmap.createBitmap(sIconWidth, sIconHeight, c);
307 final Canvas canvas = sCanvas;
308 final Paint paint = sPaint;
309 canvas.setBitmap(thumb);
310 paint.setDither(false);
311 paint.setFilterBitmap(true);
312 sBounds.set((sIconWidth - width) / 2, (sIconHeight - height) / 2, width, height);
313 sOldBounds.set(0, 0, bitmapWidth, bitmapHeight);
314 canvas.drawBitmap(bitmap, sOldBounds, sBounds, paint);
315 return thumb;
316 } else if (bitmapWidth < width || bitmapHeight < height) {
317 final Bitmap.Config c = Bitmap.Config.ARGB_8888;
318 final Bitmap thumb = Bitmap.createBitmap(sIconWidth, sIconHeight, c);
319 final Canvas canvas = sCanvas;
320 final Paint paint = sPaint;
321 canvas.setBitmap(thumb);
322 paint.setDither(false);
323 paint.setFilterBitmap(true);
324 canvas.drawBitmap(bitmap, (sIconWidth - bitmapWidth) / 2,
325 (sIconHeight - bitmapHeight) / 2, paint);
326 return thumb;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400327 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400328 }
329
330 return bitmap;
331 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800332 }
Joe Onoratobf15cb42009-08-07 14:33:40 -0700333
Joe Onorato6665c0f2009-09-02 15:27:24 -0700334 private static void initStatics(Context context) {
335 final Resources resources = context.getResources();
Joe Onorato1291a8c2009-09-15 15:07:25 -0400336 final DisplayMetrics metrics = resources.getDisplayMetrics();
337 final float density = metrics.density;
338
Joe Onorato6665c0f2009-09-02 15:27:24 -0700339 sIconWidth = sIconHeight = (int) resources.getDimension(android.R.dimen.app_icon_size);
Jason Samsb4ecab22010-01-19 16:43:26 -0800340 sIconTextureWidth = sIconTextureHeight = sIconWidth + 2;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400341
Joe Onoratoa4c0cb92009-11-02 10:42:02 -0500342 sBlurPaint.setMaskFilter(new BlurMaskFilter(5 * density, BlurMaskFilter.Blur.NORMAL));
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500343 sGlowColorPressedPaint.setColor(0xffffc300);
344 sGlowColorPressedPaint.setMaskFilter(TableMaskFilter.CreateClipTable(0, 30));
Joe Onoratoc61cff92009-11-08 11:54:39 -0500345 sGlowColorFocusedPaint.setColor(0xffff8e00);
346 sGlowColorFocusedPaint.setMaskFilter(TableMaskFilter.CreateClipTable(0, 30));
Joe Onorato6665c0f2009-09-02 15:27:24 -0700347 }
348
Joe Onoratobf15cb42009-08-07 14:33:40 -0700349 static class BubbleText {
350 private static final int MAX_LINES = 2;
Joe Onoratobf15cb42009-08-07 14:33:40 -0700351
Romain Guy88f38d12010-01-26 14:50:34 -0800352 private final TextPaint mTextPaint;
Joe Onoratobf15cb42009-08-07 14:33:40 -0700353
Romain Guy88f38d12010-01-26 14:50:34 -0800354 private final float mBubblePadding;
355 private final RectF mBubbleRect = new RectF();
Joe Onoratobf15cb42009-08-07 14:33:40 -0700356
Romain Guy88f38d12010-01-26 14:50:34 -0800357 private final float mTextWidth;
358 private final int mLeading;
359 private final int mFirstLineY;
360 private final int mLineHeight;
361
362 private final int mBitmapWidth;
363 private final int mBitmapHeight;
364 private final int mDensity;
Joe Onoratobf15cb42009-08-07 14:33:40 -0700365
366 BubbleText(Context context) {
367 final Resources resources = context.getResources();
368
Romain Guy88f38d12010-01-26 14:50:34 -0800369 final DisplayMetrics metrics = resources.getDisplayMetrics();
370 final float scale = metrics.density;
371 mDensity = metrics.densityDpi;
Joe Onoratobf15cb42009-08-07 14:33:40 -0700372
373 final float paddingLeft = 5.0f * scale;
374 final float paddingRight = 5.0f * scale;
Romain Guy442eda22010-02-04 15:59:37 -0800375 final float cellWidth = resources.getDimension(R.dimen.title_texture_width);
Joe Onoratobf15cb42009-08-07 14:33:40 -0700376 final float bubbleWidth = cellWidth - paddingLeft - paddingRight;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700377 mBubblePadding = 3.0f * scale;
Joe Onoratobf15cb42009-08-07 14:33:40 -0700378
379 RectF bubbleRect = mBubbleRect;
380 bubbleRect.left = 0;
381 bubbleRect.top = 0;
382 bubbleRect.right = (int)(bubbleWidth+0.5f);
383
Joe Onoratobf15cb42009-08-07 14:33:40 -0700384 mTextWidth = bubbleWidth - mBubblePadding - mBubblePadding;
385
Joe Onoratobf15cb42009-08-07 14:33:40 -0700386 TextPaint textPaint = mTextPaint = new TextPaint();
Joe Onoratoefabe002009-08-28 09:38:18 -0700387 textPaint.setTypeface(Typeface.DEFAULT);
388 textPaint.setTextSize(13*scale);
Joe Onoratobf15cb42009-08-07 14:33:40 -0700389 textPaint.setColor(0xffffffff);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700390 textPaint.setAntiAlias(true);
Joe Onorato9392a752009-09-15 17:13:09 -0400391 if (TEXT_BURN) {
392 textPaint.setShadowLayer(8, 0, 0, 0xff000000);
393 }
Joe Onoratobf15cb42009-08-07 14:33:40 -0700394
395 float ascent = -textPaint.ascent();
396 float descent = textPaint.descent();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700397 float leading = 0.0f;//(ascent+descent) * 0.1f;
Joe Onoratobf15cb42009-08-07 14:33:40 -0700398 mLeading = (int)(leading + 0.5f);
399 mFirstLineY = (int)(leading + ascent + 0.5f);
400 mLineHeight = (int)(leading + ascent + descent + 0.5f);
401
Romain Guy442eda22010-02-04 15:59:37 -0800402 mBitmapWidth = (int)(mBubbleRect.width() + 0.5f);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700403 mBitmapHeight = roundToPow2((int)((MAX_LINES * mLineHeight) + leading + 0.5f));
Joe Onoratobf15cb42009-08-07 14:33:40 -0700404
Joe Onoratofccc8df2009-10-01 14:30:16 -0700405 mBubbleRect.offsetTo((mBitmapWidth-mBubbleRect.width())/2, 0);
406
Joe Onorato080d9b62009-11-02 12:01:11 -0500407 if (false) {
408 Log.d(TAG, "mBitmapWidth=" + mBitmapWidth + " mBitmapHeight="
409 + mBitmapHeight + " w=" + ((int)(mBubbleRect.width() + 0.5f))
410 + " h=" + ((int)((MAX_LINES * mLineHeight) + leading + 0.5f)));
411 }
Joe Onoratobf15cb42009-08-07 14:33:40 -0700412 }
413
414 /** You own the bitmap after this and you must call recycle on it. */
415 Bitmap createTextBitmap(String text) {
Jason Sams6ec11bc2010-01-19 17:56:52 -0800416 Bitmap b = Bitmap.createBitmap(mBitmapWidth, mBitmapHeight, Bitmap.Config.ALPHA_8);
Romain Guy88f38d12010-01-26 14:50:34 -0800417 b.setDensity(mDensity);
Joe Onoratobf15cb42009-08-07 14:33:40 -0700418 Canvas c = new Canvas(b);
419
420 StaticLayout layout = new StaticLayout(text, mTextPaint, (int)mTextWidth,
421 Alignment.ALIGN_CENTER, 1, 0, true);
422 int lineCount = layout.getLineCount();
423 if (lineCount > MAX_LINES) {
424 lineCount = MAX_LINES;
425 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700426 //if (!TEXT_BURN && lineCount > 0) {
427 //RectF bubbleRect = mBubbleRect;
428 //bubbleRect.bottom = height(lineCount);
429 //c.drawRoundRect(bubbleRect, mCornerRadius, mCornerRadius, mRectPaint);
430 //}
Joe Onoratobf15cb42009-08-07 14:33:40 -0700431 for (int i=0; i<lineCount; i++) {
Joe Onoratofccc8df2009-10-01 14:30:16 -0700432 //int x = (int)((mBubbleRect.width() - layout.getLineMax(i)) / 2.0f);
433 //int y = mFirstLineY + (i * mLineHeight);
Romain Guy88f38d12010-01-26 14:50:34 -0800434 final String lineText = text.substring(layout.getLineStart(i), layout.getLineEnd(i));
Joe Onoratofccc8df2009-10-01 14:30:16 -0700435 int x = (int)(mBubbleRect.left
Romain Guy88f38d12010-01-26 14:50:34 -0800436 + ((mBubbleRect.width() - mTextPaint.measureText(lineText)) * 0.5f));
Joe Onoratobf15cb42009-08-07 14:33:40 -0700437 int y = mFirstLineY + (i * mLineHeight);
Romain Guy88f38d12010-01-26 14:50:34 -0800438 c.drawText(lineText, x, y, mTextPaint);
Joe Onoratobf15cb42009-08-07 14:33:40 -0700439 }
440
441 return b;
442 }
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700443
444 private int height(int lineCount) {
445 return (int)((lineCount * mLineHeight) + mLeading + mLeading + 0.0f);
446 }
447
448 int getBubbleWidth() {
449 return (int)(mBubbleRect.width() + 0.5f);
450 }
451
452 int getMaxBubbleHeight() {
453 return height(MAX_LINES);
454 }
455
456 int getBitmapWidth() {
457 return mBitmapWidth;
458 }
459
460 int getBitmapHeight() {
461 return mBitmapHeight;
462 }
Joe Onoratobf15cb42009-08-07 14:33:40 -0700463 }
464
465 /** Only works for positive numbers. */
466 static int roundToPow2(int n) {
467 int orig = n;
468 n >>= 1;
469 int mask = 0x8000000;
470 while (mask != 0 && (n & mask) == 0) {
471 mask >>= 1;
472 }
473 while (mask != 0) {
474 n |= mask;
475 mask >>= 1;
476 }
477 n += 1;
478 if (n != orig) {
479 n <<= 1;
480 }
481 return n;
482 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800483}