如何缩放ImageView宽度

Cha*_*gUZ 1 android android-layout android-imageview

这是我的布局文件

<LinearLayout ...
<ImageView
    android:id="@+id/feed_image"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_horizontal"
    android:adjustViewBounds="true"
    android:contentDescription="@string/image_content_description" />
Run Code Online (Sandbox Code Playgroud)

ImageView宽度与父宽度不匹配.(宽度是图像源宽度)

图像源从URL加载了延迟加载.

如何缩放图像视图无缝图像源的宽度?

我想要

宽度=匹配(或填充)父级.

高度=自动缩放

teh*_*nsi 6

您可以通过两种方式实现此目的:

  1. 如果您的图像大于ImageView,则只能使用xml

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="fitCenter"
        android:adjustViewBounds="true"/>
    
    Run Code Online (Sandbox Code Playgroud)
  2. 如果您的图像小于ImageView

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="fitXY"
        android:adjustViewBounds="true"/>
    
    Run Code Online (Sandbox Code Playgroud)

使用第二个选项,您必须根据图像视图的实际宽度(相同比率)测量图像的宽度和高度,并在代码中设置ImageView的高度.