跟随屏幕大小自动调整bitmap大小(横竖屏拍摄的照片均可)
16lz
2021-01-22
在一个项目中用到了全屏显示bitmap,由前面的从图库或是直接拍照获得的图片是经过压缩的,这里就说一下怎么调整bitmap的样式大小
public static Bitmap resizeImage(Bitmap originalBitmap) {
int width = originalBitmap.getWidth();
int height = originalBitmap.getHeight();
//screenWidth为手机屏幕的宽
//screenHeight为手机屏幕的高
float scanleWidth = 0;
float scanleHeight = 0;
if (width > height) {
//横屏的图片
if (width > screenWidth / 2) {
scanleWidth = (float) ((float) screenWidth / (float) width );
scanleHeight = scanleWidth;
} else {
scanleWidth = (float) screenWidth / (float) 2 / (float) width;
scanleHeight = scanleWidth;
}
}
if (width <= height) {
//竖屏的图片
if (width >= screenWidth / 2) {
scanleWidth = (float) ((float) screenWidth / (float) width);
scanleHeight = scanleWidth;
} else {
scanleWidth = (float) screenWidth / (float) 2 / (float) width;
scanleHeight = scanleWidth;
}
}
//操作图片使用的matrix对象 Matrix
Matrix matrix = new Matrix();
// 缩放图片动作
matrix.postScale(scanleWidth, scanleHeight);
//旋转图片 动作
// matrix.postRotate(45);
// 创建新的图片Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(originalBitmap, 0, 0, width, height, matrix, true);
// 用完了记得回收
// resizedBitmap.recycle();
return resizedBitmap;
}
更多相关文章
- Android多线程下载远程图片【转】
- Android界面的.9.png图片显示出错,怎么回事啊?
- 关于Android4.0之上的ListView显示从网络上获取图片和文字
- Android 图片添加水印图片或者文字
- 拍照后获取不了图片!
- Android——播放器和图片轮播
- Android实现非本地图片的点击效果
- Android自定义Toast带图片的
- Android 自定义控件 改变图片颜色来实现类似selector点击更改颜