减小位图的大小在Android中一些特定的像素位图、像素、大小、Android

2023-09-12 09:18:16 作者:忍住眼泪

我想减少我的位图图像尺寸最大的640px。比如我有尺寸的位图图像1200 * 1200像素。我怎样才能将其降低到640px。

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.

推荐答案

如果您通过位图的宽度和高度,然后使用下面的功能。

If you pass bitmap width and height then use below function.

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

如果你想要的位图的比例相同,减少位图尺寸

。然后通过你的最大尺寸的位图。 您可以使用此功能

if you want bitmap ratio same and reduce bitmap size. then pass your maximum size bitmap. you can use this function

public Bitmap getResizedBitmap(Bitmap image, int maxSize) {
        int width = image.getWidth();
        int height = image.getHeight();

        float bitmapRatio = (float)width / (float) height;
        if (bitmapRatio > 0) {
            width = maxSize;
            height = (int) (width / bitmapRatio);
        } else {
            height = maxSize;
            width = (int) (height * bitmapRatio);
        }
        return Bitmap.createScaledBitmap(image, width, height, true);
}