Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 啟動畫面的兩種實作方法

編輯:關於 Android 開發

Android 啟動畫面的兩種實作方法


現在打開任一款 App,大部分都會顯示一個啟動畫面,展示公司的 Logo 與當前版本,有的乾脆把廣告放上去。啟動畫面可以分為兩種設定方式:一種用兩個 Activity 實現,另一種用一個 Activity 實現。以下分別介紹這兩種做法。

一、兩個 Activity

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Window;

public class SplashActivity extends Activity {
    private static int SPLASH_DISPLAY_LENGHT = 6000; // 延遲 6 秒

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_NO_TITLE); // 去掉標題
        setContentView(R.layout.activity_splash);
        new Handler().postDelayed(new Runnable() {
            public void run() {
                Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                startActivity(intent);
                SplashActivity.this.finish(); // 關閉並回收 SplashActivity,否則按返回鍵會回到此頁
            }
        }, SPLASH_DISPLAY_LENGHT);
    }
}

別忘了設定 AndroidManifest.xml:

<activity android:name="com.example.andorid_splash_0.SplashActivity" android:label="splash">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<activity android:name=".MainActivity" android:label="@string/app_name">
</activity>

不難看出:SplashActivity 先於 MainActivity 啟動,經過 6 秒後才啟動 MainActivity。

補充一點知識(Handler 的幾個常用方法):

// 立即執行 Runnable 物件
public final boolean post(Runnable r);
// 在指定時間(uptimeMillis)執行 Runnable 物件
public final boolean postAtTime(Runnable r, long uptimeMillis);
// 在指定時間間隔(delayMillis)後執行 Runnable 物件
public final boolean postDelayed(Runnable r, long delayMillis);

二、一個 Activity 啟動

先看佈局檔,裡面放了兩個充滿螢幕的元件(一個 ImageView 與一個 TextView):

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <LinearLayout
        android:id="@+id/splashScreen"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ImageView
            android:id="@+id/iv_image"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/new00"/>
    </LinearLayout>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="100dp"
        android:gravity="center"
        android:text="主界面"/>
</LinearLayout>

Activity 程式碼:

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class MainActivity extends Activity {
    private LinearLayout splash;
    private ImageView iv_image;
    private static final int STOPSPLASH = 0;
    private static final long SPLASHTIME = 1000;

    private Handler splashHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case STOPSPLASH:
                    SystemClock.sleep(4000); // 休眠 4 秒
                    splash.setVisibility(View.GONE);
                    break;
            }
            super.handleMessage(msg);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        splash = (LinearLayout) findViewById(R.id.splashScreen);
        Message msg = new Message();
        msg.what = STOPSPLASH;
        splashHandler.sendMessageDelayed(msg, SPLASHTIME); // 在 SPLASHTIME 時間後發送訊息
    }
}

三、總結:

以上兩種方法都能實現應用啟動前的開機畫面,但在實際開發中還是建議採用第一種,因為主界面的程式碼不宜過多,應當保持簡潔。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved