編輯:Android 開發實例
稍微成熟一點的應用,幾乎都會在主界面之前放一個啟動頁。它或簡樸或華麗,或只放一張圖或塞滿信息,風格各異,用意也不盡相同。本文把這個看似簡單的環節系統梳理一遍。
以騰訊 QQ、新浪微博、UC 瀏覽器、神廟逃亡等七個應用的啟動頁做個橫向對比:

個人認為最耐看的是 QQ 2012 那一版:畫面裡只有一張圖,加上應用名稱、版本、圖標這些基本信息,乾淨舒服。可見啟動頁並不是元素越多越好。
啟動頁的本意,是用一個友好的界面掩蓋後台的初始化與加載,讓用戶帶著平和的心情進入應用。但正因為它處在「最開始」這個特殊位置,能承載的事情遠不止於此——應用宣傳、版權展示、廣告投放都可以放在這裡。
常見元素包括:
元素可多可少,取決於你的目的與需求。
從產品角度看:
從技術角度看,還有幾項常被忽略的價值:
為了說明各元素如何排布,這裡刻意做一個毫無修飾的啟動頁:
<?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>
效果示意如下:

單獨強調這一點,是想提醒養成「職責分離」的習慣。以檢查新版本為例:如果塞在主界面裡,它就會和數據加載邏輯糾纏在一起,把主界面越寫越腫。放到啟動頁裡,檢測行為獨立清晰(本來就該在啟動時執行),而且還能順便決定是否允許用戶進入主界面——當你決定放棄舊版本時,強制升級就有一個天然的位置去實現。
啟動頁友好展示的同時,後台可以並行做不少事。用 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 足夠短,不會對體驗造成干擾。
啟動頁是個看似簡單、細節頗多的話題。把元素設計、代碼分離、異步加載、過場動畫、最短展示時長這幾件事處理好,一個得體的啟動體驗就成型了。
Android 自訂 ViewGroup 打造多風格 SlidingMenu
側滑選單是許多 App 常見的互動模式。本文以自訂 ViewGroup 的方式,從最基礎的側滑菜單開始,逐步加入 Scroller 自動吸附、事件攔截、滑動衝突處理,最後
Android 登入介面教學:三次錯誤即停用按鈕
需要帳號的服務,就一定會有登入畫面。使用者在此輸入一組憑證,App 驗證通過後才導向下一頁——Facebook、微博這類應用都是這個模式。本章示範如何從零做出一個登入介面
Android JSON 解析教學:以天氣 API 為例的實務流程
JSON 是目前 Android 應用常遇到的資料格式。當伺服器回傳 JSON 字串時,開發者需要把文字轉成物件,再取出所需欄位。本文以天氣資料為例,說明解析流程。 常見
Android 實作:完整實現 QQ 表情的發送與接收
本文要完整說明如何在 Android 中實作 QQ 風格的表情發送與接收。在自己練習製作聊天應用時,需要用到表情功能,於是便模仿 QQ 表情做了一個,圖片資源完全取自 Q