Android: how to scale image to fill LinearLayout background without preserving aspect ratio?

I have a 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 needs to be scaled to fill its LinearLayout, without preserving the aspect ratio of the image. And I do not want each LinearLayout scaled to fit the image size (if the image is higher than LinearLayout, I want the image height to decrease).

In the future, it should look like this:

enter image description here

Does anyone know how to do this?

Thanks!

+4
source share
1 answer

You can use imageView and set its scaleType for fitXY. Here is an example:

 <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. Hello!

EDIT

This is a clearer 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> 
+4
source

All Articles