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

啟動頁 SplashActivity 怎麼做:元素設計、異步加載與跳轉動畫

編輯:關於 Android 程式設計

比較成熟的應用,幾乎都會在進入主界面前放一個啟動頁。它或簡單或華麗,或樸素或炫目,用處不同,風格自然也不同。先看幾個主流應用的啟動頁找找感覺。

一、貨比三家

以 QQ、新浪微博、UC 瀏覽器、神廟逃亡等七個應用為例:

個人覺得最耐看的是 QQ 2012:畫面裡只有一張圖,應用名稱、版本、圖標等基本資訊都在,但看起來舒服、乾淨。

二、啟動頁可以放哪些元素

啟動頁的本意,是用一個友善的介面掩蓋後台的緩衝與加載,讓使用者帶著平和的心情進入應用。但正因為它處在「開始」這個特殊位置,能做的事反而更多:應用宣傳、版權展示、廣告投放等等。

所以元素可多可少,取決於你的目的與需求。常見的有:

  1. 應用名稱
  2. 應用版本
  3. 應用圖標
  4. 版權資訊
  5. 一句話描述
  6. 進度條
  7. 額外資訊,如市場專版、獨家首發等

三、它能帶來什麼好處

綜合來看,至少有這些價值:

  • 一個友善的等待介面,這是最基本的作用;
  • 承載應用的基本說明與宣傳介紹;
  • 可以放動態廣告甚至全屏廣告;
  • 展示版本、版權等屬性,讓使用者清楚當前用的是什麼版本。

從技術角度看,還能順手做這些事:

  • 預加載耗時資源;
  • 檢查新版本;
  • 準備預設條件與初始狀態;
  • 把這些初始化邏輯從主介面剝離出去,實現代碼分離。

四、一個「毫無修飾」的佈局示例

把能塞的元素統統塞進去,就得到下面這個只為表意、不講美感的佈局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff">

    <!-- 底部版權 -->
    <TextView android:id="@+id/copy_right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="12dip"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        android:text="by xxxxx 出品"
        android:textSize="11sp" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_centerInParent="true">

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <ImageView android:id="@+id/jay_studio_icon"
                android:layout_width="110dip"
                android:layout_height="130dip"
                android:src="@drawable/app_jay" />
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/jay_studio_icon"
                android:src="@drawable/icon" />
        </RelativeLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <TextView android:id="@+id/app_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="6dip"
                android:text="@string/app_name"
                android:textSize="24sp" />
            <TextView android:id="@+id/version_name"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:gravity="bottom"
                android:paddingBottom="6dip"
                android:textSize="14sp" />
        </LinearLayout>

        <View android:layout_width="fill_parent"
            android:layout_height="1px"
            android:layout_marginLeft="20dip"
            android:layout_marginRight="20dip"
            android:background="#dddddd" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/setting_about_description"
            android:textSize="13sp" />

        <ProgressBar android:id="@+id/refresh_list_footer_progressbar"
            android:layout_width="24dip"
            android:layout_height="24dip"
            android:indeterminateDrawable="@anim/app_refresh_progress"
            android:layout_gravity="center" />
    </LinearLayout>
</RelativeLayout>
</RelativeLayout>

效果大致如下:

五、代碼分離:值得單獨強調

把這一點單拎出來,是因為寫程式時的「分離意識」實在太重要了。

以檢查新版本為例:如果寫在主介面裡,一個本來很獨立的動作就和「加載首屏資料」攪在一起,主介面的複雜度直線上升。放到啟動頁裡就乾淨多了——檢查新版本本來就該在應用啟動時做,而且順便還能決定要不要放使用者進主介面(當你決定放棄舊版本的時候,強制升級就靠這一步)。

其他初始化動作也可以用同樣的思路審視一遍,往往能優化出更清晰的程式碼結構。

六、用 AsyncTask 異步執行

啟動頁友善展示的同時,後台可以做很多事,用 AsyncTask 就能最簡單地實現。別的方法當然也行,但在這個場景下它最簡潔——不用它,還能用在哪?

public class SplashActivity extends BaseActivity {

    private static final int FAILURE = 0;   // 失敗
    private static final int SUCCESS = 1;   // 成功
    private static final int OFFLINE = 2;   // 進入離線模式

    private TextView mVersionNameText;

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

        mVersionNameText = (TextView) findViewById(R.id.version_name);
        mVersionNameText.setText(BaseApplication.mVersionName);

        new AsyncTask<Void, Void, Integer>() {

            @Override
            protected Integer doInBackground(Void... params) {
                int result;
                result = loadingCache();
                return result;
            }

            @Override
            protected void onPostExecute(Integer result) {
                // 後續跳轉邏輯
            }
        }.execute(new Void[]{});
    }

    private int loadingCache() {
        if (BaseApplication.mNetWorkState == NetworkUtils.NETWORN_NONE) {
            return OFFLINE;
        }
        return SUCCESS;
    }
}

把所有耗時操作放進 doInBackground,最後回傳一個狀態碼,作為後台執行的結果,交給 onPostExecute 決定下一步。

七、自定義跳轉動畫

onPostExecute 裡最終一定要跳到下一個 Activity 並把自己 finish 掉。這個跳轉動畫是可以自定義的(手機預設往往是左右滑入滑出),比如改成淡入淡出。

先在 res/anim 下定義兩個動畫:

<!-- fade_in.xml -->
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    <alpha android:fromAlpha="0" android:toAlpha="1" android:duration="500" />
</set>

<!-- fade_out.xml -->
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    <alpha android:fromAlpha="1" android:toAlpha="0" android:duration="500" />
</set>

然後在 finish 之後呼叫 overridePendingTransition:

@Override
protected void onPostExecute(Integer result) {
    Intent intent = new Intent();
    intent.setClassName(SplashActivity.this, getString(R.string.splash_out_activity));
    startActivity(intent);
    finish();
    // 兩個參數分別是進入動畫與退出動畫
    overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
}

八、設定最短顯示時間

實測中發現一個小問題:如果後台操作特別快,啟動頁一閃而過,反而有一種「閃屏」的突兀感。解法是定義一個最短顯示時間,這裡取 800ms:

private static final int SHOW_TIME_MIN = 800;

@Override
protected Integer doInBackground(Void... params) {
    int result;
    long startTime = System.currentTimeMillis();
    result = loadingCache();
    long loadingTime = System.currentTimeMillis() - startTime;
    if (loadingTime < SHOW_TIME_MIN) {
        try {
            Thread.sleep(SHOW_TIME_MIN - loadingTime);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    return result;
}

這樣跳轉時就不會再有「閃一下」的感覺,而 800ms 足夠短,不會對使用體驗造成困擾。

九、小結

啟動頁是個不算複雜的話題,本文只是把實戰中碰到的幾個具體問題做了一次小整理,留作記錄。

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