ImageView:填充水平维持宽高比

Add*_*dev 8 layout android imageview android-layout

我需要一个ImageView缩放图像,直到水平填充父级.

如果图像视图背景为红色,其余内容(图像下方)为绿色,我正在寻找图片1所示的结果.如果图像宽度高于屏幕宽度,则自动获得该结果.

但如果是图片2中的小图片,我可以得到的最好结果是图片3(将ImageView宽度和高度设置为fill_parent,将scaleType设置为FitStart

获得图片4设置height = wrap_content,width = fill_parent,scaleType = CenterCrop.它应该垂直缩放以显示整个图像,但是当scaleType表示它会裁剪它.

即使图像很小,任何获取图片1的想法?

将奖励50分给工作答案

在此输入图像描述

War*_*zit 1

无需自定义类即可实现。这是我用小图像获得的结果: 在此输入图像描述

大图: 在此输入图像描述

您必须使用RelativeLayout才能使其发挥作用,但您可以将其结合起来LinearLayout以实现您需要的任何功能。这是我的 xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <RelativeLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <LinearLayout
            android:id="@+id/button_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_alignParentBottom="true"
            >
            <Button
                android:text="button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <Button
                android:text="button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <Button
                android:text="button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
        </LinearLayout>
        <ImageView 
            android:src="@drawable/cat"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:adjustViewBounds="true"
            android:scaleType="centerCrop"
            android:layout_above="@id/button_container"/>
    </RelativeLayout>
</ScrollView>
Run Code Online (Sandbox Code Playgroud)

诀窍是您将其设置为填充屏幕,但它必须位于其他布局之上。这样您就可以实现所需的一切。