Android | SplashScreen 建置

本次目的是為建置在執行主程式前的 Splash Screen ,此效果在許多 App 上都有實踐,其目的有幾種:顯示團隊 LOGO、作為載入資源的前置作業並顯示進度。

此次需要建立三個檔案。
分別是:

SplashScreen.java ( 和 MainActivity 同資料夾 )
activity_splash.xml ( 和 activity_main.xml 同資料夾 )
圖檔 ( 置於 mipmap 註1)


需要修改以下檔案內容。
分別是:

MainActivity.java ( 避免畫面返回 Splash Screen )
AndroidManifest.xml ( 修改初始啟動畫面 )

1.建立 activity_splash.xml

@mipmap/load 為圖檔
<relativelayout 
  android:layout_height="match_parent" 
  android:layout_width="match_parent" 
  xmlns:android="http://schemas.android.com/apk/res/android">

    <imageview 
    android:id="@+id/imgLogo" 
    android:layout_centerinparent="true" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:scaletype="fitCenter" 
    android:src="@mipmap/load" />

    <textview 
    android:gravity="center_horizontal" 
    android:layout_alignparentbottom="true" 
    android:layout_height="wrap_content" 
    android:layout_marginbottom="10dp" 
    android:layout_width="fill_parent" 
    android:text="Loading..." 
    android:textcolor="#454545" 
    android:textsize="12dp" />

</relativelayout>

2.建立 SplashScreen.java

public class SplashScreen extends Activity {

    // Splash 畫面顯示時間
    private static int SPLASH_TIME_OUT = 3000; // 3000 毫秒


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        new Handler().postDelayed(new Runnable() {

            /*
             * 在特定時間內顯示 初始啟動畫面
             * 此處如果對於要顯示 LOGO 有特別用途
             */

            @Override
            public void run() {
                // 此處會在等待時間結束之後被執行一次
                // 啟動 APP 主要 Activity
                Intent i = new Intent(SplashScreen.this, MainActivity.class);
                startActivity(i);

                // 關閉 SplashScreen Activity
                finish();
            }
        }, SPLASH_TIME_OUT);
    }


}

3.修改 AndroidManifest.xml

xx.yyyyy.zzzz 為你的 package 名稱
<manifest 
  android:versioncode="1" 
  android:versionname="1.0" 
  package="xx.yyyyy.zzzz" 
  xmlns:android="http://schemas.android.com/apk/res/android">

    <application 
    android:allowbackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:roundicon="@mipmap/ic_launcher_round" 
    android:supportsrtl="true" 
    android:theme="@style/AppTheme">
        <activity 
      android:label="@string/app_name" 
      android:name="xx.yyyyy.zzzz.SplashScreen" 
      android:screenorientation="portrait" 
      android:theme="@android:style/Theme.Black.NoTitleBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <!-- MainActivity -->
        <activity 
      android:label="@string/app_name" 
      android:name="xx.yyyyy.zzzz.MainActivity">
        </activity>
    </application>

</manifest>

4.修改 MainActivity.java

在 onCreate 結束之後新增此段,為求避免回到 Splash Screen
    @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);
    }



Work in Android Studio 3.1.4

註1:請注意圖檔大小,分配到適合的資料夾,否則會出錯

參考資料:
How to implement Android Splash Screen
Android 启动页 (Splash) 的实现

留言

這個網誌中的熱門文章

Leetcode:Number of 1 Bits