在一個項目中用到了全屏顯示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; }