01 |
public static Drawable
resizeImage(Bitmap bitmap, int w, int h)
{ |
02 |
03 |
//
load the origial Bitmap |
04 |
Bitmap
BitmapOrg = bitmap; |
05 |
06 |
int width
= BitmapOrg.getWidth(); |
07 |
int height
= BitmapOrg.getHeight(); |
08 |
int newWidth
= w; |
09 |
int newHeight
= h; |
10 |
11 |
//
calculate the scale |
12 |
float scaleWidth
= ((float)
newWidth) / width; |
13 |
float scaleHeight
= ((float)
newHeight) / height; |
14 |
15 |
//
create a matrix for the manipulation |
16 |
Matrix
matrix = new Matrix(); |
17 |
//
resize the Bitmap |
18 |
matrix.postScale(scaleWidth,
scaleHeight); |
19 |
//
if you want to rotate the Bitmap |
20 |
//
matrix.postRotate(45); |
21 |
22 |
//
recreate the new Bitmap |
23 |
Bitmap
resizedBitmap = Bitmap.createBitmap(BitmapOrg, 0, 0,
width, |
24 |
height,
matrix, true); |
25 |
26 |
//
make a Drawable from Bitmap to allow to set the Bitmap |
27 |
//
to the ImageView, ImageButton or what ever |
28 |
return new BitmapDrawable(resizedBitmap); |
29 |
30 |
} |

本文介绍了一种使用Android平台下的Bitmap对象进行图片缩放的方法。通过计算目标宽度和高度的比例来调整图片尺寸,并利用Matrix对象完成缩放操作。最终将处理后的Bitmap转换为Drawable对象以便在应用程序中使用。
820

被折叠的 条评论
为什么被折叠?



