保持调整大小的位图的宽高比

时间:2018-02-19 17:11:37

标签: java android image-processing

我实现了这个来调整位图的大小并保留纵横比,但是我想知道是否有办法实现这一点,使得所有图像都具有相同的高度和宽度,与原始图像尺寸无关,同样保持纵横比,以便他们都填写我的imageView而不留任何空格或我不得不裁剪

private static Bitmap resize(Bitmap image, int maxWidth, int maxHeight) {
if (maxHeight > 0 && maxWidth > 0) {
    int width = image.getWidth();
    int height = image.getHeight();
    float ratioBitmap = (float) width / (float) height;
    float ratioMax = (float) maxWidth / (float) maxHeight;

    int finalWidth = maxWidth;
    int finalHeight = maxHeight;
    if (ratioMax > ratioBitmap) {
        finalWidth = (int) ((float)maxHeight * ratioBitmap);
    } else {
        finalHeight = (int) ((float)maxWidth / ratioBitmap);
    }
    image = Bitmap.createScaledBitmap(image, finalWidth, finalHeight, true);
    return image;
} else {
    return image;
}

}

0 个答案:

没有答案