blob: 5aeef9bd593f632cb0fc718849033ad413e2413f [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;
Mike Reedcdd11792009-10-29 17:27:55 -040025import android.graphics.MaskFilter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080026import android.graphics.Paint;
Joe Onoratobf15cb42009-08-07 14:33:40 -070027import android.graphics.PaintFlagsDrawFilter;
28import android.graphics.PixelFormat;
Joe Onorato1291a8c2009-09-15 15:07:25 -040029import android.graphics.PorterDuff;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.graphics.Rect;
Joe Onoratobf15cb42009-08-07 14:33:40 -070031import android.graphics.RectF;
Mike Reedcdd11792009-10-29 17:27:55 -040032import android.graphics.TableMaskFilter;
Joe Onoratobf15cb42009-08-07 14:33:40 -070033import android.graphics.Typeface;
34import android.text.Layout.Alignment;
35import android.text.StaticLayout;
36import android.text.TextPaint;
Dianne Hackborn32ce7f12009-07-22 21:56:50 -070037import android.util.DisplayMetrics;
Joe Onoratobf15cb42009-08-07 14:33:40 -070038import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.content.res.Resources;
40import android.content.Context;
41
42/**
43 * Various utilities shared amongst the Launcher's classes.
44 */
45final class Utilities {
Joe Onorato1291a8c2009-09-15 15:07:25 -040046 private static final String TAG = "Launcher.Utilities";
47
Joe Onorato9392a752009-09-15 17:13:09 -040048 private static final boolean TEXT_BURN = false;
49
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050 private static int sIconWidth = -1;
51 private static int sIconHeight = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -070052 private static int sIconTextureWidth = -1;
53 private static int sIconTextureHeight = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054
55 private static final Paint sPaint = new Paint();
Joe Onorato1291a8c2009-09-15 15:07:25 -040056 private static final Paint sBlurPaint = new Paint();
57 private static final Paint sGlowColorPaint = new Paint();
58 private static final Paint sEmptyPaint = new Paint();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059 private static final Rect sBounds = new Rect();
60 private static final Rect sOldBounds = new Rect();
Romain Guy89911d22009-09-28 18:48:49 -070061 private static final Canvas sCanvas = new Canvas();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062
63 static {
64 sCanvas.setDrawFilter(new PaintFlagsDrawFilter(Paint.DITHER_FLAG,
65 Paint.FILTER_BITMAP_FLAG));
66 }
67
68 static Bitmap centerToFit(Bitmap bitmap, int width, int height, Context context) {
69 final int bitmapWidth = bitmap.getWidth();
70 final int bitmapHeight = bitmap.getHeight();
71
72 if (bitmapWidth < width || bitmapHeight < height) {
73 int color = context.getResources().getColor(R.color.window_background);
74
75 Bitmap centered = Bitmap.createBitmap(bitmapWidth < width ? width : bitmapWidth,
76 bitmapHeight < height ? height : bitmapHeight, Bitmap.Config.RGB_565);
Dianne Hackborn32ce7f12009-07-22 21:56:50 -070077 centered.setDensity(bitmap.getDensity());
The Android Open Source Project31dd5032009-03-03 19:32:27 -080078 Canvas canvas = new Canvas(centered);
79 canvas.drawColor(color);
80 canvas.drawBitmap(bitmap, (width - bitmapWidth) / 2.0f, (height - bitmapHeight) / 2.0f,
81 null);
82
83 bitmap = centered;
84 }
85
86 return bitmap;
87 }
88
89 /**
90 * Returns a Drawable representing the thumbnail of the specified Drawable.
91 * The size of the thumbnail is defined by the dimension
92 * android.R.dimen.launcher_application_icon_size.
93 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094 * @param icon The icon to get a thumbnail of.
95 * @param context The application's context.
96 *
97 * @return A thumbnail for the specified icon or the icon itself if the
Jason Samsfd22dac2009-09-20 17:24:16 -070098 * thumbnail could not be created.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080099 */
Joe Onorato6665c0f2009-09-02 15:27:24 -0700100 static Drawable createIconThumbnail(Drawable icon, Context context) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400101 synchronized (sCanvas) { // we share the statics :-(
102 if (sIconWidth == -1) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700103 initStatics(context);
Mitsuru Oshima583ed3b2009-05-12 19:19:10 -0700104 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800105
Joe Onorato9c1289c2009-08-17 11:03:03 -0400106 int width = sIconWidth;
107 int height = sIconHeight;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800108
Joe Onorato9c1289c2009-08-17 11:03:03 -0400109 if (icon instanceof PaintDrawable) {
110 PaintDrawable painter = (PaintDrawable) icon;
111 painter.setIntrinsicWidth(width);
112 painter.setIntrinsicHeight(height);
113 } else if (icon instanceof BitmapDrawable) {
114 // Ensure the bitmap has a density.
115 BitmapDrawable bitmapDrawable = (BitmapDrawable) icon;
116 Bitmap bitmap = bitmapDrawable.getBitmap();
117 if (bitmap.getDensity() == Bitmap.DENSITY_NONE) {
118 bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800119 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400120 }
121 int iconWidth = icon.getIntrinsicWidth();
122 int iconHeight = icon.getIntrinsicHeight();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123
Romain Guy89911d22009-09-28 18:48:49 -0700124 if (iconWidth > 0 && iconHeight > 0) {
125 if (width < iconWidth || height < iconHeight) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400126 final float ratio = (float) iconWidth / iconHeight;
127
128 if (iconWidth > iconHeight) {
129 height = (int) (width / ratio);
130 } else if (iconHeight > iconWidth) {
131 width = (int) (height * ratio);
132 }
133
134 final Bitmap.Config c = icon.getOpacity() != PixelFormat.OPAQUE ?
135 Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
136 final Bitmap thumb = Bitmap.createBitmap(sIconWidth, sIconHeight, c);
137 final Canvas canvas = sCanvas;
138 canvas.setBitmap(thumb);
139 // Copy the old bounds to restore them later
140 // If we were to do oldBounds = icon.getBounds(),
141 // the call to setBounds() that follows would
142 // change the same instance and we would lose the
143 // old bounds
144 sOldBounds.set(icon.getBounds());
145 final int x = (sIconWidth - width) / 2;
146 final int y = (sIconHeight - height) / 2;
147 icon.setBounds(x, y, x + width, y + height);
148 icon.draw(canvas);
149 icon.setBounds(sOldBounds);
150 icon = new FastBitmapDrawable(thumb);
151 } else if (iconWidth < width && iconHeight < height) {
152 final Bitmap.Config c = Bitmap.Config.ARGB_8888;
153 final Bitmap thumb = Bitmap.createBitmap(sIconWidth, sIconHeight, c);
154 final Canvas canvas = sCanvas;
155 canvas.setBitmap(thumb);
156 sOldBounds.set(icon.getBounds());
157 final int x = (width - iconWidth) / 2;
158 final int y = (height - iconHeight) / 2;
159 icon.setBounds(x, y, x + iconWidth, y + iconHeight);
160 icon.draw(canvas);
161 icon.setBounds(sOldBounds);
162 icon = new FastBitmapDrawable(thumb);
163 }
164 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165
Joe Onorato9c1289c2009-08-17 11:03:03 -0400166 return icon;
167 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168 }
169
Joe Onorato6665c0f2009-09-02 15:27:24 -0700170 static int sColors[] = { 0xffff0000, 0xff00ff00, 0xff0000ff };
171 static int sColorIndex = 0;
172
173 /**
174 * Returns a bitmap suitable for the all apps view. The bitmap will be a power
175 * of two sized ARGB_8888 bitmap that can be used as a gl texture.
176 */
177 static Bitmap createAllAppsBitmap(Drawable icon, Context context) {
178 synchronized (sCanvas) { // we share the statics :-(
179 if (sIconWidth == -1) {
180 initStatics(context);
181 }
182
183 int width = sIconWidth;
184 int height = sIconHeight;
185
Joe Onorato6665c0f2009-09-02 15:27:24 -0700186 if (icon instanceof PaintDrawable) {
187 PaintDrawable painter = (PaintDrawable) icon;
188 painter.setIntrinsicWidth(width);
189 painter.setIntrinsicHeight(height);
190 } else if (icon instanceof BitmapDrawable) {
191 // Ensure the bitmap has a density.
192 BitmapDrawable bitmapDrawable = (BitmapDrawable) icon;
193 Bitmap bitmap = bitmapDrawable.getBitmap();
194 if (bitmap.getDensity() == Bitmap.DENSITY_NONE) {
195 bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics());
196 }
197 }
198 int sourceWidth = icon.getIntrinsicWidth();
199 int sourceHeight = icon.getIntrinsicHeight();
200
201 if (sourceWidth > 0 && sourceWidth > 0) {
202 // There are intrinsic sizes.
Romain Guy89911d22009-09-28 18:48:49 -0700203 if (width < sourceWidth || height < sourceHeight) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700204 // It's too big, scale it down.
205 final float ratio = (float) sourceWidth / sourceHeight;
206 if (sourceWidth > sourceHeight) {
207 height = (int) (width / ratio);
208 } else if (sourceHeight > sourceWidth) {
209 width = (int) (height * ratio);
210 }
211 } else if (sourceWidth < width && sourceHeight < height) {
212 // It's small, use the size they gave us.
213 width = sourceWidth;
Romain Guy89911d22009-09-28 18:48:49 -0700214 height = sourceHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700215 }
216 }
217
218 // no intrinsic size --> use default size
219 int textureWidth = sIconTextureWidth;
220 int textureHeight = sIconTextureHeight;
221
222 final Bitmap bitmap = Bitmap.createBitmap(textureWidth, textureHeight,
223 Bitmap.Config.ARGB_8888);
224 final Canvas canvas = sCanvas;
225 canvas.setBitmap(bitmap);
226
227 final int left = (textureWidth-width) / 2;
228 final int top = (textureHeight-height) / 2;
229
230 if (false) {
231 // draw a big box for the icon for debugging
232 canvas.drawColor(sColors[sColorIndex]);
233 if (++sColorIndex >= sColors.length) sColorIndex = 0;
234 Paint debugPaint = new Paint();
235 debugPaint.setColor(0xffcccc00);
236 canvas.drawRect(left, top, left+width, top+height, debugPaint);
237 }
238
239 sOldBounds.set(icon.getBounds());
240 icon.setBounds(left, top, left+width, top+height);
241 icon.draw(canvas);
242 icon.setBounds(sOldBounds);
243
244 return bitmap;
245 }
246 }
247
Joe Onorato1291a8c2009-09-15 15:07:25 -0400248 static void drawSelectedAllAppsBitmap(Canvas dest, int destWidth, int destHeight, Bitmap src) {
249 synchronized (sCanvas) { // we share the statics :-(
250 if (sIconWidth == -1) {
251 // We can't have gotten to here without src being initialized, which
252 // comes from this file already. So just assert.
253 //initStatics(context);
254 throw new RuntimeException("Assertion failed: Utilities not initialized");
255 }
256
257 dest.drawColor(0, PorterDuff.Mode.CLEAR);
Mike Reedcdd11792009-10-29 17:27:55 -0400258
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;
264 dest.drawBitmap(mask, px + xy[0], py + xy[1], sGlowColorPaint);
Joe Onorato1291a8c2009-09-15 15:07:25 -0400265
266 mask.recycle();
267 }
268 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700269
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800270 /**
271 * Returns a Bitmap representing the thumbnail of the specified Bitmap.
272 * The size of the thumbnail is defined by the dimension
273 * android.R.dimen.launcher_application_icon_size.
274 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800275 * @param bitmap The bitmap to get a thumbnail of.
276 * @param context The application's context.
277 *
278 * @return A thumbnail for the specified bitmap or the bitmap itself if the
279 * thumbnail could not be created.
280 */
281 static Bitmap createBitmapThumbnail(Bitmap bitmap, Context context) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400282 synchronized (sCanvas) { // we share the statics :-(
283 if (sIconWidth == -1) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700284 initStatics(context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800285 }
286
Joe Onorato9c1289c2009-08-17 11:03:03 -0400287 int width = sIconWidth;
288 int height = sIconHeight;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800289
Joe Onorato9c1289c2009-08-17 11:03:03 -0400290 final int bitmapWidth = bitmap.getWidth();
291 final int bitmapHeight = bitmap.getHeight();
292
Romain Guy89911d22009-09-28 18:48:49 -0700293 if (width > 0 && height > 0) {
294 if (width < bitmapWidth || height < bitmapHeight) {
295 final float ratio = (float) bitmapWidth / bitmapHeight;
296
297 if (bitmapWidth > bitmapHeight) {
298 height = (int) (width / ratio);
299 } else if (bitmapHeight > bitmapWidth) {
300 width = (int) (height * ratio);
301 }
302
303 final Bitmap.Config c = (width == sIconWidth && height == sIconHeight) ?
304 bitmap.getConfig() : Bitmap.Config.ARGB_8888;
305 final Bitmap thumb = Bitmap.createBitmap(sIconWidth, sIconHeight, c);
306 final Canvas canvas = sCanvas;
307 final Paint paint = sPaint;
308 canvas.setBitmap(thumb);
309 paint.setDither(false);
310 paint.setFilterBitmap(true);
311 sBounds.set((sIconWidth - width) / 2, (sIconHeight - height) / 2, width, height);
312 sOldBounds.set(0, 0, bitmapWidth, bitmapHeight);
313 canvas.drawBitmap(bitmap, sOldBounds, sBounds, paint);
314 return thumb;
315 } else if (bitmapWidth < width || bitmapHeight < height) {
316 final Bitmap.Config c = Bitmap.Config.ARGB_8888;
317 final Bitmap thumb = Bitmap.createBitmap(sIconWidth, sIconHeight, c);
318 final Canvas canvas = sCanvas;
319 final Paint paint = sPaint;
320 canvas.setBitmap(thumb);
321 paint.setDither(false);
322 paint.setFilterBitmap(true);
323 canvas.drawBitmap(bitmap, (sIconWidth - bitmapWidth) / 2,
324 (sIconHeight - bitmapHeight) / 2, paint);
325 return thumb;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400326 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400327 }
328
329 return bitmap;
330 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800331 }
Joe Onoratobf15cb42009-08-07 14:33:40 -0700332
Joe Onorato6665c0f2009-09-02 15:27:24 -0700333 private static void initStatics(Context context) {
334 final Resources resources = context.getResources();
Joe Onorato1291a8c2009-09-15 15:07:25 -0400335 final DisplayMetrics metrics = resources.getDisplayMetrics();
336 final float density = metrics.density;
337
Joe Onorato6665c0f2009-09-02 15:27:24 -0700338 sIconWidth = sIconHeight = (int) resources.getDimension(android.R.dimen.app_icon_size);
339 sIconTextureWidth = sIconTextureHeight = roundToPow2(sIconWidth);
Joe Onorato1291a8c2009-09-15 15:07:25 -0400340
Mike Reedcdd11792009-10-29 17:27:55 -0400341 sBlurPaint.setMaskFilter(new BlurMaskFilter(4 * density, BlurMaskFilter.Blur.NORMAL));
Joe Onorato1291a8c2009-09-15 15:07:25 -0400342 sGlowColorPaint.setColor(0xffff9000);
Mike Reedcdd11792009-10-29 17:27:55 -0400343 sGlowColorPaint.setMaskFilter(TableMaskFilter.CreateClipTable(0, 64));
Joe Onorato6665c0f2009-09-02 15:27:24 -0700344 }
345
Joe Onoratobf15cb42009-08-07 14:33:40 -0700346 static class BubbleText {
347 private static final int MAX_LINES = 2;
348 private TextPaint mTextPaint;
Joe Onoratobf15cb42009-08-07 14:33:40 -0700349
350 private float mBubblePadding;
Joe Onoratobf15cb42009-08-07 14:33:40 -0700351 private RectF mBubbleRect = new RectF();
352
353 private float mTextWidth;
354 private int mLeading;
355 private int mFirstLineY;
356 private int mLineHeight;
357
358 private int mBitmapWidth;
359 private int mBitmapHeight;
360
361 BubbleText(Context context) {
362 final Resources resources = context.getResources();
363
364 final float scale = resources.getDisplayMetrics().density;
365
366 final float paddingLeft = 5.0f * scale;
367 final float paddingRight = 5.0f * scale;
368 final float cellWidth = resources.getDimension(R.dimen.workspace_cell_width);
369 final float bubbleWidth = cellWidth - paddingLeft - paddingRight;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700370 mBubblePadding = 3.0f * scale;
Joe Onoratobf15cb42009-08-07 14:33:40 -0700371
372 RectF bubbleRect = mBubbleRect;
373 bubbleRect.left = 0;
374 bubbleRect.top = 0;
375 bubbleRect.right = (int)(bubbleWidth+0.5f);
376
Joe Onoratobf15cb42009-08-07 14:33:40 -0700377 mTextWidth = bubbleWidth - mBubblePadding - mBubblePadding;
378
Romain Guy89911d22009-09-28 18:48:49 -0700379 Paint rectPaint = new Paint();
Joe Onoratoefabe002009-08-28 09:38:18 -0700380 rectPaint.setColor(0xff000000);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700381 rectPaint.setAntiAlias(true);
Joe Onoratobf15cb42009-08-07 14:33:40 -0700382
Joe Onorato1291a8c2009-09-15 15:07:25 -0400383 Log.d(TAG, "scale=" + scale + " textSize=" + (13*scale));
Joe Onoratoefabe002009-08-28 09:38:18 -0700384
Joe Onoratobf15cb42009-08-07 14:33:40 -0700385 TextPaint textPaint = mTextPaint = new TextPaint();
Joe Onoratoefabe002009-08-28 09:38:18 -0700386 textPaint.setTypeface(Typeface.DEFAULT);
387 textPaint.setTextSize(13*scale);
Joe Onoratobf15cb42009-08-07 14:33:40 -0700388 textPaint.setColor(0xffffffff);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700389 textPaint.setAntiAlias(true);
Joe Onorato9392a752009-09-15 17:13:09 -0400390 if (TEXT_BURN) {
391 textPaint.setShadowLayer(8, 0, 0, 0xff000000);
392 }
Joe Onoratobf15cb42009-08-07 14:33:40 -0700393
394 float ascent = -textPaint.ascent();
395 float descent = textPaint.descent();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700396 float leading = 0.0f;//(ascent+descent) * 0.1f;
Joe Onoratobf15cb42009-08-07 14:33:40 -0700397 mLeading = (int)(leading + 0.5f);
398 mFirstLineY = (int)(leading + ascent + 0.5f);
399 mLineHeight = (int)(leading + ascent + descent + 0.5f);
400
Joe Onoratobf15cb42009-08-07 14:33:40 -0700401 mBitmapWidth = roundToPow2((int)(mBubbleRect.width() + 0.5f));
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700402 mBitmapHeight = roundToPow2((int)((MAX_LINES * mLineHeight) + leading + 0.5f));
Joe Onoratobf15cb42009-08-07 14:33:40 -0700403
Joe Onoratofccc8df2009-10-01 14:30:16 -0700404 mBubbleRect.offsetTo((mBitmapWidth-mBubbleRect.width())/2, 0);
405
Joe Onorato1291a8c2009-09-15 15:07:25 -0400406 Log.d(TAG, "mBitmapWidth=" + mBitmapWidth + " mBitmapHeight="
Joe Onoratobf15cb42009-08-07 14:33:40 -0700407 + mBitmapHeight + " w=" + ((int)(mBubbleRect.width() + 0.5f))
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700408 + " h=" + ((int)((MAX_LINES * mLineHeight) + leading + 0.5f)));
Joe Onoratobf15cb42009-08-07 14:33:40 -0700409 }
410
411 /** You own the bitmap after this and you must call recycle on it. */
412 Bitmap createTextBitmap(String text) {
413 Bitmap b = Bitmap.createBitmap(mBitmapWidth, mBitmapHeight, Bitmap.Config.ARGB_8888);
414 Canvas c = new Canvas(b);
415
416 StaticLayout layout = new StaticLayout(text, mTextPaint, (int)mTextWidth,
417 Alignment.ALIGN_CENTER, 1, 0, true);
418 int lineCount = layout.getLineCount();
419 if (lineCount > MAX_LINES) {
420 lineCount = MAX_LINES;
421 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700422 //if (!TEXT_BURN && lineCount > 0) {
423 //RectF bubbleRect = mBubbleRect;
424 //bubbleRect.bottom = height(lineCount);
425 //c.drawRoundRect(bubbleRect, mCornerRadius, mCornerRadius, mRectPaint);
426 //}
Joe Onoratobf15cb42009-08-07 14:33:40 -0700427 for (int i=0; i<lineCount; i++) {
Joe Onoratofccc8df2009-10-01 14:30:16 -0700428 //int x = (int)((mBubbleRect.width() - layout.getLineMax(i)) / 2.0f);
429 //int y = mFirstLineY + (i * mLineHeight);
430 int x = (int)(mBubbleRect.left
431 + ((mBubbleRect.width() - layout.getLineMax(i)) / 2.0f));
Joe Onoratobf15cb42009-08-07 14:33:40 -0700432 int y = mFirstLineY + (i * mLineHeight);
433 c.drawText(text.substring(layout.getLineStart(i), layout.getLineEnd(i)),
434 x, y, mTextPaint);
435 }
436
437 return b;
438 }
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700439
440 private int height(int lineCount) {
441 return (int)((lineCount * mLineHeight) + mLeading + mLeading + 0.0f);
442 }
443
444 int getBubbleWidth() {
445 return (int)(mBubbleRect.width() + 0.5f);
446 }
447
448 int getMaxBubbleHeight() {
449 return height(MAX_LINES);
450 }
451
452 int getBitmapWidth() {
453 return mBitmapWidth;
454 }
455
456 int getBitmapHeight() {
457 return mBitmapHeight;
458 }
Joe Onoratobf15cb42009-08-07 14:33:40 -0700459 }
460
461 /** Only works for positive numbers. */
462 static int roundToPow2(int n) {
463 int orig = n;
464 n >>= 1;
465 int mask = 0x8000000;
466 while (mask != 0 && (n & mask) == 0) {
467 mask >>= 1;
468 }
469 while (mask != 0) {
470 n |= mask;
471 mask >>= 1;
472 }
473 n += 1;
474 if (n != orig) {
475 n <<= 1;
476 }
477 return n;
478 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800479}