Drawable as background of splash theme doesn't keep ratio

1.7k Views Asked by At

In my application, I'm trying to set the splash theme with a drawable background. It works correctly if I use a small image, but when the image is bigger then the screen size, it doesn't work anymore.

The image is correctly scaled horizzontally, but the height isn't, as follows: my splash

I tried every scaletype and gravity, what else can I do?

this is my Theme:

<style name="SplashTheme" parent="AppTheme">
    <item name="android:windowBackground">@drawable/background_splash</item>
</style>

This is my background_splash.xml

<item android:drawable="@android:color/white" />

<item
    android:left="50dp"
    android:right="50dp"
    >
    <bitmap android:src="@drawable/logo_splash"
        android:gravity="center|clip_horizontal"
        android:scaleType="centerInside"
        />
</item>

and this is my manifest (relevant code):

    <activity
        android:name=".activities.SplashScreenActivity"
        android:configChanges="orientation|screenSize"
        android:theme="@style/SplashTheme">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

Any idea?

1

There are 1 best solutions below

0
On

You can use a layer-list like this in your background_splash.xml:

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

    <item
        android:drawable="@drawable/logo_splash"
        android:gravity="center" />
</layer-list>

This will center your image on the screen and the left space will be filled by the white background color.

Maybe you have to add the image in different sizes like you can read here: https://developer.android.com/training/multiscreen/screendensities#TaskProvideAltBmp

If you do that you can access the image with the same name. It will look like this your project structure:

enter image description here

If you want to remove the action and status bar in your splash screen, you can add the following line to your SplashTheme:

<item name="android:windowFullscreen">true</item>