我正在尝试缩小位图,同时保持相同的宽高比。 以下代码工作正常,但我知道它不是最好的方法。 我绝对不喜欢创建一个持有人位图,然后另一个也可以缩小它。 关于如何改进这一点的任何建议都表示赞赏。
//width and height are passed in as params
Bitmap viewBM = view.getBitMap();
float aspectRatio = (float) width/height;
int newWidth= viewBM.getWidth();
int newHeight = viewBM.getHeight();
if (newHeight > newWidth) {
newHeight= (int)Math.round(newWidth * aspectRatio);
} else {
newWidth= (int)Math.round(newHeight * aspectRatio);
}
Bitmap holderBitmap = Bitmap.createBitmap(viewBM, 0, 0, newWidth, newHeight);
final Bitmap finalBitmap = Bitmap.createScaledBitmap(holderBitmap, width, height, true);