ImageView 仅缩放高度并保持宽度 match_parent

时间:2021-05-02 13:23:28

标签: android android-layout

我想 ImageView Scale Only Height 并保持宽度 match_parent 这样宽度就可以了

这是我的代码,你能给我建议吗。

      <androidx.cardview.widget.CardView
            android:id="@+id/card_view_image"
            android:layout_gravity="center"
            android:layout_width="match_parent"
            android:layout_height="140dp"


            card_view:elevation="0dp"
            card_view:cardCornerRadius="10dp"
            card_view:cardElevation="0dp"
            android:layout_marginTop="15dp"
            card_view:layout_constraintTop_toBottomOf="@+id/user_name"
            tools:ignore="MissingConstraints">

            <ImageView
                android:id="@+id/img"
                android:layout_width="match_parent"
                android:layout_height="140dp">

            </ImageView>
        </androidx.cardview.widget.CardView>

我想将高度设置为 140,图像应缩放到该高度。

4 个答案:

答案 0 :(得分:2)

我想你问的是比例类型。你可以使用

<div class="wrapper">
  <svg height="30" version="1.1" viewbox="0 0 530 15" width="100%" xmlns="http://www.w3.org/2000/svg">
    <rect aria-expanded="false" fill-opacity="1" height="30" width="12" rx="3" ry="3" x="0" y="0"></rect>
    <rect aria-expanded="false" fill-opacity="1" height="30" width="12" rx="3" ry="3" x="0" y="10.25"></rect>
    <rect aria-expanded="false" fill-opacity="1" height="30" width="12" rx="3" ry="3" x="0" y="20.25"></rect>
  </svg>
</div>

如果我理解正确,你可以通过添加上面的代码来解决它。 如果这不是解决方案,如果您检查 link,您将更清楚地理解该主题。

答案 1 :(得分:1)

据我了解, 如果您为父级使用线性布局,则可以使用 android:layout_weight 并将其赋值为 1,并将 android:layout_height 保持为 0dp。这将允许您的图像尽可能缩放,因为它不会在屏幕上留下任何未使用的空间。

<LinearLayout>
<ImageView
    android:id="@+id/img"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"/>
</LinearLayout>

答案 2 :(得分:1)

用于在 Y 方向缩放视图的 scaleY 属性,您可以通过给它一个大于 1 的整数值来检查它。 请参阅下面的代码。

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    android:scaleY="3"
    app:layout_constraintStart_toStartOf="parent"
    android:src="@drawable/ic_launcher_background"
    />

但它可能会拉伸你的图像视图,看起来很难看,所以尝试使用 scaleType 属性,如下所示:

<ImageView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:scaleType="fitCenter"
    app:layout_constraintStart_toStartOf="parent"
    android:src="@drawable/ic_launcher_background"
    />

答案 3 :(得分:1)

如果您使用以下设置

 <ImageView
  android:id="@+id/img"
  android:layout_width="match_parent"
  android:layout_height="140dp">

图像视图将采用完全匹配的父级作为宽度和 140dp 作为高度,但 ImageView 内的图像仍将保持其纵横比,并在保持纵横比的同时仅拉伸到任一端(宽度或高度)。< /p>

例如:如果您有 100*70 尺寸的图像,那么在 ImageView 中渲染后的图像将采用 140dp 的高度和 200dp 的宽度来保持纵横比,尽管宽度上有很多额外的可用空间(宽度为 match_parent)< /p>

但是如果你想让你的图像占据你的 Imageview 的全部空间而不用担心宽高比,那么你可以使用

android:scaleType="fitXY"

fitXY 确保图像被拉伸到可用空间。

<ImageView
  android:id="@+id/img"
  android:layout_width="match_parent"
  android:scaleType="fitXY"
  android:layout_height="140dp">

例如:如果您有相同的 100*70 尺寸图像,那么在 ImageView 中渲染后的图像将采用 140dp 的高度和宽度将取决于设备的尺寸,因为可用宽度是分配给父视图的全宽(宽度为 match_parent) 并且图像将被拉伸并且将超出纵横比。