ImageView填充父级的宽度或高度,但保持纵横比

gar*_*tor 41 android android-layout android-imageview android-relativelayout

我有一个方形图像(虽然这个问题也适用于矩形图像).我想尽可能大地显示图像,必要时拉伸它们以填充它们的父母,同时仍然保持纵横比.图像小于ImageView.问题是,我无法拉伸图像并"匹配"ImageView的高度和宽度.

这是我的XML布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:padding="10dp">

    <ImageView android:id="@+id/image"
               android:layout_width="fill_parent"
               android:layout_height="fill_parent"
               android:adjustViewBounds="true"
               android:scaleType="fitCenter"
               android:layout_marginTop="10dp"/>

    <TextView android:id="@+id/name"
              android:layout_below="@id/image"
              android:layout_alignLeft="@id/image"
              android:layout_marginTop="20dp"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:textSize="18dp"/>

    <TextView android:id="@+id/name2"
              android:layout_below="@id/name"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:textSize="14dp"/>


</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

我已经使用的多种组合fill_parent,wrap_content与多个scaleTypes: ,fitCenter,fitStart,fitEnd,centerInside他们都画在正确的纵横比的图像,但没有人真正缩放图像UP和ImageView的本身,导致无论是在TextViews被推所有屏幕下方,ImageView内部空白区域,图像未缩放或图像裁剪.

我无法为此找到合适的组合.

Foa*_*Guy 103

这些:

android:layout_height="wrap_content"
android:scaleType="fitStart"
android:adjustViewBounds="true"
Run Code Online (Sandbox Code Playgroud)

应调整图像大小并更改边界的大小以适合新的图像大小.如果在您的设备上没有这样做,请发布您正在使用的图像以及您正在测试的设备.

  • 不行.我不能发布图像,因为它们是专有的,但它们只是标准的128x128 PNG.这也发生在模拟器上.所以这不是设备特定的问题.这些图像比ImageView小.我认为这就是问题所在. (2认同)
  • FitXY代替fitStart工作,否则它很好,谢谢 (2认同)

Con*_*ext 46

使用此代码: android:scaleType="fitXY"

有关图像缩放的详细信息,看看这是什么文章中示意这里

摘要:

  • center

将图像置于视图中心,但不执行缩放

在此输入图像描述

  • centerCrop

均匀缩放图像(保持图像的纵横比),使图像的尺寸(宽度和高度)等于或大于视图的相应尺寸(减去填充).然后图像在视图中居中

在此输入图像描述

  • centerInside

均匀缩放图像(保持图像的纵横比),使图像的尺寸(宽度和高度)等于或小于视图的相应尺寸(减去填充)

在此输入图像描述

  • fitCenter

在此输入图像描述

  • fitEnd

在此输入图像描述

  • fitStart

在此输入图像描述

  • fitXY

在此输入图像描述

  • matrix

绘图时使用图像矩阵缩放

在此输入图像描述

这里有更多细节新文章

  • 因为它不保持源图像的宽高比.见[here](http://bon-app-etit.blogspot.in/2014/01/imageview-scaletypes.html). (10认同)

aka*_*tra 14

将此代码与视图布局参数一起用作wrapcontent

机器人:adjustViewBounds = "真"

希望它会起作用.


Feb*_*hew 14

这个xml代码将起作用!如果您指定应用程序宽度始终与窗口相同,android:adjustViewBounds="true"则将设置高度与图像的比例相对应.

        <ImageView
        android:adjustViewBounds="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/screen"/>
Run Code Online (Sandbox Code Playgroud)