戲說Android view 工作流程
http://blog.csdn.net/aaa2832/article/details/7849400
遍歷View樹performTraversals()執行過程
view樹遍歷概述
還是回到ViewRoot.java,我們直接看performTraversals(),該函式就是android系統View樹遍歷工作的核心。一眼看去,發現這個函式挺長的,但是邏輯是非常清晰的,其執行過程可簡單概括為根據之前所有設定好的狀態,判斷是否需要計算檢視大小(measure)、是否需要重新安置檢視的位置(layout),以及是否需要重繪(draw)檢視,可以用以下圖來表示該流程。
- private void performTraversals() {
- // cache mView since it is used so much below...
- //1 處理mAttachInfo的初始化,並根據resize、visibility改變的情況,給相應的變數賦值。
- final View host = mView;
- final View.AttachInfo attachInfo = mAttachInfo;
- final int viewVisibility = getHostVisibility();
- boolean viewVisibilityChanged = mViewVisibility != viewVisibility
- || mNewSurfaceNeeded;
- float appScale = mAttachInfo.mApplicationScale;
- WindowManager.LayoutParams params = null;
- if (mWindowAttributesChanged) {
- mWindowAttributesChanged = false;
- surfaceChanged = true;
- params = lp;
- }
- Rect frame = mWinFrame;
- if (mFirst) {
- // For the very first time, tell the view hierarchy that it
- // is attached to the window. Note that at this point the surface
- // object is not initialized to its backing store, but soon it
- // will be (assuming the window is visible).
- attachInfo.mSurface = mSurface;
- attachInfo.mUse32BitDrawingCache = PixelFormat.formatHasAlpha(lp.format) ||
- lp.format == PixelFormat.RGBX_8888;
- attachInfo.mHasWindowFocus = false;
- attachInfo.mWindowVisibility = viewVisibility;
- ......
- }
- //2 如果mLayoutRequested判斷為true,那麼說明需要重新layout,不過在此之前那必須重新measure。
- if (mLayoutRequested) {
- // Execute enqueued actions on every layout in case a view that was detached
- // enqueued an action after being detached
- getRunQueue().executeActions(attachInfo.mHandler);
- if (mFirst) {
- ......
- }
- }
- //3 判斷是否有子檢視的屬性發生變化,ViewRoot需要獲取這些變化。
- if (attachInfo.mRecomputeGlobalAttributes) {
- ......
- }
- if (mFirst || attachInfo.mViewVisibilityChanged) {
- ......
- }
- //4 根據上面得到的變數數值,確定我們的view需要多大尺寸才能裝下。於是就得measure了,有viewgroup的weight屬性還得再做些處理
- // Ask host how big it wants to be
- host.measure(childWidthMeasureSpec, childHeightMeasureSpec);
- mLayoutRequested = true;
- }
- }
- //5 measure完畢,接下來可以layout了。
- final boolean didLayout = mLayoutRequested;
- boolean triggerGlobalLayoutListener = didLayout
- || attachInfo.mRecomputeGlobalAttributes;
- if (didLayout) {
- host.layout(0, 0, host.mMeasuredWidth, host.mMeasuredHeight);
- }
- //6 如果mFirst為true,那麼會進行view獲取焦點的動作。
- if (mFirst) {
- mRealFocusedView = mView.findFocus();
- }
- boolean cancelDraw = attachInfo.mTreeObserver.dispatchOnPreDraw();
- //7 終於,來到最後一步,前面的工作可以說都是鋪墊,都是為了draw而準備的。
- if (!cancelDraw && !newSurface) {
- mFullRedrawNeeded = false;
- draw(fullRedrawNeeded);
- }
下面我們就來會會view那三部曲吧。
計算檢視大小(measure)的過程
整個view檢視的Measure過程就是一個量體裁衣,按需分配的過程。看一下以下的遞迴過程:
從上圖可以看出,measure過程始於ViewRoot的host.measure(),調的就是view類的measure()函式,該函式然後回撥onMeasure。如果host物件是一個ViewGroup例項,一般會過載onMeasure,如果沒有的話,則會執行view類中預設的onMeasure。合理的情況是程式設計人員過載onMeasure並逐一對裡面的子view進行measure。我們可以看一下view的measure方法:
- /**
- * 該方法在需要確定view所需尺寸大小時呼叫,父檢視會提供寬和高的屬性約束。
- * 具體檢視完全可以在onMeasure中改變這些。
- * @see #onMeasure(int, int)
- */
- public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
- if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
- widthMeasureSpec != mOldWidthMeasureSpec ||
- heightMeasureSpec != mOldHeightMeasureSpec) {
- // 首先清除dimension的設值
- mPrivateFlags &= ~MEASURED_DIMENSION_SET;
- // measure 自己, 並設定dimension
- onMeasure(widthMeasureSpec, heightMeasureSpec);
- // 丟擲未設值flag的異常
- 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;
- }
這裡強烈建議去看看viewGroup例項FrameLayout和LinearLayout的onMeasure方法,一定會有所感悟的,尤其是LinerLayout的。這樣對於viewGroup的專用標籤pading和weight也會有新的體會。
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="100dip"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="20dip"
- android:layout_weight="2"
- android:text="@string/hello"
- />
- <ListView
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="#ff00ff00"
- ></ListView>
- </LinearLayout>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="100dip"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="60dip"
- android:layout_weight="2"
- android:text="@string/hello"
- />
- <ListView
- android:layout_width="fill_parent"
- android:layout_height="0dip"
- android:layout_weight="2"
- android:background="#ff00ff00"
- ></ListView>
- </LinearLayout>
請問以上兩佈局有無不同,能否自行畫出?
佈局(layout)過程
執行完measure過程,也就是說各個view的大小尺寸已經登記在案,現在它們要確定的是自己應該置身於何處,也就是擺放在哪裡。好吧,這個就是layout的職責所在,讓父檢視按照子檢視的大小及佈局引數,將子檢視放置在合適的位置上。
同樣需要看下以下流程圖:
- public void layout(int l, int t, int r, int b) {
- int oldL = mLeft;
- int oldT = mTop;
- int oldB = mBottom;
- int oldR = mRight;
- //呼叫setFrame()函式給當前檢視設定引數中指定的位置
- 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()函式
- onLayout(changed, l, t, r, b);
- mPrivateFlags &= ~LAYOUT_REQUIRED;
- //4.0新增監聽可捕獲layout變化
- if (mOnLayoutChangeListeners != null) {
- ArrayList<OnLayoutChangeListener> listenersCopy =
- (ArrayList<OnLayoutChangeListener>) mOnLayoutChangeListeners.clone();
- int numListeners = listenersCopy.size();
- for (int i = 0; i < numListeners; ++i) {
- listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
- }
- }
- }
- //layout完成,清楚標籤
- mPrivateFlags &= ~FORCE_LAYOUT;
- }
view中的該layout函式流程大概如下:
1,呼叫setFrame()將位置引數儲存,這些引數會儲存到view內部變數(mLeft,mTop,mRight,mButtom)。如果有數值的變化那麼會呼叫invalidate()重繪那些區域。
2,回撥onLayout(),View中定義的onLayout()函式預設什麼都不做,View系統提供onLayout函式的目的是為了使系統包含有子檢視的父檢視能夠在onLayout()函式對子檢視進行位置分配,正因為這樣,如果是viewGroup型別,就必須過載onLayout(),由此ViewGroup的onLayout為abstract也就很好解釋了。
3,清楚mPrivateFlags中的LAYOUT_REQUIRED標記,因為layout的操作已經完成了。
為了對layout過程有更深的體會,有必要針對特定的一種viewGroup進行分析,我們還是把魔爪伸向linearLayout,看看它的onMeasure,onMeasure中會根據mOrientation變數,進行不同的layout,我們看一種就行了:
- void layoutVertical() {
- final int paddingLeft = mPaddingLeft;
- int childTop;
- int childLeft;
- // 獲得子檢視可以用的寬度,順便也把子檢視左邊沿的位置計算出來。
- final int width = mRight - mLeft;
- int childRight = width - mPaddingRight;
- // Space available for child
- int childSpace = width - paddingLeft - mPaddingRight;
- final int count = getVirtualChildCount();
- //根據父檢視中的gravity屬性,決定子檢視的起始位置。
- final int majorGravity = mGravity & Gravity.VERTICAL_GRAVITY_MASK;
- final int minorGravity = mGravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK;
- switch (majorGravity) {
- case Gravity.BOTTOM:
- // mTotalLength contains the padding already
- childTop = mPaddingTop + mBottom - mTop - mTotalLength;
- break;
- // mTotalLength contains the padding already
- case Gravity.CENTER_VERTICAL:
- childTop = mPaddingTop + (mBottom - mTop - mTotalLength) / 2;
- break;
- case Gravity.TOP:
- default:
- childTop = mPaddingTop;
- break;
- }
- //遍歷所有子檢視,為它們分配位置.setChildFrame()結果還是會呼叫child.layout()為子檢視設定佈局位置.
- for (int i = 0; i < count; i++) {
- final View child = getVirtualChildAt(i);
- if (child == null) {
- childTop += measureNullChild(i);
- ......
- childTop += lp.topMargin;
- setChildFrame(child, childLeft, childTop + getLocationOffset(child),
- childWidth, childHeight);
- childTop += childHeight + lp.bottomMargin + getNextLocationOffset(child);
- i += getChildrenSkipCount(child, i);
- }
- }
- }
繪製(draw)過程
這是見證奇蹟的時刻,draw過程就是要把view物件繪製到螢幕上,如果它是一個viewGroup,則需要遞迴繪製它所有的子檢視。檢視中有哪些元素是需要繪製的呢?
1,view背景。所有view都會有一個背景,可以是一個顏色值,也可以是一張背景圖片
2,檢視本身內容。比如TextView的文字
3,漸變邊框。就是一個shader物件,讓檢視看起來更具有層次感
4,滾動條。
按照慣例,還是先看一下繪製的總體流程吧:
眼尖的同學應該發現了,上面這張圖比前面的measure和layout多了一步:draw()。在performTraversals()函式中呼叫的是viewRoot的draw()函式,在該函式中進行一系列的前端處理後,再呼叫host.draw()。
一般情況下,View物件不應該過載draw()函式,因此,host.draw()就是view.draw()。該函式內部過程也就是View系統繪製過程的核心過程,該函式中會依次繪製前面所說哦四種元素,其中繪製檢視本身的具體實現就是回撥onDraw()函式,應用程式一般也會過載onDraw()函式以繪製所設計的View的真正介面內容。
Google原始碼的註釋太nice了,我加任何說辭都顯得多餘,就不畫蛇添足了:
- public void draw(Canvas canvas) {
- if (ViewDebug.TRACE_HIERARCHY) {
- ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
- }
- final int privateFlags = mPrivateFlags;
- final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
- (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
- mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
- /*
- * Draw traversal performs several drawing steps which must be executed
- * in the appropriate order:
- *
- * 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)
- */
- // Step 1, draw the background, if needed
- int saveCount;
- if (!dirtyOpaque) {
- final Drawable background = mBGDrawable;
- if (background != null) {
- final int scrollX = mScrollX;
- final int scrollY = mScrollY;
- if (mBackgroundSizeChanged) {
- background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
- mBackgroundSizeChanged = false;
- }
- if ((scrollX | scrollY) == 0) {
- background.draw(canvas);
- } else {
- canvas.translate(scrollX, scrollY);
- background.draw(canvas);
- canvas.translate(-scrollX, -scrollY);
- }
- }
- }
- // skip step 2 & 5 if possible (common case)
- final int viewFlags = mViewFlags;
- boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
- boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
- if (!verticalEdges && !horizontalEdges) {
- // Step 3, draw the content
- if (!dirtyOpaque) onDraw(canvas);
- // Step 4, draw the children
- dispatchDraw(canvas);
- // Step 6, draw decorations (scrollbars)
- onDrawScrollBars(canvas);
- // we're done...
- return;
- }
- /*
- * Here we do the full fledged routine...
- * (this is an uncommon case where speed matters less,
- * this is why we repeat some of the tests that have been
- * done above)
- */
- boolean drawTop = false;
- boolean drawBottom = false;
- boolean drawLeft = false;
- boolean drawRight = false;
- float topFadeStrength = 0.0f;
- float bottomFadeStrength = 0.0f;
- float leftFadeStrength = 0.0f;
- float rightFadeStrength = 0.0f;
- // Step 2, save the canvas' layers
- int paddingLeft = mPaddingLeft;
- final boolean offsetRequired = isPaddingOffsetRequired();
- if (offsetRequired) {
- paddingLeft += getLeftPaddingOffset();
- }
- int left = mScrollX + paddingLeft;
- int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
- int top = mScrollY + getFadeTop(offsetRequired);
- int bottom = top + getFadeHeight(offsetRequired);
- if (offsetRequired) {
- right += getRightPaddingOffset();
- bottom += getBottomPaddingOffset();
- }
- final ScrollabilityCache scrollabilityCache = mScrollCache;
- final float fadeHeight = scrollabilityCache.fadingEdgeLength;
- int length = (int) fadeHeight;
- // clip the fade length if top and bottom fades overlap
- // overlapping fades produce odd-looking artifacts
- if (verticalEdges && (top + length > bottom - length)) {
- length = (bottom - top) / 2;
- }
- // also clip horizontal fades if necessary
- if (horizontalEdges && (left + length > right - length)) {
- length = (right - left) / 2;
- }
- if (verticalEdges) {
- topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
- drawTop = topFadeStrength * fadeHeight > 1.0f;
- bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
- drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
- }
- saveCount = canvas.getSaveCount();
- int solidColor = getSolidColor();
- if (solidColor == 0) {
- final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
- if (drawTop) {
- canvas.saveLayer(left, top, right, top + length, null, flags);
- }
- if (drawBottom) {
- canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
- }
- } else {
- scrollabilityCache.setFadeColor(solidColor);
- }
- // Step 3, draw the content
- if (!dirtyOpaque) onDraw(canvas);
- // Step 4, draw the children
- dispatchDraw(canvas);
- // Step 5, draw the fade effect and restore layers
- final Paint p = scrollabilityCache.paint;
- final Matrix matrix = scrollabilityCache.matrix;
- final Shader fade = scrollabilityCache.shader;
- if (drawTop) {
- matrix.setScale(1, fadeHeight * topFadeStrength);
- matrix.postTranslate(left, top);
- fade.setLocalMatrix(matrix);
- canvas.drawRect(left, top, right, top + length, p);
- }
- 。。。。。
- canvas.restoreToCount(saveCount);
- // Step 6, draw decorations (scrollbars)
- onDrawScrollBars(canvas);
- }
繪製完介面內容後,如果該檢視還包含子檢視,則呼叫dispatchDraw()函式,實際上起作用的還是viewGroup的dispatchDraw()函式。需要說明的是應用程式不應該再過載ViewGroup中該方法,因為它已經有了預設而且標準的view系統流程。dispatchDraw()內部for迴圈呼叫drawChild()分別繪製每一個子檢視,而drawChild()內部又會呼叫draw()函式完成子檢視的內部繪製工作。
- protected void dispatchDraw(Canvas canvas) {
- final int count = mChildrenCount;
- final View[] children = mChildren;
- int flags = mGroupFlags;
- //1 判斷mGroupFlags是否設有FLAG_RUN_ANIMATION標識並且不為0.該layout動畫指的是載入或移除子檢視時候呈現的動畫.
- if ((flags & FLAG_RUN_ANIMATION) != 0 && canAnimate()) {
- final boolean cache = (mGroupFlags & FLAG_ANIMATION_CACHE) == FLAG_ANIMATION_CACHE;
- final boolean buildCache = !isHardwareAccelerated();//硬體加速,4.0加入.
- for (int i = 0; i < count; i++) {
- final View child = children[i];
- if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) {
- final LayoutParams params = child.getLayoutParams();
- attachLayoutAnimationParameters(child, params, i, count);
- bindLayoutAnimation(child);
- }
- }
- //2 處理padding屬性,如果該viewGroup有設定.
- int saveCount = 0;
- final boolean clipToPadding = (flags & CLIP_TO_PADDING_MASK) == CLIP_TO_PADDING_MASK;
- if (clipToPadding) {
- saveCount = canvas.save();
- canvas.clipRect(mScrollX + mPaddingLeft, mScrollY + mPaddingTop,
- mScrollX + mRight - mLeft - mPaddingRight,
- mScrollY + mBottom - mTop - mPaddingBottom);
- }
- //3 開始繪製子檢視動畫之前先清除flag.
- // We will draw our child's animation, let's reset the flag
- mPrivateFlags &= ~DRAW_ANIMATION;
- mGroupFlags &= ~FLAG_INVALIDATE_REQUIRED;
- boolean more = false;
- final long drawingTime = getDrawingTime();
- //4 使用佛如迴圈,使viewGroup的子檢視逐個呼叫drawChild函式.
- if ((flags & FLAG_USE_CHILD_DRAWING_ORDER) == 0) {
- for (int i = 0; i < count; i++) {
- final View child = children[i];
- if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null) {
- more |= drawChild(canvas, child, drawingTime);
- }
- }
- } else {
- for (int i = 0; i < count; i++) {
- final View child = children[getChildDrawingOrder(count, i)];
- if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null) {
- more |= drawChild(canvas, child, drawingTime);
- }
- }
- }
- //5 Draw any disappearing views that have animations
- if (mDisappearingChildren != null) {
- final ArrayList<View> disappearingChildren = mDisappearingChildren;
- final int disappearingCount = disappearingChildren.size() - 1;
- // Go backwards -- we may delete as animations finish
- for (int i = disappearingCount; i >= 0; i--) {
- final View child = disappearingChildren.get(i);
- more |= drawChild(canvas, child, drawingTime);
- }
- }
- if (clipToPadding) {
- canvas.restoreToCount(saveCount);
- }
- // mGroupFlags might have been updated by drawChild()
- flags = mGroupFlags;
- if ((flags & FLAG_INVALIDATE_REQUIRED) == FLAG_INVALIDATE_REQUIRED) {
- invalidate(true);
- }
- }
整個view的工作流程那是相當複雜的,這需要靜下心來,加以時日細細揣摩才能略有體會,本人寫有小Demo一個,就當拋磚引玉:ClickMe!
相關文章
- View 體系詳解:View 的工作流程View
- Android View繪製流程AndroidView
- View工作流程-相關學習View
- Android View 的工作原理AndroidView
- Android View的工作原理(上)AndroidView
- 初·Android View的繪製流程AndroidView
- 【Android原始碼】View的建立流程Android原始碼View
- Android 中 View 繪製流程分析AndroidView
- Android進階(五)View繪製流程AndroidView
- Android View 佈局流程(Layout)完全解析AndroidView
- Android View 繪製流程(Draw) 完全解析AndroidView
- 戲說HTML5HTML
- Android原始碼分析之View繪製流程Android原始碼View
- Android View 測量流程(Measure)完全解析AndroidView
- 【Android原始碼】View的繪製流程分析Android原始碼View
- 深入解析 Android 中 View 的工作原理AndroidView
- 探究 Android View 繪製流程,Activity 的 View 如何展示到螢幕AndroidView
- Android自定義View之Window、ViewRootImpl和View的三大流程AndroidView
- 探究Android View 繪製流程,Canvas 的由來AndroidViewCanvas
- 深入理解 Android 之 View 的繪製流程AndroidView
- Android View的Measure測量流程全解析AndroidView
- View draw流程分析View
- Android View繪製流程看這篇就夠了AndroidView
- Android開發藝術(3)——View的工作原理AndroidView
- Android自定義View之(一)View繪製流程詳解——向原始碼要答案AndroidView原始碼
- 探究Android View 繪製流程,Xml 檔案到 View 物件的轉換過程AndroidViewXML物件
- Android檢視載入流程(6)之View的詳細繪製流程DrawAndroidView
- View 繪製流程分析View
- # 自定義view————流程位置View
- Android高階進階之路【一】Android中View繪製流程淺析AndroidView
- View的工作原理View
- 馳騁工作流引擎-父子流程設計說明
- Android系統原始碼分析--View繪製流程之-inflateAndroid原始碼View
- Android View繪製原理:繪製流程排程、測算等AndroidView
- View的繪製二:View的繪製流程View
- Android ViewAndroidView
- Android自定義View:View(二)AndroidView
- 高速輸出-我們戲說快取快取