[图像在Android中的ImageView中按尺寸失真

问题描述 投票:0回答:1

我正在开发一个购物应用程序,其中有多个带有图片的产品。我有ImageView在其中显示该产品图像。我正在从服务器获取多个图像。我正在制作ImageView,其宽度为“ wrap_content”,并使高度为静态。如果我将高度设置为“ wrap_content”,则ImageView大小将变得太大。我不想使ImageView太大

XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>

<androidx.cardview.widget.CardView
    android:id="@+id/layProduct"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/white"
    android:elevation="@dimen/dp5"
    android:orientation="vertical"
    app:cardCornerRadius="@dimen/dp8">

    <RelativeLayout
        android:id="@+id/layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="@dimen/dp10">

        <ImageView
            android:id="@+id/ivProduct"
            android:layout_width="wrap_content"
            android:layout_height="@dimen/dp124"
            android:layout_centerHorizontal="true"
            android:adjustViewBounds="true"
            android:src="@mipmap/image_placeholder" />


        <TextView
            android:id="@+id/txtProductName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/ivProduct"
            android:layout_marginStart="@dimen/dp14"
            android:maxLines="1"
            android:text="English umbrella"
            android:textColor="#ff393737" />

        <TextView
            android:id="@+id/txtProductPrice"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/txtProductName"
            android:layout_marginStart="@dimen/dp14"
            android:text="$24.99"
            android:textColor="#ff393737"
            android:textStyle="bold" />
    </RelativeLayout>
</androidx.cardview.widget.CardView>

代码:

             Glide.with(activity).load(image)
                .placeholder(R.mipmap.image_placeholder)
                .error(R.mipmap.image_placeholder)
                .into(ivProduct);

enter image description here

图像在图像视图中拉伸。我想要像亚马逊清单一样的图片。

enter image description here

android android-layout android-fragments android-imageview
1个回答
0
投票

说,您想将图像裁剪为400x200。

<ImageView
                    android:layout_width="400dp"
                    android:layout_height="200dp"
                    android:scaleType="centerCrop"
                    android:src="@android:drawable/btn_star"/>

然后,丑陋的Android 2.3星形图标显示为:screenshot of the cropped star icon

400200更改为您的首选尺寸。

ImageView to scale to fixed height, but crop excess width建议

© www.soinside.com 2019 - 2024. All rights reserved.