Android 将图像缩放到最大可用宽度,保持纵横比并避免ImageView额外空间

Android 将图像缩放到最大可用宽度,保持纵横比并避免ImageView额外空间,android,layout,scale,fill-parent,Android,Layout,Scale,Fill Parent,我有一个简单的XML布局,还有一个30X30像素的green_square.png文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"

我有一个简单的XML布局,还有一个30X30像素的green_square.png文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ImageView
        android:background="@color/red"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:adjustViewBounds="true"
        android:scaleType="fitStart"
        android:src="@drawable/green_square"/>

</LinearLayout>

我试图实现的是缩放图像以填充父宽度,保持其纵横比而不增加ImageView的空间

我在ImageView中添加了一个红色背景,只是为了避免额外的空间

使用刻度类型“fitCenter”或“fitxy”

试试这个

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<ImageView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/red"
    android:scaleType="centerCrop"
    android:src="@drawable/green_square" />

</LinearLayout>

在我看来,这个
ImageView
额外的布局是一个bug。我也有同样的事情发生。因此,我想要跟随绿色方格的文本遵循了额外的布局。所以我所做的就是指定相对于屏幕底部的文本。这样,文本将覆盖额外的布局。我承认,这不是最好的解决方案,但它看起来比在绿色正方形和文本之间有一个巨大的空间要好得多。


<ImageView
        android:id="@+id/game"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:scaleType="centerCrop"
        android:adjustViewBounds="true"
        android:src="@drawable/ic_game" 
        />

这对我有用。父级和中心裁剪的全宽,以保持纵横比,非常简单。只需将“宽度填充”设置为“父级”,将“高度包裹”设置为“内容”。这是我找到的答案

<ImageView
    android:id="@+id/game"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:scaleType="centerCrop"
    android:adjustViewBounds="true"
    android:src="@drawable/ic_game" 
    />

这里有很多很好的答案,这些答案确实有效。我只是想用一种更现代的XML语法,约束视图来分享我现在的做法。相信我,你的车

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#394BAB"
    tools:context="com.appdomain.app.apppro.ActivityName">

    <ImageView
        android:id="@+id/nice_img"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="0dp"
        android:src="@drawable/green_square"
        android:scaleType="centerCrop"
        android:adjustViewBounds="true"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

</android.support.constraint.ConstraintLayout>


当然是 工具:context=“com.collectsavings.collect.collectpro.LoginAndRegisterPrompt” 及 android:src=“@drawable/green_square”
应该分别与您的应用程序活动上下文和drawabl资源匹配。

感谢您的建议,但是此布局占据了整个视图。以下是由于缺乏恰当的描述,我想要实现的目标。这只是一段代码,不是答案。请尝试
android:adjustViewBounds=“true”
谢谢,这是我的想法,但它已经存在了。看起来这个问题困扰着许多用户,请查看此链接尝试此链接,为我做了工作:非常好!这就成功了,但奇怪的是,在原始图像视图上没有设置此行为的属性。感谢您的回复,但是,fitCenter只是将视图居中,将页边距保留在顶部和底部,而不是底部(如果使用fitStart)通过Bitmapfactory创建位图图像,您可以在代码中而不是在xml中设置位图图像,但如果不降低图像的纵横比,则无法在全屏显示30X30像素的图像。使用BitmapFactoey创建位图图像,然后使用位图创建功能创建缩放图像。它将帮助您。。感谢您的回复,代码解决方案是一个选项,但我仍然想确定(据我所知)是否可以从XML中实现。我不认为这是用XML实现的,如果有机会,那么必须用代码部分实现。为我工作。应该是公认的解决方案。谢谢
centerCrop
将图像拉伸至全宽,但可能会在视图保持原始高度不变的情况下从顶部和底部进行裁剪,但图像纵横比会保持不变,因此不会被挤压或拉伸
adjustViewBounds
允许高度增长以避免实际裁剪,并使视图保持图像的纵横比。它们一起使用,可以执行OP所需的操作。将
centerCrop
替换为
fitXY
我没有注意到任何差异。有什么区别?