9

I am trying to make an image cover the screen, by using the following drawable:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@android:color/white" />

    <item>
        <bitmap
            android:gravity="fill_horizontal|fill_vertical"
            android:src="@drawable/launch_image" />
    </item>
</layer-list>

Now, I don't want to FILL the space (stretching the image) but to COVER it (as in CSS3 cover).

Every single answer I have seen only either fills or centers an image, is there really no way to do this?

Just note that I'm using Flutter for this app.

Raj
  • 2,997
  • 2
  • 12
  • 30
arielnmz
  • 8,354
  • 9
  • 38
  • 66

4 Answers4

3

You can find every single parameter you can use for your bitmap here:
https://developer.android.com/guide/topics/resources/drawable-resource#XmlBitmap

I think what you want to use for your gravity value is either clip_vertical or clip_horizontal and you need to combine it with one of the alignment options for gravity: top, left, bottom, right, center_vertical or center_horizontal.

You need to use an image with the biggest aspect ratio (21:9) and define a base for it (the main part of the image). The base must be either in one of the sides or the center.

Example: If you want to put the "base" of your image in the bottom, even if the upper part is clipped, the bottom part keeps what is important. Take a look at the images below, the first is 21:9 and the second is the same image cliped into a 4:3 screen. The third image is how each of the main aspect ratios would clip the image.

Background 21:9

Background 4:3

enter image description here

The same idea can be used to clip images with a 4:3 aspect ratio, where you would keep the height and clip the width.

I hope that answers your question.

Rod
  • 1,601
  • 12
  • 18
  • 1
    In my case any of `clip_vertical` and `clip_horizontal` doesn't fit image to screen leaving white margins at left and right. `clip_vertical` also stretches the image (compressing it vertically) – Pavel Aug 20 '20 at 20:44
  • The image must fit the biggest aspect ratio (21:9), thus even after cliped, it still will cover the entirety of the screen. Your problem is in preparing the image, not the avaible parameters. I'll update my answer with an example. – Rod Aug 20 '20 at 23:08
  • How about landscape mode? will this cover the area? – arielnmz Aug 21 '20 at 21:02
  • It is the same idea. You need to fix one side and work with the other. – Rod Jan 26 '21 at 13:54
-1

below code worked for me....

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:drawable="@android:color/white" />
     <item>
         <bitmap
             android:gravity="fill"
             android:src="@drawable/launch_image" />
     </item> 
</layer-list>
Pavel
  • 5,374
  • 4
  • 30
  • 55
-7

Check the Tolyas answer Starting from API 23, it is possible to set width and height right within the "item" tag

-8

In Flutter, you can achieve it with the help of the following code snippet.

body: new Center(
child: new Image.asset('assets/images/flutter.png'),
),

I found a simple example for it here

Sunil
  • 3,211
  • 3
  • 21
  • 21
  • Splash screen must be implemented in `launch_background.xml` for Android. Also, please format your code. – mertcanb Oct 16 '19 at 18:18