2

I want to place a background image in a RelativeLayout. The background image has got some objects in it, like a chair, a table, some cards, etc.

android:background="@drawable/remy_menu_background" but the damn thing stretches according the the screen, the cards and table look stretched.

How should i go about doing this, so it looks ok on any screen?

AndreiBogdan
  • 10,858
  • 13
  • 58
  • 106
  • May be the same case : http://stackoverflow.com/questions/8202647/setting-a-background-image-to-a-view-stretches-my-view – MKJParekh Jul 12 '12 at 09:27

3 Answers3

9

You can use an XML drawable that uses the tag to disable the stretching:

<bitmap android:src="@drawable/background" android:gravity="center" />
Romain Guy
  • 97,993
  • 18
  • 219
  • 200
2
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ImageView
    android:id="@+id/main_backgroundImage"
    android:layout_width="match_parent" // comment: stretches picture in the width if too small. 
                                           use "wrap_content" does not stretch, but leaves space
    android:layout_height="match_parent" // in my case I always want the height filled
    android:layout_alignParentTop="true"
    android:scaleType="centerCrop" // will crop picture left and right , so it fits in height and keeps aspect ratio
    android:contentDescription="@string/image"
    android:src="@drawable/your_image" />

<LinearLayout
    android:id="@+id/main_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
</LinearLayout>
1

You can use .9.png images in such situation and can specify which area of the image should stretch and which should not. You can convert any png into .9.png using "draw9patch" tool available under 'tools' folder inside android-sdk.

Vipul J
  • 6,641
  • 9
  • 46
  • 61