将位图的大小减小到 Android 中的某个指定像素 [英] Reduce size of Bitmap to some specified pixel in Android

查看:23
本文介绍了将位图的大小减小到 Android 中的某个指定像素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将我的位图图像大小减小到最大 640 像素.例如,我有大小为 1200 x 1200 像素的位图图像.. 如何将其减小到 640 像素.

I would like to reduce My Bitmap image size to maximum of 640px. For example I have Bitmap image of size 1200 x 1200 px .. How can I reduce it to 640px.

推荐答案

如果传递位图 widthheight 然后使用:

If you pass bitmap width and height then use:

public Bitmap getResizedBitmap(Bitmap image, int bitmapWidth, int bitmapHeight) {
    return Bitmap.createScaledBitmap(image, bitmapWidth, bitmapHeight, true);
}

如果您想保持位图比例不变,但将其减少到最大边长,请使用:

If you want to keep the bitmap ratio the same, but reduce it to a maximum side length, use:

public 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);
}

这篇关于将位图的大小减小到 Android 中的某个指定像素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