2
votes

I am working on getting a splash activity implemented in my project. The splash activity currently loads perfectly, and there's no "white flash" before the image loads - which is good.

My only problem is maintaining the correct aspect ratio of the splash screen image.

Here's the theme I am using for SplashActivity

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

splash.xml

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
        android:src="@drawable/pink_grid"
        android:gravity="fill_vertical|clip_horizontal"
/>

And here's a screenshot of the result I am getting:

Improper Aspect Ratio

The black / pink grid image that I am using as the background has uniform squares. As you can see from the image, it's not maintaining a proper aspect ratio (being squished horizontally).

Here's the grid image (1280x1920):

Pink and black grid image

What I tried:

It seems the only way to control the aspect ratio of the splash windowBackground is by using gravity. I tried to fill the image vertically and crop horizontally. But this does not maintain aspect ratio.

How can I adjust the gravity of the splash image to maintain aspect ratio and fit the screen on any device?

Edit: Progress based on Raz's answer:

activity_splash.xml

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
    <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/pink_grid" android:scaleType="centerCrop"/>
</merge>

SplashActivity.kt

class SplashActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_splash)

//        val intent = Intent(this, MainActivity::class.java)
//        startActivity(intent)
//        finish()
    }
}

AndroidManifest.xml

<application>
    <!-- ... -->
    <activity
            android:name=".SplashActivity"
            android:theme="@style/SplashTheme">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
</application>

styles.xml

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

The splash screen is just a black screen now. The pink / black grid is not showing up.

3
i am at the same situation as you were. did you solve it? if yes then please let me know.Tara

3 Answers

0
votes

AFAIK, windowBackground will always fill the screen. What you need to do is:

Set the layout of the activity to:

<FrameLayout 
        android:width="match_parent" 
        android:height="match_parent">
    <ImageView  
android:width="match_parent"  
android:height="match_parent"  
android:ScaleType="CENTER_CROP"  android:src="your_image"/>
        </FrameLayout>

The imageView property scaletype will preserve the aspect ratio.

As optimizations:

  1. Set windowBackground as transparent (for reducing overdraw)
  2. Change the root FrameLayout to <merge> - like so:

<merge> <ImageView
android:width="match_parent"
android:height="match_parent"
android:ScaleType="CENTER_CROP" android:src="your_image"/> </merge>

0
votes

When using vector drawable the following works great:

drawable/splash.xml

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

For those who land on this page but couldn't find an answer:

Here is a solution.

Basically, you need to change the src from the raw "png/jpg/..." to xml, and the xml imports the raw image with setting android:gravity="center".