android缩放图片
16lz
2021-01-23
public static Bitmap scaleImg(Bitmap bm, int newWidth, int newHeight) {
// 获得图片的宽高
int width = bm.getWidth();
int height = bm.getHeight();
// 设置想要的大小
int newWidth1 = newWidth;
int newHeight1 = newHeight;
// 计算缩放比例
float scaleWidth = ((float) newWidth1) / width;
float scaleHeight = ((float) newHeight1) / height;
// 取得想要缩放的matrix参数
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
// 得到新的图片
Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix,
true);
if(width != newWidth && height != newHeight){
freeBitmap(bm);
}
return newbm;
}
// 获得图片的宽高
int width = bm.getWidth();
int height = bm.getHeight();
// 设置想要的大小
int newWidth1 = newWidth;
int newHeight1 = newHeight;
// 计算缩放比例
float scaleWidth = ((float) newWidth1) / width;
float scaleHeight = ((float) newHeight1) / height;
// 取得想要缩放的matrix参数
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
// 得到新的图片
Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix,
true);
if(width != newWidth && height != newHeight){
freeBitmap(bm);
}
return newbm;
}
更多相关文章
- Android 实现局部图片滑动指引效果
- Android学习教程之九宫格图片展示(13)
- Android 7.0调用相机拍照并获取图片路径
- 实现Android的图片画廊控件ScrollGalleryView
- Android相机相册的调用,图片的存储删除
- Android实用图片显示控件photoview
- android原生分享图片失败的问题
- Android TextView带背景图片和自定义边框