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

深入解析 Android 的 ViewTree 與 DecorView 繪製機制

編輯:Android 開發實例

圖形使用者介面(GUI)是 Android 應用開發中不可或缺的一環。它不僅為使用者提供輸入管道,還能依據使用者執行的動作回饋相應的反應。因此,作為開發者,理解 UI 究竟是如何被建立與更新的,顯得格外重要。

ViewTree

View 與 ViewGroup 是構成 Android UI 的基礎單元。ViewGroup 作為容器,可以容納一組 View,而其本身也是 View 的延伸。來看源碼:

 public abstract class ViewGroup extends View implements ViewParent, ViewManager{} 

而像 TextView、Button 這類不同的 Widgets,同樣是 View 的延伸,只是它們被放進了各種 Layout 之中,例如 LinearLayout、RelativeLayout。Layout 本身則是 ViewGroup 的子類。因此,所謂的 ViewTree,其實就是由各種 View 與 ViewGroup 組合成的一棵樹狀結構。

空談無益,讓我們用實例說話。透過 Eclipse 的 Outline 視窗,可以觀察到下面這個樹狀布局:

XML 程式碼:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
  3. android:id="@+id/RelativeLayout1"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent" >
  6.  
  7. <View
  8. android:id="@+id/WhiteView"
  9. android:layout_width="200dp"
  10. android:layout_height="300dp"
  11. android:layout_marginLeft="20dp"
  12. android:background="#ffffff" />
  13.  
  14. <TextView
  15. android:id="@+id/RedText"
  16. android:layout_width="fill_parent"
  17. android:layout_height="wrap_content"
  18. android:layout_centerInParent="true"
  19. android:text="@string/hello"
  20. android:textColor="#ff0000" />
  21.  
  22. <LinearLayout
  23. android:layout_width="wrap_content"
  24. android:layout_height="wrap_content"
  25. android:layout_above="@+id/GrayView"
  26. android:layout_alignLeft="@+id/GrayView"
  27. android:layout_marginBottom="25dp"
  28. android:background="#0000ff"
  29. android:orientation="vertical" >
  30.  
  31. <TextView
  32. android:id="@+id/GreenText"
  33. android:layout_width="wrap_content"
  34. android:layout_height="wrap_content"
  35. android:text="TextView"
  36. android:textColor="#00ff00"
  37. android:textStyle="bold" />
  38.  
  39. <Button
  40. android:id="@+id/Button1"
  41. android:layout_width="wrap_content"
  42. android:layout_height="wrap_content"
  43. android:text="Button in LinearLayout" />
  44. </LinearLayout>
  45. <View
  46. android:id="@+id/RedView"
  47. android:layout_width="100dp"
  48. android:layout_height="100dp"
  49. android:layout_alignParentRight="true"
  50. android:layout_above="@+id/GrayView"
  51. android:layout_marginRight="10dp"
  52. android:layout_marginBottom="-10dp"
  53. android:background="#ff0000" />
  54. <View
  55. android:id="@+id/GrayView"
  56. android:layout_width="200dp"
  57. android:layout_height="300dp"
  58. android:layout_alignParentBottom="true"
  59. android:layout_alignParentRight="true"
  60. android:layout_marginRight="20dp"
  61. android:background="#cccccc" />
  62.  
  63. </RelativeLayout>

當我們在 Activity 中以 setContentView() 載入這個 view 並運行後,就會看到如下的渲染結果:

仔細對照 XML 檔與實際渲染出來的畫面,我們可以歸納出兩個關鍵結論:

1. ViewTree 是以一種自上而下的方式進行遍歷。

2. Parent 總是最先被繪製,其次才是 Children,且同樣遵循自上而下的順序。

因此在這個範例中,RelativeLayout 最先被繪製,接著是它的子節點 WhiteView、RedText 等等,直到最後的 GrayView 被繪製完成。而我們會發現,後繪製的元件覆蓋了部分先繪製的內容。

