1. 程式人生 > >Android Splash閃屏

Android Splash閃屏

Why

Getting users to the content they care about should be your #1 priority

Android APP 啟動,替代白屏、黑屏、閃屏

當我們的 APP 已經啟動但尚未在記憶體中時,使用者點選 app 圖示啟動應用程式與實際呼叫啟動程式 Activity 的 onCreate()之間可能會有一些延遲。在“冷啟動”期間,WindowManager 嘗試使用應用程式主題中的元素(如 windowBackground)繪製佔位符 UI。 因此,我們可以將其更改為顯示啟動畫面的自定義drawable,而不是顯示預設的 windowBackground(通常為白色或黑色,也就是我們常說的黑屏、白屏)。這樣,啟動畫面僅在需要時顯示,而且不會減慢使用者啟動 APP 的速度。

How

最佳方式,通過設定 Theme

1、設計一個 splash screen layout(splash.xml)

瞭解一下 Android 圖層列表:LayerList
https://blog.csdn.net/u011489043/article/details/84958186

<?xml version="1.0" encoding="utf-8"?>
<!-- The android:opacity=”opaque” line — this is critical in preventing a flash of black as your theme transitions.
--> <layer-list xmlns:android="http://schemas.android.com/apk/res/android" android:opacity="opaque"> <!-- 背景顏色 --> <item android:drawable="@color/white" /> <item> <!-- splash screen 展示的圖片 居中 不縮放 --> <bitmap android:gravity="center" android:
src="@drawable/headset" /> </item> </layer-list>

2、定義一個 theme,使用以上的 drawable 作為 theme 屬性 windowBackground 的值

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

3、之後,我們把以上 theme 作為應用主介面 MainActivity 的 theme:

<activity android:name=".MainActivity"
      android:theme="@style/SplashTheme">

      <intent-filter>
        <action android:name="android.intent.action.MAIN" />

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

4、最後,我們在 MainActivity 的 onCreate 方法中使用真實的 theme:

public class MainActivity extends BaseActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    // Make sure this is before calling super.onCreate
    setTheme(R.style.AppTheme);

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.bind(this);
  }
}

Conclusion

使用以上的方式,splash screen 將在 App 初始化過程中展示,而且不用為 splash 建立額外的 activity。Splash screen 的展示時間與 activity 的建立時間相關。

優點:

  1. 不需要單獨宣告諸如 LaunchActivity/SplashActivity 類的額外的 Activity;
  2. Splash screen 的展示時間與 activity 的建立時間相關。因此不會影響 app 的啟動時間;

缺點:

  1. 如果主 Activity 再次被建立,Splash screen 還是會顯示(可以用下面的方法解決);
  2. 無法實現按需定製,比如期望該啟動畫面持續時長等;
  3. 無法載入較大的資源(可以使用懶載入、快取的方式解決)

注意的問題

1. 只顯示一次啟動頁( App 沒被 kill 的情況下)

微信開啟之後,按下返回鍵回到桌面,再開啟微信,並不會再看到啟動頁(除非你手動清了微信的後臺或者被系統 kill 了),這個是怎麼實現的呢?

其實很簡單,只需要重寫一下 MainActivity 的 onBackPressed() 方法就行。

// 避免多次啟動 啟動介面
  @Override
  public void onBackPressed() {
    // super.onBackPressed();
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.addCategory(Intent.CATEGORY_HOME);
    startActivity(intent);
  }

參考文章

帶你重新認識:Android Splash頁秒開 Activity白屏 Activity黑屏

Splash Screens the Right Way

強推:The (Complete) Android Splash Screen Guide

——樂於分享,共同進步,歡迎補充
——Any comments greatly appreciated
——誠心歡迎各位交流討論!QQ:1138517609
——CSDN:https://blog.csdn.net/u011489043
——簡書:https://www.jianshu.com/u/4968682d58d1
——GitHub:https://github.com/selfconzrr