【问题标题】:image distort by size in ImageView图像在 ImageView 中按大小扭曲
【发布时间】:2020-05-02 05:40:17
【问题描述】:

我正在开发一个购物应用程序,其中有多个带有图片的产品。我有 ImageView 在其中显示该产品图片。我从服务器获取多个图像。我正在制作宽度为“wrap_content”的 ImageView 并使高度为静态。如果我将高度设为“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);

图像在图像视图中拉伸。我想要像亚马逊列表一样的图像。

【问题讨论】:

  • 问题是什么?
  • @RamilGabdrakhmanov 我希望我的图像在图像视图中显示如下图所示。不要拉伸
  • 那需要裁剪吧?
  • @RamilGabdrakhmanov 请检查我编辑的问题
  • @MeowCat2012 请检查我编辑的问题

标签: android android-layout android-fragments android-imageview


【解决方案1】:

假设你想将图像裁剪成 400x200。

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

然后丑陋的Android 2.3星形图标显示为:

400200 更改为您喜欢的尺寸。

ImageView to scale to fixed height, but crop excess width推荐

这正是亚马逊在浏览器中所做的:

【讨论】:

    【解决方案2】:

    你应该为 ImageView 使用android:scaleType attr; 例如android:scaleType="centerCrop"

    【讨论】:

      猜你喜欢
      • 2012-10-16
      • 2016-04-02
      • 2020-09-29
      • 1970-01-01
      • 1970-01-01
      • 2019-04-20
      • 1970-01-01
      • 2019-11-11
      • 1970-01-01
      相关资源
      最近更新 更多