不进行缩放的ImageView

3

我是一名有用的助手,可以为您翻译文本。

我有一个显示行的ListView。

这是xml:

 ...
  <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="5dp"
        android:layout_weight="1"
        android:background="@color/white"
        android:orientation="vertical" >

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight=".30">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:background="@drawable/face"
                android:scaleType="centerCrop" />
        </FrameLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight=".70"
            android:background="@color/white"
            android:padding="4dp" >

            // Other views...

        </LinearLayout>
    </LinearLayout>

并获得以下结果:

enter image description here

这是原始图像

enter image description here

需要的是没有缩放的图像,就像这样:

enter image description here

我正在使用android:ScaleType =“centerCrop”进行测试,我尝试使用android:adjustViewBounds =“true”和其他许多参数,但从未成功。

有什么好主意吗?

提前感谢!

4个回答

3

将您的图像设置为 android:src 而不是 android:background

<ImageView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:src="@drawable/face"
                android:scaleType="centerCrop" />

所有视图都可以添加背景图片 ImageViewsrc有更多的特性:
  • 不同的缩放类型
  • adjustViewBounds用于设置边界以匹配图像尺寸
  • 一些转换,如透明度设置
您可以在文档中找到更多信息。

0

您可以使用android:src而不是android:background来设置您的可绘制对象。例如:

<ImageView  android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:src="@drawable/face"
            android:scaleType="centerCrop" />

此外,为什么不使用像Photoshop这样的图像编辑软件裁剪图像并使用该图像呢?应尽可能避免动态缩放和操作图像,以防止过度使用系统资源。

0

我认为,你的问题是在LinearLayout和FrameLayout中插入了权重。请使用RelativeLayout而不是权重。系统无法裁剪您的图片,因为系统无法测量ImageView并将其与您的图片进行比较。总之,系统认为“这是图片,ImageView相等”。我已经这样做了:

 <RelativeLayout        
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:padding="5dp">

      <ImageView 
          android:id="@+id/image"
          android:layout_height="50dp"
          android:layout_width="match_parent"
          android:src="@drawable/photo"
          android:scaleType="centerCrop"
          android:contentDescription="@string/app_name"
          android:layout_alignParentTop="true"
          />
      <TextView android:layout_height="150dp"
          android:layout_width="match_parent"
          android:text="@string/text"
          android:layout_below="@id/image"
          android:maxLength="350"
          android:layout_marginTop="5dp"/>

</RelativeLayout>

0

android:background="@drawable/face" 改为 android:src="@drawable/face" 缩放类型仅适用于android:src


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接