Android:如何在不保留宽高比的情况下缩放图像以填充LinearLayout的背景?

时间:2022-08-27 10:54:38

I have got a RelativeLayout like this:

我有一个像这样的RelativeLayout:

<?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                       android:id="@+id/favoriteLinearLayout"
                       android:layout_width="fill_parent"
                       android:layout_height="wrap_content"
                       android:orientation="vertical">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/background1"
        >
    ....
    </LinearLayout>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/linearLayout2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/background2"
            android:layout_below="@+id/linearLayout1"
        >
    ....
    </LinearLayout>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/linearLayout3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/background3"
            android:layout_below="@+id/linearLayout2"
        >
    ....
    </LinearLayout>

</RelativeLayout> 

I want to have a different background image in each of the three LinearLayouts and this image should be scaled to fill its LinearLayout without preserving the aspect ratio of the image. And I don't want each LinearLayout to be scaled to fit the size of the image (if the image is higher than the LinearLayout, I want the image height to be reduced).

我希望在三个LinearLayouts中的每一个中都有不同的背景图像,并且应该缩放此图像以填充其LinearLayout而不保留图像的纵横比。我不希望每个LinearLayout都缩放到适合图像的大小(如果图像高于LinearLayout,我希望图像高度减小)。

Hereafter what it should look like:

此后应该是什么样子:

Android:如何在不保留宽高比的情况下缩放图像以填充LinearLayout的背景?

Anyone knows how to do this ?

谁知道怎么做?

Thanks !!

1 个解决方案

#1


4  

You can use an imageView and set its scaleType to fitXY. Here's an example:

您可以使用imageView并将其scaleType设置为fitXY。这是一个例子:

<FrameLayout  android:layout_width="fill_parent" android:layout_height="fill_parent">

    <ImageView 
      android:layout_width="fill_parent" android:layout_height="fill_parent"
      android:src="@drawable/blue_dark_background" android:scaleType="fitXY" />

    <LinearLayout>....</LinearLayout>
</FrameLayout>

Please let me know if this helps you. Regards!

如果这有助于您,请告诉我。问候!

EDIT

This a more clear example

这是一个更清晰的例子

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

<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="1" >

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="fitXY"
        android:src="@drawable/ic_menu_share" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

    </LinearLayout>
</FrameLayout>
<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="1" >

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="fitXY"
        android:src="@drawable/ic_menu_share" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

    </LinearLayout>
</FrameLayout>
<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="1" >

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="fitXY"
        android:src="@drawable/ic_menu_share" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

    </LinearLayout>
</FrameLayout>

</LinearLayout>

#1


4  

You can use an imageView and set its scaleType to fitXY. Here's an example:

您可以使用imageView并将其scaleType设置为fitXY。这是一个例子:

<FrameLayout  android:layout_width="fill_parent" android:layout_height="fill_parent">

    <ImageView 
      android:layout_width="fill_parent" android:layout_height="fill_parent"
      android:src="@drawable/blue_dark_background" android:scaleType="fitXY" />

    <LinearLayout>....</LinearLayout>
</FrameLayout>

Please let me know if this helps you. Regards!

如果这有助于您,请告诉我。问候!

EDIT

This a more clear example

这是一个更清晰的例子

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

<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="1" >

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="fitXY"
        android:src="@drawable/ic_menu_share" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

    </LinearLayout>
</FrameLayout>
<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="1" >

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="fitXY"
        android:src="@drawable/ic_menu_share" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

    </LinearLayout>
</FrameLayout>
<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="1" >

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="fitXY"
        android:src="@drawable/ic_menu_share" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

    </LinearLayout>
</FrameLayout>

</LinearLayout>