android - 调整无宽高比的图像大小

标签 android image image-resizing

我想将我的位图图像调整为 512px,512px。我使用下面的代码,但是当我的图像调整大小时它非常糟糕并且纵横比受到影响。我如何在没有纵横比的情况下调整我的图像?第二张图像用电脑调整到 512 像素。 enter image description here enter image description here

public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
        int width = bm.getWidth();
        int height = bm.getHeight();
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
        return resizedBitmap;
    }

最佳答案

您可以使用它调整位图的大小。

Bitmap yourBitmap;
Bitmap resized = Bitmap.createScaledBitmap(yourBitmap, newWidth, newHeight, true);

或:

resized = Bitmap.createScaledBitmap(yourBitmap,(int)(yourBitmap.getWidth()*0.8), (int)(yourBitmap.getHeight()*0.8), true);

关于android - 调整无宽高比的图像大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35275579/

相关文章:

android - PhoneGap 3 插件 : exec() call to unknown plugin "..."

css - 使用 CSS 在导航栏中悬停时更改 SVG 图像

html - 最大化 img 的大小

javascript - 通过客户端从服务器检索图像

iOS 图像调整大小 : is there a better interpolation than the kCGInterpolationHigh?

java - Android振动强度和损坏

android - 如何在android中生成带有图像的Pdf文件?

android - Activity 和 Service 的生命周期

firebase - 从第一张图片生成的缩略图在 Firebase 存储中为其他人复制

android - 从 url 获取图像并在 ImageView 中显示时调整图像大小