在一个项目中用到了全屏显示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; }