下面的代码调整位图的大小并保持宽高比。 我想知道是否有更有效的调整大小的方法,因为我知道我正在编写 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;
}
最佳答案
使用方法Bitmap.createScaledBitmap()
:)
关于java - 调整位图大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9583757/