Android中缩放图片的一个方法

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


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, 00, 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         }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值