androidandroid-layoutsplash-screenscreen-densitypixel-density

Android Splash Screen: Scale Bitmap


I'm basically using the following xml's for an Android Splash Screen: Empty activity with windowBackground:

<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="android:windowBackground">@drawable/background_splash</item>
</style>

background_splash.xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <color android:color="@color/defaultBackground" />
    </item>
    <item>
        <bitmap
            android:src="@drawable/logo"
            android:tileMode="disabled"
            android:gravity="center"/>
    </item>
</layer-list>

This works fine as long the logo.png is smaller than the screen size. If the logo.png is bigger than the screen it goes beyond the screen.

I see 3 workarounds, but all have drawbacks:

  1. Setting left/right in <item, but this requires API 23+
  2. Vary @drawable/logo for xhdpi, xxhdpi etc. but I'm using Density Split, which would break it when reusing apk's for other devices (apk sites, "Move to new device"-Apps that transfer apks etc.)
  3. Use layout with an ImageView, but this has an noticeable delay

How to do it correctly / without drawbacks?


Solution

  • I found no good solution, so I looked at how Google Drive's splashscreen is implemented.

    Basically they use a single splash.png with 384x384px and put it in drawable-xhdpi and used the following xml:

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list android:opacity="opaque"
        xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@color/defaultBackground" />
        <item>
            <bitmap android:gravity="center" android:src="@drawable/splash" />
        </item>
    </layer-list>
    

    That seems to look good on all devices (that I tested) and solves my issues (specifically 2). Highly recommended!