如何在ImageView的缩放图像,使其与QUOT;适合"使其、缩放、图像、适合

2023-09-08 09:30:45 作者:今晚有约了吗

我想在通过以下方式的ImageView的缩放的图像。该ImageView的具有一定尺寸宽度(W)和高度(H)。我把到图像查看图像可能比宽x高较小或较大。我希望它扩展,而preserving宽高比的填写的宽x高的空间。

I want to scale an image in an ImageView in the following way. The ImageView has some dimensions Width (W) and Height (H). The image I'm putting into the image view could be smaller or bigger than WxH. I want it to scale while preserving aspect ratio to fill WxH space.

这似乎是最接近我想要的是机器人:scaleType =centerInside,但我所看到的是,如果图像比长x宽小,它会把一个小未缩放版本图像的该ImageView的中心(如文档说),但我想它扩展为适合,同时显示整幅图像,并将其延伸到容器的最大尺寸无需裁剪任何东西。换句话说,拉伸preserving宽高比直到宽度或高度凸块到容器(ImageView的)的边缘上。

It seems like the closest thing to what I want is android:scaleType="centerInside", but what I'm seeing is that if the image is smaller than WxH, it will put a small-unscaled version of that image in the center of the ImageView (like the documentation says), but I want it to scale it to "fit", while showing the entire image, and stretching it to the maximum possible size of the container without cropping anything. In other words, stretch preserving aspect ratio until either the width or the height bumps into the edge of the container (ImageView).

这似乎是一个明显的事情要做,但我不能得到它的工作这样!

This seems like an obvious thing to want to do, but I can't get it to work this way!!!

推荐答案

从Android文档...

From the Android docs...

公共静态最终Matrix.ScaleToFit中心

public static final Matrix.ScaleToFit CENTER

计算一个比例,将保持原来的src长宽比,但也将确保的src完全适合内部dst的。至少有一个轴(X或Y)将完全适合。结果里面居中DST。

Compute a scale that will maintain the original src aspect ratio, but will also ensure that src fits entirely inside dst. At least one axis (X or Y) will fit exactly. The result is centered inside dst.

此XML属性是...

The XML attribute for this is...

android:scaleType="fitCenter"