編輯:關於 Android 程式設計
比較成熟的應用,幾乎都會在進入主界面前放一個啟動頁。它或簡單或華麗,或樸素或炫目,用處不同,風格自然也不同。先看幾個主流應用的啟動頁找找感覺。
以 QQ、新浪微博、UC 瀏覽器、神廟逃亡等七個應用為例:
個人覺得最耐看的是 QQ 2012:畫面裡只有一張圖,應用名稱、版本、圖標等基本資訊都在,但看起來舒服、乾淨。
啟動頁的本意,是用一個友善的介面掩蓋後台的緩衝與加載,讓使用者帶著平和的心情進入應用。但正因為它處在「開始」這個特殊位置,能做的事反而更多:應用宣傳、版權展示、廣告投放等等。
所以元素可多可少,取決於你的目的與需求。常見的有:
綜合來看,至少有這些價值:
從技術角度看,還能順手做這些事:
把能塞的元素統統塞進去,就得到下面這個只為表意、不講美感的佈局:
<?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 就能最簡單地實現。別的方法當然也行,但在這個場景下它最簡潔——不用它,還能用在哪?
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 MediaPlayer 的生命週期與狀態轉換全解析
MediaPlayer 的狀態轉換圖,其實就是它的生命週期圖。把這張圖吃透,寫出來的播放程式才會考慮得夠周全、夠有健壯性。 下面用幾張圖,一步步把整個生命週期演一遍: 圖
WoWoViewPager:讓 ViewPager 切換充滿動畫的開源庫
目錄 English READMEGradle備註Demo版本TodoLicense 動畫用法 基本動畫 位移動畫 縮放動畫 漸現、漸逝動畫 旋轉動畫 文字大小動畫
用 Xcode Targets 實作 iOS 多版本建置
開發過程中,我們往往需要在測試與正式發布之間維護兩套以上的環境。如果只是單純切換某個 API,用不同的巨集定義即可;但若要求不同版本共用相同的基礎程式碼,卻各自擁有圖示、
談談 Android 機型適配:語言、屏幕與 SDK 三大面向
機型適配的核心目的,在於讓 Android 應用能夠適應不同國家語言、型號、螢幕尺寸與 SDK 版本等多元的手机環境,同時保持主要功能與介面風格不變。手機適配主要包括三個