Splash啟動界面秒開的正確打開模式

谷歌建議

谷歌在material design中提倡使用Splash啟動界面。那Splash啟動界面如何呈現(xiàn)秒開的效果呢,我們下面來看看具體的實(shí)現(xiàn)步驟。

具體實(shí)現(xiàn)

在drawable文件夾下建立splash.xml

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

在style.xml為SplashActivity建立一個theme:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    </style>

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

</resources>

在AndroidManifest.xml中定義SplashActivity的theme為splash_theme,

<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>

SplashActivity的實(shí)現(xiàn)

package com.my.media;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class SplashActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent lIntent = new Intent(this,MainActivity.class);
        startActivity(lIntent);
        finish();
    }
}

注意為了保證啟動速度,SplashActivity不需要實(shí)現(xiàn)setContentView()方法。如果你的SplashActivity設(shè)置了layout文件,那么其在app完全初始化完成后才會顯示,帶來一定的耗時。這里直接以theme作為SplashActivity展示的UI,減少了加載時間達(dá)到秒開的效果。使用該啟動畫面實(shí)現(xiàn)方式亦可以避免有些app點(diǎn)開時出現(xiàn)的白屏問題。
參考

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容