Android: 如何缩放图像以填充LinearLayout的背景,而不保留纵横比?

4

我有一个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> 

我想在三个LinearLayout中分别设置不同的背景图像,这些图像应该按比例缩放以填充其LinearLayout,不保留图像的宽高比。同时,我不希望每个LinearLayout都被缩放以适应图像的大小(如果图像高度大于LinearLayout,则希望图像高度减小)。
下面是期望效果: enter image description here 有人知道如何实现吗?
谢谢!
1个回答

4
您可以使用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>

如果这对你有帮助,请告诉我。 祝好!

编辑

这是一个更明确的示例。

<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>

谢谢您的回答,但它在我的特定情况下不起作用。更准确地说,我有一个RelativeLayout,里面有3个连续的LinearLayouts。我想为每个LinearLayout设置不同的背景图像(适合LinearLayout大小而不保留纵横比)。我将更新我的问题以更准确。 - toto_tata
我刚刚修改了我的问题。希望你能够帮助我! - toto_tata
在这种情况下,将会有三个高度不同的imageView(最好以权重给出)。 - Zadec
如果您能提供一些代码行,我将非常感激。谢谢! - toto_tata

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