1

I tried to create splash screen following this https://www.bignerdranch.com/blog/splash-screens-the-right-way/, where a custom theme of the main activity is used to show the splash screen.

<?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="@mipmap/ic_launcher"/>
    </item>
</layer-list>

But I need to use an image for the background in the layer-list item of the theme instead of a solid color. The problem is, as many have pointed out elsewhere, there seems no way to have that background image scaled proportionately center cropped filling the screen like what you can do with ImageView in a layout. I tried to create a new bitmap drawable using the original one programatically based on the screen dimensions, then replace the original drawable. This however doesn't work since the main activity theme shows up immediately after launch and well before the application's constructor, or the activity's constructor or their onCreate get called. Is there anyway to replace that drawable before the windowBackground of the main activity theme shows up on screen?

Bing Qiao
  • 451
  • 1
  • 5
  • 14

1 Answers1

0

. I tried to create a new bitmap drawable using the original one programatically based on the screen dimensions, then replace the original drawable. This however doesn't work since the main activity theme shows up immediately after launch and well before the application's constructor, or the activity's constructor or their onCreate get called.

For the method you are using, your java codes will be useless with the splash screen as you are working with theme that's called before activity starts. So the only thing I can think of is making changes in the drawable itself.

The problem is, as many have pointed out elsewhere, there seems no way to have that background image scaled proportionately center cropped filling the screen like what you can do with ImageView in a layout.

This is where it gets tricky. For us to maintain the ratio while filling the screen will have to done with the images themselves. The best way to do so is to create images for each specific size of the screen (ldpi,mdpi,hdpi,xhdpi,xxhdpi). Loading these images in drawable for various display sizes (Ex: drawable-hdpi will store the image of hdpi resolution) will allow for the image size to be almost constant across various screen resolutions.

The following link will provide the minimum screen size for various displays. https://stackoverflow.com/a/42177842/7550472

With a bit of experimenting, you can get images to the exact size you want in the splash screen, with no code change required from your side. All you have to do is put your image in various sizes for the different displays and it will be working as per your specification.

Note: For image resizing, you can use Adobe Photoshop (I find it easier to work with).

According to preference, you may also use 9 Patch image so that the image's border can stretch to fit the size of the screen without affecting the static area of the image.

http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch

Community
  • 1
  • 1
Kaushik NP
  • 6,733
  • 9
  • 31
  • 60