简体   繁体   中英

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.

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>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM