編輯:關於 Android 程式設計
View 樹是怎麼被走過一遭並畫出來的?invalidate() 最後會發起一次 View 樹遍歷的請求,由 performTraversal() 回應;而 performTraversal() 裡的主體邏輯就是判斷三件事:要不要重新測量(measure)、要不要重新配置(layout)、要不要重新繪製(draw)。三者有嚴格的先後順序:measure 是前提,因為 layout 需要用到 measure 算出來的尺寸,而 draw 需要 layout 決定的位置。本文聚焦其中最難理解的一段——measure。
寫 layout XML 時,layout_width 與 layout_height 有三種選項:具體數值、match_parent、wrap_content。如果 Google 當初規定只能填具體數值,那 measure 這一套機制根本沒有必要存在。正因為加入了 match_parent 與 wrap_content,版面才能變得靈活,而 measure 就是負責把這些相對描述換算成實際像素的過程。
public final void measure(int widthMeasureSpec, int heightMeasureSpec);
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec);
protected void measureChildren(int widthMeasureSpec, int heightMeasureSpec);
protected void measureChild(View child, int parentWidthMeasureSpec, int parentHeightMeasureSpec);
protected void measureChildWithMargins(View child, int parentWidthMeasureSpec, int widthUsed,
int parentHeightMeasureSpec, int heightUsed);
public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
widthMeasureSpec != mOldWidthMeasureSpec ||
heightMeasureSpec != mOldHeightMeasureSpec) {
// 先清掉「已設定測量尺寸」的標記
mPrivateFlags &= ~MEASURED_DIMENSION_SET;
// 測量自己,這一步應該會把標記設回來
onMeasure(widthMeasureSpec, heightMeasureSpec);
// 若標記沒被設回來,代表沒有呼叫 setMeasuredDimension(),丟出例外警告開發者
if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
throw new IllegalStateException("onMeasure() did not set the"
+ " measured dimension by calling"
+ " setMeasuredDimension()");
}
mPrivateFlags |= LAYOUT_REQUIRED;
}
mOldWidthMeasureSpec = widthMeasureSpec;
mOldHeightMeasureSpec = heightMeasureSpec;
}
final 修飾意味著 measure 的流程是固定的,不打算被子類覆寫;真正開放給子類客製的是 onMeasure()。另外注意開頭那個 if 條件:如果尺寸規格與上次相同、又沒有被標記為強制 layout,整個測量會被直接跳過,這是避免無謂重排的優化。
而 onMeasure() 的預設實作非常樸素:
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(
getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
}
protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
mMeasuredWidth = measuredWidth;
mMeasuredHeight = measuredHeight;
mPrivateFlags |= MEASURED_DIMENSION_SET;
}
這是最關鍵的一個函式。measure 的主要目的就是為 View 樹中的每一個 View 的 mMeasuredWidth 與 mMeasuredHeight 賦值;一旦這兩個變數被寫入,就代表該 View 的測量工作結束。上面 measure() 之所以要檢查那個 flag,就是為了確認 onMeasure 確實呼叫過它。
對於沒有子項的普通 View,預設的 measure → onMeasure 就足以完成測量。你也可以覆寫 onMeasure() 並自行呼叫 setMeasuredDimension() 指定任意大小,但一般不建議這麼做——原因見文末。而對於 ViewGroup 的子類,通常要覆寫 onMeasure() 來負責子項的測量,並且別忘了呼叫 setMeasuredDimension() 設定自己的尺寸。
measure() 的兩個參數是父 View 傳下來的「測量規格」。父 View 在測量子 View 時會傳入這兩個值,再結合子 View 自身的 LayoutParams,共同決定子 View 的最終規格——這個計算過程就在 measureChildWithMargins() 裡。
MeasureSpec 是個 int,高 2 位元存 specMode,低 30 位元存 specSize。specMode 有三種:
UNSPECIFIED——父 View 不施加任何限制,子 View 可以得到任意想要的大小(通常出現在 ScrollView 等可捲動容器中)。
EXACTLY——父 View 希望子 View 就是 specSize 指定的大小(對應 match_parent 或明確數值)。
AT_MOST——子 View 的大小最多只能是 specSize(對應 wrap_content)。
要强调的是,這些只是父 View「希望」的結果,子 View 最終尺寸仍取決於多方因素。
ViewGroup 提供了 measureChildren、measureChild、measureChildWithMargins 三個方法來測量子項。第一個只是迴圈呼叫第二個;後兩者的差別在於是否把 margin 與 padding 一併計入子 View 的尺寸。
protected void measureChildWithMargins(View child,
int parentWidthMeasureSpec, int widthUsed,
int parentHeightMeasureSpec, int heightUsed) {
final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
mPaddingLeft + mPaddingRight + lp.leftMargin + lp.rightMargin
+ widthUsed, lp.width);
final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
mPaddingTop + mPaddingBottom + lp.topMargin + lp.bottomMargin
+ heightUsed, lp.height);
child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
}
邏輯是:先把「父容器要扣掉的 padding、margin 與已被兄弟節點佔用的空間」加總成 padding 參數,再交給 getChildMeasureSpec() 換算。
public static int getChildMeasureSpec(int spec, int padding, int childDimension) {
int specMode = MeasureSpec.getMode(spec);
int specSize = MeasureSpec.getSize(spec);
int size = Math.max(0, specSize - padding);
int resultSize = 0;
int resultMode = 0;
switch (specMode) {
case MeasureSpec.EXACTLY:
if (childDimension >= 0) {
resultSize = childDimension;
resultMode = MeasureSpec.EXACTLY;
} else if (childDimension == LayoutParams.MATCH_PARENT) {
resultSize = size;
resultMode = MeasureSpec.EXACTLY;
} else if (childDimension == LayoutParams.WRAP_CONTENT) {
resultSize = size;
resultMode = MeasureSpec.AT_MOST;
}
break;
case MeasureSpec.AT_MOST:
if (childDimension >= 0) {
resultSize = childDimension;
resultMode = MeasureSpec.EXACTLY;
} else if (childDimension == LayoutParams.MATCH_PARENT) {
resultSize = size;
resultMode = MeasureSpec.AT_MOST;
} else if (childDimension == LayoutParams.WRAP_CONTENT) {
resultSize = size;
resultMode = MeasureSpec.AT_MOST;
}
break;
case MeasureSpec.UNSPECIFIED:
if (childDimension >= 0) {
resultSize = childDimension;
resultMode = MeasureSpec.EXACTLY;
} else if (childDimension == LayoutParams.MATCH_PARENT) {
resultSize = 0;
resultMode = MeasureSpec.UNSPECIFIED;
} else if (childDimension == LayoutParams.WRAP_CONTENT) {
resultSize = 0;
resultMode = MeasureSpec.UNSPECIFIED;
}
break;
}
return MeasureSpec.makeMeasureSpec(resultSize, resultMode);
}
整體思路是:從父 View 傳來的 MeasureSpec 解出 specMode 與 specSize,扣掉 padding 得到可用大小 size,再依 specMode 與子 View 在 XML 中宣告的 childDimension 組合出新的 MeasureSpec。若這個 View 自己還有子項,算出來的規格會作為呼叫子項 measure 的參數;若沒有子項,則最終會流入 setMeasuredDimension()。
從表格不難看出規律:只要子 View 明確給了數值(childDimension >= 0),結果一律是 EXACTLY;而 wrap_content 在父容器為 EXACTLY 或 AT_MOST 時,會被降級成 AT_MOST——也就是「你可以自己決定多大,但不能超過我」。
整條流程裡權力最大的是 View 的設計者,因為他可以透過 setMeasuredDimension(100, 100) 直接把尺寸寫死,此時父 View 傳來的規格與 XML 中設定的 layout_width、layout_height 都將完全失效。
這也是為什麼「自行覆寫 onMeasure 並硬寫數值」被視為不良示範:它剝奪了使用者的控制權,讓你的自訂 View 在任何容器裡都無法被正常配置。良好的設計會參照傳入的 MeasureSpec 來決定 mMeasuredWidth 與 mMeasuredHeight,尊重使用者的意圖——例如 wrap_content 時才採用自身內容的大小,EXACTLY 時則服從父容器的安排。
Android 自訂標題欄
Android 預設標題欄通常只有標題文字,樣式較單調。若要自訂標題欄,可先建立自訂版面,放入 TextView 或 Button,再透過 requestWindowFe
用自訂 ListView 打造 QQ 風格聯繫人列表
本文示範如何用自訂的 Adapter 實作一個類似 QQ 的聯繫人列表:每一行有頭像、暱稱與簽名,點擊頭像還會跳出提示。相較系統的 SimpleAdapter,自訂 Ad
公司網路連不上更新伺服器?用差異 patch 手動升級 Android Studio
公司機器的 Android Studio 很「時髦」,每次都準時收到官方穩定版推送,目前穩定版已經是 1.4.1,多了不少實用功能。而筆電上的這台還卡在 1.3.1,於是
實作桌面浮動歌詞視窗:WindowManager 與漸層渲染
隨著手機螢幕越來越大,浮動視窗的應用也越來普遍:音樂播放器的桌面歌詞、內存清理的小火箭、螢幕錄影的浮動按鈕,背後都是同一套 WindowManager 機制。本文以桌面歌