下面的代碼調整位圖大小並保持寬高比。 我想知道是否有更有效的調整大小的方法,因爲我的想法是我正在編寫android API中已有的代碼。調整位圖大小
private Bitmap resizeImage(Bitmap bitmap, int newSize){
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int newWidth = 0;
int newHeight = 0;
if(width > height){
newWidth = newSize;
newHeight = (newSize * height)/width;
} else if(width < height){
newHeight = newSize;
newWidth = (newSize * width)/height;
} else if (width == height){
newHeight = newSize;
newWidth = newSize;
}
float scaleWidth = ((float) newWidth)/width;
float scaleHeight = ((float) newHeight)/height;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,
width, height, matrix, true);
return resizedBitmap;
}