缩放位图保持纵横比并适应宽度和高度

Scale Bitmap maintaining aspect ratio and fitting both width and height

我想缩放保持纵横比但适合所需尺寸的位图。 This answer 缩放位图并保持纵横比,但保留一些空白 space 除非图像是完美的正方形。我需要同时填充宽度和高度,就像 ImageView.

FIT_XY ScaleType 属性

基于Streets of Boston's answer,我制作了这种方法,可以缩放 returns 任何位图到所需的宽度和高度,适合两个尺寸(没有空白 space!)。它会自动适应更多水平或更多垂直图像。

public Bitmap resizeBitmapFitXY(int width, int height, Bitmap bitmap){
    Bitmap background = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    float originalWidth = bitmap.getWidth(), originalHeight = bitmap.getHeight();
    Canvas canvas = new Canvas(background);
    float scale, xTranslation = 0.0f, yTranslation = 0.0f;
    if (originalWidth > originalHeight) {
        scale = height/originalHeight;
        xTranslation = (width - originalWidth * scale)/2.0f;
    }
    else {
        scale = width / originalWidth;
        yTranslation = (height - originalHeight * scale)/2.0f;
    }
    Matrix transformation = new Matrix();
    transformation.postTranslate(xTranslation, yTranslation);
    transformation.preScale(scale, scale);
    Paint paint = new Paint();
    paint.setFilterBitmap(true);
    canvas.drawBitmap(bitmap, transformation, paint);
    return background;
}

这将缩放图像以适应正方形,其余区域没有任何纯色填充,也没有裁剪。如果图像不是正方形,那么会有透明区域,图像的纵横比将与原始图像相同。 我已经用 potrait 和 lanscape 图像对其进行了测试

import android.graphics.*;

 public static Bitmap scalePreserveRatio(Bitmap imageToScale, int destinationWidth,
        int destinationHeight) {
        if (destinationHeight > 0 && destinationWidth > 0 && imageToScale != null) {
            int width = imageToScale.getWidth();
            int height = imageToScale.getHeight();

            //Calculate the max changing amount and decide which dimension to use
            float widthRatio = (float) destinationWidth / (float) width;
            float heightRatio = (float) destinationHeight / (float) height;

            //Use the ratio that will fit the image into the desired sizes
            int finalWidth = (int)Math.floor(width * widthRatio);
            int finalHeight = (int)Math.floor(height * widthRatio);
            if (finalWidth > destinationWidth || finalHeight > destinationHeight) {
                finalWidth = (int)Math.floor(width * heightRatio);
                finalHeight = (int)Math.floor(height * heightRatio);
            }

            //Scale given bitmap to fit into the desired area
            imageToScale = Bitmap.createScaledBitmap(imageToScale, finalWidth, finalHeight, true);

            //Created a bitmap with desired sizes
            Bitmap scaledImage = Bitmap.createBitmap(destinationWidth, destinationHeight, Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(scaledImage);

            //Draw background color
            Paint paint = new Paint();
            paint.setColor(Color.TRANSPARENT);
            paint.setStyle(Paint.Style.FILL);
            canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), paint);

            //Calculate the ratios and decide which part will have empty areas (width or height)
            float ratioBitmap = (float)finalWidth / (float)finalHeight;
            float destinationRatio = (float) destinationWidth / (float) destinationHeight;
            float left = ratioBitmap >= destinationRatio ? 0 : (float)(destinationWidth - finalWidth) / 2;
            float top = ratioBitmap < destinationRatio ? 0: (float)(destinationHeight - finalHeight) / 2;
            canvas.drawBitmap(imageToScale, left, top, null);

            return scaledImage;
        } else {
            return imageToScale;
        }
    }

来自