1 BitmapDrawable icon= oldIcon;
2 final Bitmap.Config c = icon.getOpacity() != PixelFormat.OPAQUE ?
3 Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
4 int sIconWidth,sIconHeight ;
5 sIconWidth = sIconHeight = (int) context.getResources().getDimension(R.dimen.img_width);
6
7 int width = sIconWidth;
8 int height = sIconHeight;
9
10 Bitmap bmp=BitmapFactory.decodeResource(context.getResources(), R.drawable.tb_25);
11 float scale = 1.0f;
12 int iconWidth = icon.getIntrinsicWidth();
13 int iconHeight = icon.getIntrinsicHeight();
14
15 // Log.e("12", "BitmapDrawable:iconwidth"+iconWidth);
16 // Log.e("12", "BitmapDrawable:iconHeight"+iconHeight);
17 // Log.e("12", "BitmapDrawable:width"+width);
18 // Log.e("12", "BitmapDrawable:height"+height);
19 final Canvas canvas = new Canvas();
20 Rect sOldBounds = new Rect();
21 if (width > 0 && height > 0) {
22 // if (width < iconWidth || height < iconHeight || scale != 1.0f) {
23
24 final float ratio = (float) iconWidth / iconHeight;
25
26 if (iconWidth > iconHeight) {
27 height = (int) (width / ratio);
28 } else if (iconHeight > iconWidth) {
29 width = (int) (height * ratio);
30 }
31
32 final Bitmap thumb = Bitmap.createBitmap(sIconWidth, sIconHeight, c);
33 canvas.setBitmap(thumb);
34 sOldBounds.set(icon.getBounds());
35 final int x = (sIconWidth - width) / 2;
36 final int y = (sIconHeight - height) / 2;
37 icon.setBounds(x, y, x + width, y + height);
38
39 canvas.drawBitmap(bmp, new Matrix(), null);
40 icon.draw(canvas);
41 icon.setBounds(sOldBounds);
42 icon = new BitmapDrawable(context.getResources(), thumb);
43
44 // } else if (iconWidth < width && iconHeight < height) {
45 //
46 // final Bitmap.Config c1 = Bitmap.Config.ARGB_8888;
47 // final Bitmap thumb = Bitmap.createBitmap(sIconWidth, sIconHeight, c1);
48 // canvas.setBitmap(thumb);
49 // sOldBounds.set(icon.getBounds());
50 // final int x = (width - iconWidth) / 2;
51 // final int y = (height - iconHeight) / 2;
52 // icon.setBounds(x, y, x + iconWidth, y + iconHeight);
53 // canvas.drawBitmap(bmp, new Matrix(), null);
54 // icon.draw(canvas);
55 // icon.setBounds(sOldBounds);
56 // icon = new BitmapDrawable(context.getResources(), thumb);
57 // Log.e("12", "iconWidth < widthBitmapDrawable:iconwidth"+iconWidth);
58 // Log.e("12", "BitmapDrawable:iconHeight"+iconHeight);
59 // Log.e("12", "BitmapDrawable:width"+width);
60 // Log.e("12", "BitmapDrawable:height"+height);
61 // }
62 }
63 return icon;
64 }