如何在Android中调整位图大小?

398

我有一个位图,是从我的远程数据库中取出的Base64字符串生成的(encodedImage表示使用Base64表示的图像字符串):

profileImage = (ImageView)findViewById(R.id.profileImage);

byte[] imageAsBytes=null;
try {
    imageAsBytes = Base64.decode(encodedImage.getBytes());
} catch (IOException e) {e.printStackTrace();}

profileImage.setImageBitmap(
    BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length)
);

profileImage是我的ImageView

好的,但在将其显示在我的布局ImageView之前,我必须调整此图像的大小。我需要将其调整为120x120。

有人可以告诉我如何调整大小的代码吗?

我找到的示例无法应用于基于base64字符串获取的位图。


可能是在Android中调整位图大小的重复问题。 - Sagar Pilkhwal
@SagarPilkhwal 这个问题先被问到了。 - 김선달
21个回答

0
* For resize bitmap with width and height ratio.    

public static Bitmap getResizedBitmap(Bitmap image, int maxSize) {
            int width = image.getWidth();
            int height = image.getHeight();
    
            float bitmapRatio = (float) width / (float) height;
            if (bitmapRatio > 1) {
                width = maxSize;
                height = (int) (width / bitmapRatio);
            } else {
                height = maxSize;
                width = (int) (height * bitmapRatio);
            }
            return Bitmap.createScaledBitmap(image, width, height, true);
        }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接