Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android 啟動界面 SplashActivity 設計與實現全攻略

編輯:Android 開發實例

稍微成熟一點的應用,幾乎都會在主界面之前放一個啟動頁。它或簡樸或華麗,或只放一張圖或塞滿信息,風格各異,用意也不盡相同。本文把這個看似簡單的環節系統梳理一遍。

1. 貨比三家

以騰訊 QQ、新浪微博、UC 瀏覽器、神廟逃亡等七個應用的啟動頁做個橫向對比:

個人認為最耐看的是 QQ 2012 那一版:畫面裡只有一張圖,加上應用名稱、版本、圖標這些基本信息,乾淨舒服。可見啟動頁並不是元素越多越好。

2. 啟動頁可以放哪些元素

啟動頁的本意,是用一個友好的界面掩蓋後台的初始化與加載,讓用戶帶著平和的心情進入應用。但正因為它處在「最開始」這個特殊位置,能承載的事情遠不止於此——應用宣傳、版權展示、廣告投放都可以放在這裡。

常見元素包括:

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

元素可多可少,取決於你的目的與需求。

3. 啟動頁能帶來什麼

從產品角度看:

  • 友好的等待界面,這是最基本的作用
  • 應用的基本說明與宣傳介紹
  • 動態廣告、全屏廣告的展示位
  • 展示版本、版權等屬性信息,讓用戶知曉當前應用的基本狀態

從技術角度看,還有幾項常被忽略的價值:

  • 預加載耗時資源
  • 檢查新版本
  • 預置運行條件
  • 代碼分離

4. 一個「元素全塞進去」的佈局

為了說明各元素如何排布,這裡刻意做一個毫無修飾的啟動頁:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    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"/> 
    <RelativeLayout android:layout_width="fill_parent"
        android:layout_height="fill_parent"> 
        <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"
                android:layout_gravity="center_horizontal"> 
                <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"
                android:layout_gravity="center_horizontal"> 
                <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:layout_gravity="center_horizontal"
                android:padding="6dip"
                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"> 
            </ProgressBar> 
        </LinearLayout> 
    </RelativeLayout> 
</RelativeLayout>

效果示意如下:

5. 代碼分離的價值

單獨強調這一點,是想提醒養成「職責分離」的習慣。以檢查新版本為例:如果塞在主界面裡,它就會和數據加載邏輯糾纏在一起,把主界面越寫越腫。放到啟動頁裡,檢測行為獨立清晰(本來就該在啟動時執行),而且還能順便決定是否允許用戶進入主界面——當你決定放棄舊版本時,強制升級就有一個天然的位置去實現。

6. 異步執行後台任務

啟動頁友好展示的同時,後台可以並行做不少事。用 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 處理。

7. 自定義跳轉動畫

在 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); 
};

8. 設置最短顯示時間

實踐中會遇到一個小尷尬:如果後台任務很快完成,啟動頁一閃而過,反而顯得突兀。解決辦法是定義一個最短展示時長,這裡取 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 足夠短,不會對體驗造成干擾。

9. 小結

啟動頁是個看似簡單、細節頗多的話題。把元素設計、代碼分離、異步加載、過場動畫、最短展示時長這幾件事處理好,一個得體的啟動體驗就成型了。

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