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