我创建了一些位图,它们的尺寸不是固定的。有时是120x60,129x800,851x784等等,没有具体的值...... 我想将这些位图调整大小为512x512,但不改变原始图像的长宽比并且不裁剪。新的图像必须有512x512的画布,原始图像必须在中心位置而没有任何裁剪。
我使用以下函数调整我的位图大小,但效果非常糟糕,因为它同时适用于图像的X和Y轴。我不希望图像同时适应X和Y轴,只要适应其中一个轴并保持其长宽比即可。
public Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight) {
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// CREATE A MATRIX FOR THE MANIPULATION
Matrix matrix = new Matrix();
// RESIZE THE BIT MAP
matrix.postScale(scaleWidth, scaleHeight);
// "RECREATE" THE NEW BITMAP
Bitmap resizedBitmap = Bitmap.createBitmap(
bm, 0, 0, width, height, matrix, false);
bm.recycle();
return resizedBitmap;
}
我拥有的是: 我想要的是: