createIconThumbnail

来源:互联网 发布:零基础想学美工 编辑:程序博客网 时间:2024/06/08 18:06
/** * Returns a Drawable representing the thumbnail of the specified Drawable. * The size of the thumbnail is defined by the dimension * android.R.dimen.launcher_application_icon_size. *  * This method is not thread-safe and should be invoked on the UI thread * only. *  * @param icon *            The icon to get a thumbnail of. * @param context *            The application's context. *  * @return A thumbnail for the specified icon or the icon itself if the *         thumbnail could not be created. */static Drawable createIconThumbnail(Drawable icon, Context context) {if (sIconWidth == -1) {final Resources resources = context.getResources();sIconWidth = sIconHeight = (int) resources.getDimension(android.R.dimen.app_icon_size);}// 强制改成96sIconWidth = sIconHeight = 96;int width = sIconWidth;int height = sIconHeight;float scale = 1.0f;if (icon instanceof PaintDrawable) {PaintDrawable painter = (PaintDrawable) icon;painter.setIntrinsicWidth(width);painter.setIntrinsicHeight(height);} else if (icon instanceof BitmapDrawable) {// Ensure the bitmap has a density.BitmapDrawable bitmapDrawable = (BitmapDrawable) icon;Bitmap bitmap = bitmapDrawable.getBitmap();if (bitmap.getDensity() == Bitmap.DENSITY_NONE) {bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics());}}int iconWidth = icon.getIntrinsicWidth();int iconHeight = icon.getIntrinsicHeight();if (width > 0 && height > 0) {if (width < iconWidth || height < iconHeight || scale != 1.0f) {final float ratio = (float) iconWidth / iconHeight;// It's too big, scale it down.if (iconWidth > iconHeight) {height = (int) (width / ratio);} else if (iconHeight > iconWidth) {width = (int) (height * ratio);}final Bitmap.Config c = icon.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888: Bitmap.Config.RGB_565;final Bitmap thumb = Bitmap.createBitmap(sIconWidth,sIconHeight, c);final Canvas canvas = sCanvas;canvas.setBitmap(thumb);// Copy the old bounds to restore them later// If we were to do oldBounds = icon.getBounds(),// the call to setBounds() that follows would// change the same instance and we would lose the// old boundssOldBounds.set(icon.getBounds());final int x = (sIconWidth - width) / 2;final int y = (sIconHeight - height) / 2;icon.setBounds(x, y, x + width, y + height);icon.draw(canvas);icon.setBounds(sOldBounds);icon = new FastBitmapDrawable(thumb);} else if (iconWidth < width && iconHeight < height) {final Bitmap.Config c = Bitmap.Config.ARGB_8888;final Bitmap thumb = Bitmap.createBitmap(sIconWidth,sIconHeight, c);final Canvas canvas = sCanvas;canvas.setBitmap(thumb);sOldBounds.set(icon.getBounds());/* * final int x = (width - * iconWidth) / 2; final int y = * (height - iconHeight) / 2; * icon.setBounds(x, y, x + * iconWidth, y + iconHeight); * icon.draw(canvas); * icon.setBounds(sOldBounds); * icon = new * FastBitmapDrawable(thumb); */final float ratio;if (iconWidth < iconHeight) {ratio = (float) height / iconHeight;width = (int) (ratio * iconWidth);} else {ratio = (float) width / iconWidth;height = (int) (ratio * iconHeight);}final int x1 = (sIconWidth - width) / 2;final int y1 = (sIconHeight - height) / 2;icon.setBounds(x1, y1, x1 + width, y1 + height);icon.draw(canvas);icon.setBounds(sOldBounds);icon = new FastBitmapDrawable(thumb);Log.e("createIconThumbnail", "放大,sIconWidth:" + sIconWidth);}}return icon;}

原创粉丝点击