Android- SplashPage realizes application opening in seconds - 3 steps

Insert image description here

When an Android application is cold-started, it needs to be started from Application, and the loading time will be relatively long. During this period, all the user can see is a "white screen" (this is because the default AppTheme's android:windowBackground is set to (white), so I think the real startup page should be such that what the user sees when they click on the application is not a "white screen", but a page we created, which can be a picture or a piece of text. It will make people feel that this application can be opened in seconds.

1. First create a new splash_screen.xml file in the drawable directory

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

We use the layer-list tag to create a layer list, which is actually a LayerDrawable, set a background, and then put the application icon. This is the startup page I want to display, which can be customized according to your own needs.

2. Then define a SplashTheme in the style.xml file

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

</resources>

Here we only need to set the window background to the LayerDrawable we just defined.

3. Then we need to set the android:theme of our main page, here is MainActivity, to the SplashTheme we defined in the AndroidMenifest.xml file.

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

    ...

    <application
        ...
         >
        <activity
            android:name=".activity.MainActivity"
            android:launchMode="singleTask"
            android:theme="@style/SplashTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        ...
    </application>

</manifest>

Isn't it very simple? Just like this

Finally, I would like to share with you a set of "Advanced Materials on the Eight Modules of Android" written by Alibaba's senior architects to help you systematically organize messy, scattered, and fragmented knowledge, so that you can master Android development systematically and efficiently. Various knowledge points.

Due to the large content of the article and the limited space, the information has been organized into PDF documents. If you need the complete document of "Advanced Materials on Eight Modules of Android", you can add WeChat to get it for free!

PS: (There are also small benefits for using the ChatGPT robot at the end of the article!! Don’t miss it)

"Android Eight Modules Advanced Notes"

Insert image description here

Compared with the fragmented content we usually read, the knowledge points in this note are more systematic, easier to understand and remember, and are strictly arranged according to the knowledge system.

1. Source code analysis collection

Insert image description here

2. Collection of open source frameworks

Insert image description here

At the same time, a WeChat group chat robot based on chatGPT has been built here to answer difficult technical questions for everyone 24 hours a day .

picture

Guess you like

Origin blog.csdn.net/huahaiyi/article/details/132543444