谷歌建議
谷歌在material design中提倡使用Splash啟動(dòng)界面。那Splash啟動(dòng)界面如何呈現(xiàn)秒開(kāi)的效果呢,我們下面來(lái)看看具體的實(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建立一個(gè)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();
}
}
注意為了保證啟動(dòng)速度,SplashActivity不需要實(shí)現(xiàn)setContentView()方法隙袁。如果你的SplashActivity設(shè)置了layout文件,那么其在app完全初始化完成后才會(huì)顯示弃榨,帶來(lái)一定的耗時(shí)菩收。這里直接以theme作為SplashActivity展示的UI,減少了加載時(shí)間達(dá)到秒開(kāi)的效果鲸睛。使用該啟動(dòng)畫(huà)面實(shí)現(xiàn)方式亦可以避免有些app點(diǎn)開(kāi)時(shí)出現(xiàn)的白屏問(wèn)題娜饵。
參考