blob: 23738974c35b21552957fdc731324fdc27c6b51e [file] [log] [blame]
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001/*
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
17package com.android.launcher;
18
19import android.graphics.drawable.Drawable;
20import android.graphics.drawable.PaintDrawable;
21import android.graphics.drawable.BitmapDrawable;
22import android.graphics.Bitmap;
23import android.graphics.PixelFormat;
24import android.graphics.Canvas;
25import android.graphics.PaintFlagsDrawFilter;
26import android.graphics.Paint;
27import android.graphics.Rect;
28import android.content.res.Resources;
29import android.content.Context;
30
31/**
32 * Various utilities shared amongst the Launcher's classes.
33 */
34final class Utilities {
35 private static int sIconWidth = -1;
36 private static int sIconHeight = -1;
37
38 private static final Paint sPaint = new Paint();
39 private static final Rect sBounds = new Rect();
40 private static final Rect sOldBounds = new Rect();
41 private static Canvas sCanvas = new Canvas();
42
43 static {
44 sCanvas.setDrawFilter(new PaintFlagsDrawFilter(Paint.DITHER_FLAG,
45 Paint.FILTER_BITMAP_FLAG));
46 }
47
48 static Bitmap centerToFit(Bitmap bitmap, int width, int height, Context context) {
49 final int bitmapWidth = bitmap.getWidth();
50 final int bitmapHeight = bitmap.getHeight();
51
52 if (bitmapWidth < width || bitmapHeight < height) {
53 int color = context.getResources().getColor(R.color.window_background);
54
55 Bitmap centered = Bitmap.createBitmap(bitmapWidth < width ? width : bitmapWidth,
56 bitmapHeight < height ? height : bitmapHeight, Bitmap.Config.RGB_565);
57 Canvas canvas = new Canvas(centered);
58 canvas.drawColor(color);
59 canvas.drawBitmap(bitmap, (width - bitmapWidth) / 2.0f, (height - bitmapHeight) / 2.0f,
60 null);
61
62 bitmap = centered;
63 }
64
65 return bitmap;
66 }
67
68 /**
69 * Returns a Drawable representing the thumbnail of the specified Drawable.
70 * The size of the thumbnail is defined by the dimension
71 * android.R.dimen.launcher_application_icon_size.
72 *
73 * This method is not thread-safe and should be invoked on the UI thread only.
74 *
75 * @param icon The icon to get a thumbnail of.
76 * @param context The application's context.
77 *
78 * @return A thumbnail for the specified icon or the icon itself if the
79 * thumbnail could not be created.
80 */
81 static Drawable createIconThumbnail(Drawable icon, Context context) {
82 if (sIconWidth == -1) {
83 final Resources resources = context.getResources();
84 sIconWidth = sIconHeight = (int) resources.getDimension(
85 android.R.dimen.app_icon_size);
86 }
87
88 int width = sIconWidth;
89 int height = sIconHeight;
90
91 final int iconWidth = icon.getIntrinsicWidth();
92 final int iconHeight = icon.getIntrinsicHeight();
93
94 if (icon instanceof PaintDrawable) {
95 PaintDrawable painter = (PaintDrawable) icon;
96 painter.setIntrinsicWidth(width);
97 painter.setIntrinsicHeight(height);
98 }
99
100 if (width > 0 && height > 0 && (width < iconWidth || height < iconHeight)) {
101 final float ratio = (float) iconWidth / iconHeight;
102
103 if (iconWidth > iconHeight) {
104 height = (int) (width / ratio);
105 } else if (iconHeight > iconWidth) {
106 width = (int) (height * ratio);
107 }
108
109 final Bitmap.Config c = icon.getOpacity() != PixelFormat.OPAQUE ?
110 Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
111 final Bitmap thumb = Bitmap.createBitmap(sIconWidth, sIconHeight, c);
112 final Canvas canvas = sCanvas;
113 canvas.setBitmap(thumb);
114 // Copy the old bounds to restore them later
115 // If we were to do oldBounds = icon.getBounds(),
116 // the call to setBounds() that follows would
117 // change the same instance and we would lose the
118 // old bounds
119 sOldBounds.set(icon.getBounds());
120 icon.setBounds((sIconWidth - width) / 2, (sIconHeight - height) / 2, width, height);
121 icon.draw(canvas);
122 icon.setBounds(sOldBounds);
123 icon = new BitmapDrawable(thumb);
124 }
125
126 return icon;
127 }
128
129 /**
130 * Returns a Bitmap representing the thumbnail of the specified Bitmap.
131 * The size of the thumbnail is defined by the dimension
132 * android.R.dimen.launcher_application_icon_size.
133 *
134 * This method is not thread-safe and should be invoked on the UI thread only.
135 *
136 * @param bitmap The bitmap to get a thumbnail of.
137 * @param context The application's context.
138 *
139 * @return A thumbnail for the specified bitmap or the bitmap itself if the
140 * thumbnail could not be created.
141 */
142 static Bitmap createBitmapThumbnail(Bitmap bitmap, Context context) {
143 if (sIconWidth == -1) {
144 final Resources resources = context.getResources();
145 sIconWidth = sIconHeight = (int) resources.getDimension(
146 android.R.dimen.app_icon_size);
147 }
148
149 int width = sIconWidth;
150 int height = sIconHeight;
151
152 final int bitmapWidth = bitmap.getWidth();
153 final int bitmapHeight = bitmap.getHeight();
154
155 if (width > 0 && height > 0 && (width < bitmapWidth || height < bitmapHeight)) {
156 final float ratio = (float) bitmapWidth / bitmapHeight;
157
158 if (bitmapWidth > bitmapHeight) {
159 height = (int) (width / ratio);
160 } else if (bitmapHeight > bitmapWidth) {
161 width = (int) (height * ratio);
162 }
163
164 final Bitmap.Config c = (width == sIconWidth && height == sIconHeight) ?
165 bitmap.getConfig() : Bitmap.Config.ARGB_8888;
166 final Bitmap thumb = Bitmap.createBitmap(sIconWidth, sIconHeight, c);
167 final Canvas canvas = sCanvas;
168 final Paint paint = sPaint;
169 canvas.setBitmap(thumb);
170 paint.setDither(false);
171 paint.setFilterBitmap(true);
172 sBounds.set((sIconWidth - width) / 2, (sIconHeight - height) / 2, width, height);
173 sOldBounds.set(0, 0, bitmapWidth, bitmapHeight);
174 canvas.drawBitmap(bitmap, sOldBounds, sBounds, paint);
175 return thumb;
176 }
177
178 return bitmap;
179 }
180}