Android:ImageView,保持宽高比

时间:2014-10-06 13:30:27

标签: android image

假设我有图片test.png,大小为h:40px w:80px

我有ImageView,身高:80px。

如何将图像设置为此ImageView,以便图像可以按比例适合ImageView?

所以我需要图像来适应高度,这意味着它应该变成80px的高度, 并且适合宽度与图像成比例所需的宽度,因此宽度应为160px

我知道我可以剪掉更大的图像,这不是我的解决方案。

尝试:

android:adjustViewBounds="true"
android:scaleType="fitXY"

但不调整宽度。

3 个答案:

答案 0 :(得分:1)

fitXY不保留方面无线电。您必须使用CENTER_CROPCENTER_INSIDE中的一个。来自doc

  

均匀缩放图像(保持图像的纵横比)以便   图像的尺寸(宽度和高度)将等于或   大于视图的相应维度

答案 1 :(得分:0)

您有一个ImageView(或其他View),您希望宽度为fill_parent,并按比例缩放高度:

将这两个属性添加到ImageView

android:adjustViewBounds="true"
android:scaleType="centerCrop"

并将ImageView宽度设置为fill_parent,将高度设置为wrap_content

请参阅android:adjustViewBounds

答案 2 :(得分:0)

使用“ android:src”代替背景

相关问题