首先,在activity 類中(activity.java),我們可以看到兩個變數,分別是: private Window mWindow; private WindowManager mWindowManager;這兩個變數在attach函數中進行賦值, mWindow = PolicyManager.makeNewWindow(this); mWindow.setWindowManager(null, mToken, mComponent.flattenToString()); mWindowManager = mWindow.getWindowManager();查看PolicyManager.makeNewWindow的具體實現可以看到,返回的是PhoneWindow對象(Policy.java中實現)。PhoneWindow是Window的衍生類別。跟蹤setWindowManager我們可以得到WindowManager對象,並且這個對象是在系統唯一的,這個對象同樣被賦值給Window的成員變數mWindowManager。一個Activity包含一個PhoneWindow,所有UI都被包含在PhoneWindow中。 在PhoneWindow類中包含兩個和View相關成員變數,分別是 private DecorView mDecor; private ViewGroup mContentParent; 我們知道,在Android平台上,UI介面是通過View和ViewGroup分層樹進行定義的,如所示。 最頂層的是ViewGroup,而DecorView就是PhoneWindow的View架構最頂層的根,DecorView是FrameLayout的衍生類別。在installDecor(PhoneWindow.java)中對mContentParent進行賦值 mContentParent = generateLayout(mDecor); 在generateLayout函數中(PhoneWindow.java)有如下實現: View in = mLayoutInflater.inflate(layoutResource, null); decor.addView(in, new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT)); ViewGroup contentParent = (ViewGroup)findViewById(ID_ANDROID_CONTENT); return contentParent; mContentParent是從layoutResource中的布局xml中獲得的,所有的activity使用者新增加view都會被包含在這個對象當中。 我們在建立一個activity時,往往在OnCreate中調用setContentView(R.layout.main)定義UI介面,跟蹤setContentView發現實際上是調用了PhoneWindow的setContentView函數,在setContentView中,首先調用installDecor,對mDecor和mContentParent進行初始化,然後調用mLayoutInflater.inflate(layoutResID, mContentParent)從XML文中中產生相應的View並將使用者新增的view添加到mContentParent對象當中。這個過程中會調用View的onFinishInflate。 ViewRoot是Handler的衍生類別,在整個顯示系統中最為關鍵,在android的UI結構中扮演的是一個中間者的角色,串連PhoneWindow跟WindowManagerService. WindowManger維護了一個三元組View, ViewRoot, LayoutParams數組,在ActivityThread類的handleResumeActivity中,有如下代碼: r.window = r.activity.getWindow(); View decor = r.window.getDecorView(); decor.setVisibility(View.INVISIBLE); ViewManager wm = a.getWindowManager(); WindowManager.LayoutParams l = r.window.getAttributes(); a.mDecor = decor; l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION; l.softInputMode |= forwardBit; if (a.mVisibleFromClient) { a.mWindowAdded = true; wm.addView(decor, l); } 首先獲得WindowManger(通過跟蹤發現a.getWindowManager()返回WindowManger對象),然後調用WindowManger的addView方法(WindowManagerImpl中實現)將PhoneWindow的DecorView添加到WindowManger中,同時為它建立一個ViewRoot對象。在addView中,將進行如下調用 root.setView(view, wparams, panelParentView);在ViewRoot的setView中將調用requestLayout,在requestLayout中會調用scheduleTraversals,而scheduleTraversals只是簡單的發送處理訊息DO_TRAVERSAL;我們知道ViewRoot是一個handler,所以接下來訊息迴圈下一個將調用的就是ViewRoot的handleMessage。 public void handleMessage(Message msg) { switch (msg.what) { case DO_TRAVERSAL: if (mProfile) { Debug.startMethodTracing("ViewRoot"); } performTraversals(); if (mProfile) { Debug.stopMethodTracing(); mProfile = false; } break;performTraversals函數相當複雜,完成了View架構從上到下的繪製,函數調用流程圖如下: dispatchAttachedToWindow調用過程: 首先,判斷mFirst標誌位,只有mFirst為true,即第一次調用的時候才調用ViewRoot對應的View的dispatchAttachedToWindow,這個View就是PhoneWindow中的DecorView。我們知道DecorView繼承FrameLayout,是一個ViewGroup,dispatchAttachedToWindow實現如下: void dispatchAttachedToWindow(AttachInfo info, int visibility) { super.dispatchAttachedToWindow(info, visibility); visibility |= mViewFlags & VISIBILITY_MASK; final int count = mChildrenCount; final View[] children = mChildren; for (int i = 0; i < count; i++) { children[i].dispatchAttachedToWindow(info, visibility); } }View的dispatchAttachedToWindow中會調用onAttachedToWindow函數,各個控制項可以重寫onAttachedToWindow實現自己的操作。 由上述代碼可以知道,在View架構整個調用過程 ,如果是非分葉節點,首先調用父類的dispatchAttachedToWindow,然後調用子節點的dispatchAttachedToWindow;如果是分葉節點,則會調用View的dispatchAttachedToWindow,整個調用過程可以看成樹的先序遍曆。 measure調用過程: DecorView的measure函數,首先調用View的measure實現,measure中會調用FrameLayout重寫onMeasure,具體實現如下: protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { final int count = getChildCount(); int maxHeight = 0; int maxWidth = 0; // Find rightmost and bottommost child for (int i = 0; i < count; i++) { final View child = getChildAt(i); if (mMeasureAllChildren || child.getVisibility() != GONE) { measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0); maxWidth = Math.max(maxWidth, child.getMeasuredWidth()); maxHeight = Math.max(maxHeight, child.getMeasuredHeight()); } }measureChildWithMargins在ViewGroup中實現,會調用子控制項的measureprotected 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); }各個布局類和控制項類都實現了自己的onMeasure函數。布局類負責調用控制項的measure函數。 layout調用過程: 首先DecorView的layout調用實際上是執行View的layout調用,layout實現如下: public final void layout(int l, int t, int r, int b) { boolean changed = setFrame(l, t, r, b); if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) { if (ViewDebug.TRACE_HIERARCHY) { ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT); } onLayout(changed, l, t, r, b); mPrivateFlags &= ~LAYOUT_REQUIRED; } mPrivateFlags &= ~FORCE_LAYOUT;}各個控制項類和布局類會實現自己的onLayout,布局類的onLayout中,負責調用各個子控制項的layoutdraw過程首先調用ViewRoot中的draw(bool)函數,在這裡會計算需要繪製的canvas,然後將canvas作為參數傳入DecorView的draw中,通過跟蹤發現,首先調用View中的draw,主要進行如下步驟: * 1. Draw the background * 2. If necessary, save the canvas' layers to prepare for fading * 3. Draw view's content * 4. Draw children * 5. If necessary, draw the fading edges and restore layers * 6. Draw decorations (scrollbars for instance)第四步中,調用dispatchDraw(canvas)實現繪製子控制項,dispatchDraw在ViewGroup中實現,其中會對子控制項進行遍曆,然後調用drawChild(ViewGroup中實現)繪製子控制項,drawChild根據SKIP_DRAW標誌為選擇調用child.draw(canvas)繪製子控制項或者child.dispatchDraw(canvas)進行子空間的繪製分發。