為了更直觀地理解這些 View 的繪製順序,我們把手機螢幕看作一個 X、Y、Z 三軸座標系:螢幕左上角為原點 [0, 0, 0],X 軸向右延伸,Y 軸沿著螢幕長度向下,Z 軸則垂直於螢幕向外延伸。

也就是說,當我們遍歷這棵樹時,這些 View 基本上是沿著 Z 軸逐層堆疊。這裡有個重要細節:當某個 View 的一部分被遮擋時,Android 便不會再繪製被遮擋的區域。以範例來說,灰色方塊遮住了部分紅色區域,Android 的繪圖機制就不會去渲染那塊被蓋住的紅色,因此我們看到的是直接的遮擋,而非顏色的混合。

了解 XML 中定義的 View 如何被繪製之後,我們還可以借助一個實用工具 Hierarchy Viewer,更深入地觀察頁面布局。

Hierarchy Viewer 位於 android-sdk/tools 資料夾下,在命令列中進入該目錄後執行 hierarchyviewer.bat 即可啟動。

下圖為 Hierarchy Viewer 的執行截圖:

在 Hierarchy Viewer 中,每一「列」代表樹的深度,而每一列裡的「行」數量則表示該層的節點數量。從圖上可以注意到,RelativeLayout 並非 Root 層級,而是 id 為 content 的 FrameLayout 的子元素。而我們呼叫 setContentView(View v) 時傳入的 View,實際上就是這個 content 視圖。

接著留意與 content 同層的 FrameLayout,它還有一個子 TextView,那其實就是 Activity 的 titleBar。那麼,如果我們移除這個 TitleBar,View Tree 又會變成什麼樣子呢?

做法是修改 Manifest 檔中 application 的主題:

 android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"

如此一來,再打開 Hierarchy Viewer,就會看到下圖:

(提醒:筆者使用的是 Android 2.2,若換成 4.1 版本,會無法得到下圖,中間還會多出一個 id 為 action_menu_bar_stub 的 ViewStub)

此時我們會發現,content FrameLayout 的父元素是 PhoneWindow$DecorView。

DecorView

在 Android 中,抽象類 Window 定義了最上層視窗的基本外觀與行為,它的實例會被加入 WindowManager,提供標準的 UI 策略,例如 Background、Titlebar 以及預設按鍵處理等;當然,這些屬性都可以透過 WindowManager.LayoutParams 進行客製化。

而前面提到的 PhoneWindow,正是 Window 抽象類唯一的實作,即 android.policy.PhoneWindow。DecorView 則是 PhoneWindow 的一個私有內部類,本質上是 FrameLayout 的延伸:

 private final class DecorView extends FrameLayout implements RootViewSurfaceTaker {}

正是這個類別構成了最上層的應用程式視圖。系統會依據我們在 Manifest 中設定的 Theme,或在 PhoneWindow 上設定的 Flags,來決定 DecorView 的 Layout。以本文範例而言,第一張圖使用了一個簡單的主題(在 Manifest 中定義),包含一個 titlebar 與 contentview,於是 PhoneWindow 便生成了包含 Title 的 LinearLayout,以及用來放置 content 的 FrameLayout;而在第二張圖中,我們移除了 titlebar 主題,因此它只生成了一個包含 FrameLayout 的 DecorView。

結論

最後,我們來總結一下:當一個 Activity 被啟動時,這棵視圖樹(View Tree)大致是這樣建立起來的:

1. PhoneWindow 依據 Manifest 的主題或特定的 PhoneWindow 設定,生成一個 DecorView 布局作為根視圖(Root View)。

2. Activity 呼叫 setContentView() 方法,將使用者自訂的 Layout XML 作為內容視圖(Content View),其內部實際上是呼叫 PhoneWindow 的 setContentView()。

3. 經過上述兩個步驟,UI 視圖便已成形。此後每當 UI 被刷新時,View Tree 就會如前所述的順序被遍歷繪製。

原文地址:https://www.shubhayu.com/android/android-viewtree-and-decorview

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