且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

缩放图像以填充 ImageView 宽度并保持纵横比

更新时间:2023-08-27 21:15:58

不使用任何自定义类或库:

Without using any custom classes or libraries:

<ImageView
    android:id="@id/img"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:scaleType="fitCenter" />

scaleType="fitCenter"(省略时默认)

  • 将使其尽可能宽,并根据需要保持纵横比.

scaleType="centerInside"

  • 如果 src 的固有宽度小于父宽度
    将图像水平居中
  • 如果 src 的固有宽度大于父级宽度
    将使其与父级允许的宽度一样宽,并按比例缩小以保持纵横比.
  • if the intrinsic width of src is smaller than parent width
    will center the image horizontally
  • if the intrinsic width of src is larger than parent width
    will make it as wide as the parent allows and down-scale keeping aspect ratio.

使用 android:srcImageView.setImage* 方法没关系,关键可能是 adjustViewBounds.

It doesn't matter if you use android:src or ImageView.setImage* methods and the key is probably the adjustViewBounds.