2

I know, the subject is almost older than Android itself but I went through pretty much every single 9-patch tutorial there is and I still can't make my splash screen maintain its aspect ratio. Below is a 9-patch PNG image literally copied from tutorials as a version that's supposed to keep the logo centered without stretching no matter the aspect ratio. I've tried every possible thing and it just keeps stretching e.g. between 16:9 and 18.5:9 devices. What am I doing wrong?

My 9-patch image

My launcher theme in styles.xml:

    <style name="AppTheme.Launcher">
    <item name="android:windowBackground" >@drawable/splash</item>
        <item name="colorPrimaryDark">@android:color/black</item>
    </style>

My manifest pointing to that theme:

android:theme="@style/AppTheme.Launcher"

And my MainActivity where we switch back to regular theme on startup:

 protected void onCreate(Bundle savedInstanceState) {
        setTheme(R.style.AppTheme);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

Any help is much appreciated, thank you.

The Badak
  • 2,010
  • 2
  • 16
  • 28

1 Answers1

0

Instead of passing your splash drawable directly as the windowBackground, try to include it in a bitmap tag within a layer-list drawable and pass that drawable as the windowBackground.

You can also draw a solid color underneath the bitmap in your layer-list as the background.

background_splash.xml

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

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

    <item>
        <bitmap
            android:gravity="center"
            android:src="@drawable/splash"/>
    </item>

</layer-list>

And then add it in your style:

<item name="android:windowBackground">@drawable/background_splash</item>
Sdghasemi
  • 5,370
  • 1
  • 34
  • 42
  • 1
    Thanks you, very clever approach. It does maintain the ratio, so kudos for that, but it also zooms the splash screen in for no apparent reason. I'm using 1080x1920 vertical splash screen and every device I'm testing it with, no matter the resolution or ratio, seems to show the image enlarged so that roughly 50% of its width fits on screen. I've reformatted the image to 1920x1920 and nothing changed. – Paweł Kmieć Dec 19 '19 at 16:25
  • Since you're using 9-patch the zoom is something expected, unless you use a solid png with a known width and height. If you insist on using 9-patch you can declare a size for your bitmap layer or try to inset it from the screen bounds using the `android:top`, `android:right`, etc. attributes. – Sdghasemi Dec 19 '19 at 16:35
  • At this point I'm using a solid png with known width and height. It always appears zoomed in. I've scaled it down to 480x853 and now most of it fits on screen, but still not all of it. – Paweł Kmieć Dec 19 '19 at 16:38
  • Unfortunately I don't have access to an IDE right now, will help you as soon as I can get my hands on one. In the mean time check [this](https://stackoverflow.com/q/10574363/4399414) question out see if it can solve your problem. – Sdghasemi Dec 19 '19 at 16:54
  • @PawełKmieć was there any progress? have you tried putting your bitmap in `mipmap-*` directories instead of `drawable-*` and check the result? – Sdghasemi Dec 22 '19 at 08:27
  • Sorry for the late reply. I've been fiddling around with subdirectories and the results are somewhat odd. See, if I move properly scaled versions of the bitmap into drawable-* or mipmap-* directories, the splash screen shows the right version of the image but it's smaller than the screen, centered with a margin of at least 20% on all sides. I can remove android:gravity="center" from bitmap (or change it to "fill") in the XML and then the image fills the whole screen, but then it gets stretched without maintaining the aspect ratio, so we're back to square one. – Paweł Kmieć Jan 04 '20 at 11:05
  • Why don't you use a transparent small image (say 480x480) in the middle with `android:gravity="center"` and use the yellow color as the background color to fill the entire screen as well as underneath the image? I assumed you needed something like a google maps splash screen (without the typo), let me know to update my answer in case you are looking for a different pattern. – Sdghasemi Jan 04 '20 at 18:44
  • Yes, this is what I ended up doing. I was just hoping that I could have a splash screen filling the entire screen but I guess not. Still, you were immensely helpful, thank you very much. – Paweł Kmieć Jan 07 '20 at 10:39