Malfunction
Malfunction

Reputation: 1334

Filling a background image to screen without changing aspect ratio

I'd like to display an image as a background for my activity, but would like to make it fill to screen without changing the aspect ratio of the image (something like this).

I tried scaling the image, but that doesn't seem to work (because I'm using the image as a background, not defining it in an imageview, or so seems the problem).

How do I go about doing that? I even tried to define the background image in its own xml file, but that still didn't work.

Upvotes: 1

Views: 482

Answers (2)

k0nig
k0nig

Reputation: 340

If you uses a RelativeLayout you can put a ImageView than fill all layout and put the rest over it. Something like that:

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

<ImageView
    android:id="@+id/background"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="centerCrop"
    android:src="@drawable/background" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    // Your full layout
</LinearLayout>

</RelativeLayout>

Upvotes: 4

gingo
gingo

Reputation: 3169

AFAIK the only solution is the one with ImageView you mentioned. Why it is not appropriate for you? You can also use Bitmap.createBitmap() method to resize your bitmap do desired size.

Upvotes: 0

Related Questions