blob: 33b084b16bc230dd41f3098ddf0b255c6a9c90f6 [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
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;
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();
Mitsuru Oshima583ed3b2009-05-12 19:19:10 -070084 sIconWidth = sIconHeight = (int) resources.getDimension(android.R.dimen.app_icon_size);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080085 }
86
87 int width = sIconWidth;
88 int height = sIconHeight;
89
Mitsuru Oshima583ed3b2009-05-12 19:19:10 -070090 float scale = 1.0f;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080091 if (icon instanceof PaintDrawable) {
92 PaintDrawable painter = (PaintDrawable) icon;
93 painter.setIntrinsicWidth(width);
94 painter.setIntrinsicHeight(height);
Mitsuru Oshima583ed3b2009-05-12 19:19:10 -070095 } else if (icon instanceof BitmapDrawable) {
96 float displayDensity = context.getResources().getDisplayMetrics().density;
97 BitmapDrawable bitmapDrawable = (BitmapDrawable) icon;
98 Bitmap bitmap = bitmapDrawable.getBitmap();
99 float iconDensity = bitmap.getDensityScale();
100 scale = displayDensity / iconDensity;
101
102 // Scale the bitmap to the screen density size if it's not loaded at the same density.
103 if (scale != 1.0f) {
104 icon = bitmapDrawable = new BitmapDrawable(bitmap);
105 bitmapDrawable.setDensityScale(scale);
106 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800107 }
Mitsuru Oshima583ed3b2009-05-12 19:19:10 -0700108 int iconWidth = icon.getIntrinsicWidth();
109 int iconHeight = icon.getIntrinsicHeight();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110
111 if (width > 0 && height > 0) {
Mitsuru Oshima583ed3b2009-05-12 19:19:10 -0700112 if (width < iconWidth || height < iconHeight || scale != 1.0f) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800113 final float ratio = (float) iconWidth / iconHeight;
114
115 if (iconWidth > iconHeight) {
116 height = (int) (width / ratio);
117 } else if (iconHeight > iconWidth) {
118 width = (int) (height * ratio);
119 }
120
121 final Bitmap.Config c = icon.getOpacity() != PixelFormat.OPAQUE ?
122 Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
123 final Bitmap thumb = Bitmap.createBitmap(sIconWidth, sIconHeight, c);
124 final Canvas canvas = sCanvas;
125 canvas.setBitmap(thumb);
126 // Copy the old bounds to restore them later
127 // If we were to do oldBounds = icon.getBounds(),
128 // the call to setBounds() that follows would
129 // change the same instance and we would lose the
130 // old bounds
131 sOldBounds.set(icon.getBounds());
132 final int x = (sIconWidth - width) / 2;
133 final int y = (sIconHeight - height) / 2;
134 icon.setBounds(x, y, x + width, y + height);
135 icon.draw(canvas);
136 icon.setBounds(sOldBounds);
137 icon = new FastBitmapDrawable(thumb);
138 } else if (iconWidth < width && iconHeight < height) {
139 final Bitmap.Config c = Bitmap.Config.ARGB_8888;
140 final Bitmap thumb = Bitmap.createBitmap(sIconWidth, sIconHeight, c);
141 final Canvas canvas = sCanvas;
142 canvas.setBitmap(thumb);
143 sOldBounds.set(icon.getBounds());
144 final int x = (width - iconWidth) / 2;
145 final int y = (height - iconHeight) / 2;
146 icon.setBounds(x, y, x + iconWidth, y + iconHeight);
147 icon.draw(canvas);
148 icon.setBounds(sOldBounds);
149 icon = new FastBitmapDrawable(thumb);
150 }
151 }
152
153 return icon;
154 }
155
156 /**
157 * Returns a Bitmap representing the thumbnail of the specified Bitmap.
158 * The size of the thumbnail is defined by the dimension
159 * android.R.dimen.launcher_application_icon_size.
160 *
161 * This method is not thread-safe and should be invoked on the UI thread only.
162 *
163 * @param bitmap The bitmap to get a thumbnail of.
164 * @param context The application's context.
165 *
166 * @return A thumbnail for the specified bitmap or the bitmap itself if the
167 * thumbnail could not be created.
168 */
169 static Bitmap createBitmapThumbnail(Bitmap bitmap, Context context) {
170 if (sIconWidth == -1) {
171 final Resources resources = context.getResources();
172 sIconWidth = sIconHeight = (int) resources.getDimension(
173 android.R.dimen.app_icon_size);
174 }
175
176 int width = sIconWidth;
177 int height = sIconHeight;
178
179 final int bitmapWidth = bitmap.getWidth();
180 final int bitmapHeight = bitmap.getHeight();
181
182 if (width > 0 && height > 0 && (width < bitmapWidth || height < bitmapHeight)) {
183 final float ratio = (float) bitmapWidth / bitmapHeight;
184
185 if (bitmapWidth > bitmapHeight) {
186 height = (int) (width / ratio);
187 } else if (bitmapHeight > bitmapWidth) {
188 width = (int) (height * ratio);
189 }
190
191 final Bitmap.Config c = (width == sIconWidth && height == sIconHeight) ?
192 bitmap.getConfig() : Bitmap.Config.ARGB_8888;
193 final Bitmap thumb = Bitmap.createBitmap(sIconWidth, sIconHeight, c);
194 final Canvas canvas = sCanvas;
195 final Paint paint = sPaint;
196 canvas.setBitmap(thumb);
197 paint.setDither(false);
198 paint.setFilterBitmap(true);
199 sBounds.set((sIconWidth - width) / 2, (sIconHeight - height) / 2, width, height);
200 sOldBounds.set(0, 0, bitmapWidth, bitmapHeight);
201 canvas.drawBitmap(bitmap, sOldBounds, sBounds, paint);
202 return thumb;
203 }
204
205 return bitmap;
206 }
207}