編輯:關於 Android 程式設計
怎樣寫出一個優秀的 Android App,是每個開發者追求的目標。怎麼寫?答案聽起來很樸素:細節決定成敗。今天就從最基礎的 XML 佈局出發,聊聊效能這件事。
碰到比較複雜的介面,最直覺的做法就是多層嵌套硬堆出來。但最簡單的做法,往往不是最好的做法。下面我們拿微信「發現」頁的佈局當範例,實際走一遍優化流程。

看到這張圖,多數人第一反應是「線性佈局真好用」——左圖右文、一行白底,nested 幾層就搞定了。於是我們先寫出直覺版本:
<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:background="@color/color_eeeeee"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="@drawable/item_bg_select"
android:clickable="true"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingLeft="20dp"
android:paddingRight="20dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/afe" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:text="@string/fiends"
android:textColor="@android:color/black"
android:textSize="16sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="@android:color/white"
android:orientation="vertical">
<!-- 同結構重複: afg / scan、afh / shake -->
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="@android:color/white"
android:orientation="vertical">
<!-- 同結構重複: afd / nearby、afb / float_bottle -->
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="@android:color/white"
android:orientation="vertical">
<!-- 同結構重複: agg / shopping、ak6 / games -->
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableTop="@drawable/ala"
android:gravity="center"
android:text="@string/weixin"
android:textColor="@color/color_9e9e9e" />
<!-- countans / finds / me 同結構 -->
</LinearLayout>
</LinearLayout>
效果如下:

視覺上幾乎和微信一致。但「能跑」跟「寫得好」是兩件事,怎麼判斷?靠工具。
用 Android Studio 的話,路徑是 Tools → Android → Android Device Monitor → Hierarchy View。它能直接呈現當前頁面的檢視樹:
<RelativeLayout 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:background="@color/color_eeeeee"
tools:context=".MainActivity">
<TextView
android:id="@+id/tv_one"
style="@style/textStyle"
android:layout_marginTop="20dp"
android:background="@drawable/item_bg_select"
android:drawableLeft="@drawable/afe"
android:text="@string/fiends" />
<TextView
style="@style/textStyle"
android:background="@drawable/item_bg_select"
android:drawableLeft="@drawable/afg"
android:text="@string/scan" />
<!-- shake / nearby / float_bottle / shopping / games 同結構 -->
<include
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
layout="@layout/bottom_layout" />
</RelativeLayout>
程式碼量大幅下降,可讀性也好了不少。具體做了哪些事?
<!-- TextView 通用屬性 -->
<style name="textStyle">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textSize">16sp</item>
<item name="android:textColor">@android:color/black</item>
<item name="android:paddingRight">20dp</item>
<item name="android:paddingLeft">20dp</item>
<item name="android:gravity">center_vertical</item>
<item name="android:clickable">true</item>
</style>
<!-- 容器通用屬性 -->
<style name="LinerLayoutStyle">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_marginTop">20dp</item>
<item name="android:background">@android:color/white</item>
<item name="android:orientation">vertical</item>
</style>
TextView 本身支援四方向 drawable,直接取代「LinearLayout 包一個 ImageView 加一個 TextView」的組合,省下一層;外層再換成 RelativeLayout,又省一層。

不只層數少了兩層,控制項總數也跟著下降,invalidate 與 measure 的時間自然縮短。
以 divider 屬性處理分隔線,省掉那些只有 0.5dp 高的空 View。具體做法可參考 AndroidX 相關文件。
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableTop="@drawable/ala"
android:gravity="center"
android:text="@string/weixin"
android:textColor="@color/color_9e9e9e" />
<!-- 其餘三項同結構 -->
</LinearLayout>
手機開啟「設定 → 開發者選項 → 顯示 GPU 過度繪製」,畫面會用顏色標示重複繪製的區域,從好到壞依次是:藍、綠、淡紅、紅。

原則很簡單:盡量讓紅色消失,讓藍色變多。對比一下:


