ImageView 仅缩放高度并保持宽度 match_parent
ImageView Scale Only Height and keep width match_parent
我想 ImageView 仅缩放高度并保持宽度 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,图像应缩放到该高度。
我想你问的是秤类型。您可以使用
android:scaleType="fitXY"
如果我理解正确,你可以通过添加上面的代码来修复它。
如果这不是解决方案,您将通过检查 link.
来更清楚地理解主题。
据我了解,
如果您对父级使用线性布局,那么您可以使用 android:layout_weight 并为其分配值 1,并将 android:layout_height 保持为 0dp。这将允许您的图像尽可能地缩放,因为它不允许任何 space 在屏幕上未使用。
<LinearLayout>
<ImageView
android:id="@+id/img"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
scaleY属性用于在Y方向缩放视图,给它一个大于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"
/>
但它可能会拉伸您的 imageView,这看起来很难看,因此请尝试使用 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"
/>
如果您使用以下设置
<ImageView
android:id="@+id/img"
android:layout_width="match_parent"
android:layout_height="140dp">
图像视图将完全匹配父级作为宽度和 140dp 作为高度,但 ImageView 中的图像仍将保持其纵横比,并且将仅拉伸到任一端(宽度或高度),同时保持纵横比.
例如:如果您有 100*70 尺寸的图像,那么在 ImageView 中渲染后的图像将采用 140dp 的高度和 200dp 的宽度来保持纵横比,尽管在宽度上有很多额外的可用空间(宽度一直 match_parent)
但是,如果您希望您的图像占据整个 space 图像视图而不用担心宽高比,那么您可以使用
android:scaleType="fitXY"
fitXY 确保图像被拉伸到完全可用 space。
<ImageView
android:id="@+id/img"
android:layout_width="match_parent"
android:scaleType="fitXY"
android:layout_height="140dp">
例如:如果您有相同的 100*70 尺寸图像,那么在 ImageView 中渲染后的图像将采用 140dp 的高度和宽度将取决于设备的尺寸,因为可用宽度是分配给父级的全宽view (width been match_parent) 并且图像将被拉伸并且将超出纵横比。
我想 ImageView 仅缩放高度并保持宽度 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,图像应缩放到该高度。
我想你问的是秤类型。您可以使用
android:scaleType="fitXY"
如果我理解正确,你可以通过添加上面的代码来修复它。 如果这不是解决方案,您将通过检查 link.
来更清楚地理解主题。据我了解, 如果您对父级使用线性布局,那么您可以使用 android:layout_weight 并为其分配值 1,并将 android:layout_height 保持为 0dp。这将允许您的图像尽可能地缩放,因为它不允许任何 space 在屏幕上未使用。
<LinearLayout>
<ImageView
android:id="@+id/img"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
scaleY属性用于在Y方向缩放视图,给它一个大于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"
/>
但它可能会拉伸您的 imageView,这看起来很难看,因此请尝试使用 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"
/>
如果您使用以下设置
<ImageView
android:id="@+id/img"
android:layout_width="match_parent"
android:layout_height="140dp">
图像视图将完全匹配父级作为宽度和 140dp 作为高度,但 ImageView 中的图像仍将保持其纵横比,并且将仅拉伸到任一端(宽度或高度),同时保持纵横比.
例如:如果您有 100*70 尺寸的图像,那么在 ImageView 中渲染后的图像将采用 140dp 的高度和 200dp 的宽度来保持纵横比,尽管在宽度上有很多额外的可用空间(宽度一直 match_parent)
但是,如果您希望您的图像占据整个 space 图像视图而不用担心宽高比,那么您可以使用
android:scaleType="fitXY"
fitXY 确保图像被拉伸到完全可用 space。
<ImageView
android:id="@+id/img"
android:layout_width="match_parent"
android:scaleType="fitXY"
android:layout_height="140dp">
例如:如果您有相同的 100*70 尺寸图像,那么在 ImageView 中渲染后的图像将采用 140dp 的高度和宽度将取决于设备的尺寸,因为可用宽度是分配给父级的全宽view (width been match_parent) 并且图像将被拉伸并且将超出纵横比。