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

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

问题描述

我想将我的位图图片大小减小到最大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.

推荐答案

位图 width height ,然后使用:

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中将Bitmap的大小减小到某个指定的像素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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