第一張是微信原版,第二張是我們的版本,兩者過度繪製的情況差不多。那還能不能再往下壓?試試看。
先拿掉最外層 RelativeLayout 的背景:
android:background="@color/color_eeeeee"
再調整 item 的 selector。原本的 item_bg_select.xml:
<selector xmlns:android="https://schemas.android.com/apk/res/android">
<item android:drawable="@color/color_e0e0e0" android:state_pressed="true" />
<item android:drawable="@android:color/white" />
</selector>
改成 item_bg_select1.xml:
<selector xmlns:android="https://schemas.android.com/apk/res/android">
<item android:drawable="@color/color_e0e0e0" android:state_pressed="true" />
</selector>
差別在於移除了 <item android:drawable="@android:color/white" />——既然整頁背景已是白色,就沒有必要再逐項重設一次正常態底色。這正是 Overdraw 的來源之一:同一個像素被反覆填色。
修改後的效果:

紅色區域基本消失,繪製效率明顯提升。
有些內容只在特定操作後才需要,例如小米新增聯絡人時那個「更多資料」的下拉:


遇到這種情況,直覺做法是設 INVISIBLE 或 GONE。但 INVISIBLE 只是看不見,它照樣佔位、照樣參與繪製,時間依舊要花。GONE 好一些,物件仍會被建立。
更好的選擇是 ViewStub。它是 Android 提供的一種極輕量控制項:沒有尺寸、不具備繪製能力、不參與佈局,資源消耗極低,放在佈局裡幾乎可以視為零成本。
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp">
<EditText
android:id="@+id/et_name"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:drawableRight="@drawable/a0e"
android:hint="@string/name" />
<ViewStub
android:id="@+id/view_stub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout="@layout/item_name" />
</LinearLayout>
item_name.xml:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/et_name1"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:hint="@string/name1" />
<!-- name2 / name3 / name4 同結構 -->
</LinearLayout>
使用時呼叫 inflate() 才會真正載入:
findViewById(R.id.et_name).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ViewStub viewStub = (ViewStub) findViewById(R.id.view_stub);
if (null != viewStub) {
View view = viewStub.inflate();
EditText name1 = (EditText) view.findViewById(R.id.et_name1);
EditText name2 = (EditText) view.findViewById(R.id.et_name2);
EditText name3 = (EditText) view.findViewById(R.id.et_name3);
EditText name4 = (EditText) view.findViewById(R.id.et_name4);
}
}
});


點擊姓名欄的下拉按鈕後,其餘欄位才載入,而且顯示位置落在 ViewStub 原本所處的位置,不會錯位。
最後補一個工具:Lint。它會掃描專案中的不合理寫法、佈局冗餘、重複資源與重複圖片,幫你找出可以繼續壓縮的空間。Android Studio 使用者可從 Analyze → Inspect Code 進入。
佈局優化沒有單一絕招,它是「少嵌套、少重複繪製、少無用載入」這三件事的疊加:用 style 與 include 去重,用 RelativeLayout 與 compound drawable 壓平層級,用 ViewStub 延後成本,再用 Hierarchy View 和 GPU 過度繪製這兩個工具驗證成效。改完之後對照原版,你會發現差距是看得見的。
Android 屬性動畫深度剖析:從傳統動畫的先天缺陷說起
一句話說清主題 做產品時,恰到好處的動畫能顯著提升用戶體驗,平移、縮放、旋轉都是常見手法。這類動畫在 Android 早期版本就已具備,我們習慣稱之為「傳統動畫」,它分為
Android 自定義 ViewGroup 實作 FlowLayout 流式版面配置(三)
本篇延續自定義 ViewGroup 的系列主題,動手做一個實用的範例:FlowLayout(流式版面配置)。 所謂 FlowLayout,指的是子控制項會依序往右排入容器
Android Studio 實用指南:文本搜索工具
Android Studio 提供強大的文本搜索功能,可在編輯器或日志窗口中使用。Windows/Linux 可使用 Ctrl+F,macOS 可使用 Command+F
Cocos2d-x 移植 Android 常見問題:中文亂碼的成因與解法
Android 平台的版本與裝置碎片化相當嚴重,因此從 Win32 平台移植到 Android 平台時會遇到不少問題。以下是我們歸納的移植過程中常見的狀況之一:中文亂碼。