整个View树的绘制流程是在ViewRoot.java类中的performTraversals()方法展开的,该函数的执行过程可简单概况为:根据之前设置的状态,判断是否需要重新计算视图大小(measure)、是否需要重新安置视图的位置(layout)、以及是否需要重新绘制(draw)。
流程一:mesarue()测量过程
主要作用:为整个View树计算实际的大小,即设置实际的高(对应属性:mMeasuredHeight)和宽(对应属性:mMeasureWidth),每个View控件的实际高宽都是由父视图和本身视图决定的。
具体的调用过程:ViewRoot的属性mView(其类型一般为ViewGroup类型)调用measure()方法去计算View树的大小,回调View/ViewGroup对象的onMeasure()方法,该方法实现的功能如下:
1、设置本View视图的最终大小,该功能的实现通过调用setMeasuredDimension()方法去设置实际的高(对应属性:mMeasuredHeight)和宽(对应属性:mMeasureWidth);
2、如果该View对象是ViewGroup类型,需要重写onMeasure()方法,对其子视图进行遍历的measure()过程。对每个子视图的measure()过程,是通过调用父类ViewGroup.java类里的measureChildWithMargins()方法去实现,该方法内部只是简单地调用了View对象的measure()方法。(由于measureChildWithMargins()方法只是一个过渡,更简单的做法是直接调用View对象的measure()方法)。
整个measrue()的调用流程就是个树形的递归过程。
measure()方法的源代码:
- This is called to find out how big a view should be. The parent supplies constraint information in the width and height parameters.
- The actual mesurement work of a view is performed in onMeasure(int,int), called by this method. Therefore, only onMeasure(int,int) can and must be
-
- overriden by subclasses.
-
- Parameters:
- widthMeasureSpec Horizontal space requirements as imposed by the parent
- heightMeasureSpec Vertical space requirements as imposed by the parent
- See also:
- onMeasure(int,int)
-
- public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
- if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
- widthMeasureSpec != mOldWidthMeasureSpec ||
- heightMeasureSpec != mOldHeightMeasureSpec) {
-
-
- mPrivateFlags &= ~MEASURED_DIMENSION_SET;
-
- if (ViewDebug.TRACE_HIERARCHY) {
- ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
- }
-
-
- onMeasure(widthMeasureSpec, heightMeasureSpec);
-
-
-
- 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;
- }
为了大家更好的理解,采用“二B程序员”的方式利用伪代码描述该measure流程
-
-
-
- private void performTraversals() {
-
-
- View mView;
- mView.measure(h,l);
-
-
- }
-
-
- private void onMeasure(int height , int width) {
-
-
- setMeasuredDimension(h , l);
-
-
- int childCount = getChildCount();
-
- for(int i = 0; i < childCount; i++){
-
- View child = getChildAt(i);
-
-
-
- measureChildWithMargins(child , h, i) ;
-
-
-
- }
- }
-
-
- protected void measureChildWithMargins(View v, int height , int width) {
- v.measure(h,l);
- }
流程二:layout()布局过程
主要作用:根据子视图的大小以及布局参数将View树放在合适的位置上。
具体的调用过程:host.layout()开始View树的布局,继而回调给View/ViewGroup类中的layout()方法,具体过程如下:
1、layout()方法会设置该View视图位于父视图的坐标轴,即mLeft、mTop、mLeft、mBottom(调用setFrame()函数去实现),接下来回调onLayout()方法(如果该View是ViewGroup对象,需要实现该方法,对每个子视图进行布局);
2、如果该View是ViewGroup类型,需要遍历每个子视图childView,调用该子视图的layout()方法去设置它的坐标值。
layout()方法的源代码:
- Assign a size and position to a view and all of its descendants
- This is the second phase of the layout mechanism. (The first is measuring). In this phase, each parent calls layout on all of its children to position them.
-
- This is typically done using the child measurements that were stored in the measure pass().
- Derived classes should not override this method. Derived classes with children should override onLayout. In that method, they should call layout on each
-
- of their children.
-
- Parameters:
- l Left position, relative to parent
- t Top position, relative to parent
- r Right position, relative to parent
- b Bottom position, relative to parent
-
- @SuppressWarnings({"unchecked"})
- public void layout(int l, int t, int r, int b) {
- int oldL = mLeft;
- int oldT = mTop;
- int oldB = mBottom;
- int oldR = mRight;
- 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;
-
- ListenerInfo li = mListenerInfo;
- if (li != null && li.mOnLayoutChangeListeners != null) {
- ArrayList<OnLayoutChangeListener> listenersCopy =
- (ArrayList<OnLayoutChangeListener>)li.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);
- }
- }
- }
- mPrivateFlags &= ~FORCE_LAYOUT;
- }
同样地,将上面layout()调用流程用伪代码描述如下:
-
-
-
- private void performTraversals(){
-
-
- View mView;
- mView.layout(left, top, right, bottom);
-
- }
-
-
- private void onLayout(int left, int top, right, bottom) {
-
-
-
- setFrame(l, t, r, b) ;
-
-
-
-
- int childCount = getChildCount();
- for(int i = 0; i < childCount; i++){
-
- View child = getChildAt(i);
-
- child.layout(l, t, r, b);
- }
- }
流程三:draw()绘制过程
由ViewRoot对象的 performTraversals()方法调用draw()方法发起绘制该View树的过程,值得注意的是每次发起绘图时,并不会重新绘制每个View树的视图,而只会重新绘制那些“需要重绘”的视图,View类内部变量包含了一个标志位DRAWN,当该视图需要重绘时,就会为该View添加该标志位。
具体的调用过程:mView.draw()开始绘制,draw()方法实现的功能如下:
1、绘制该View的背景;
2、为显示渐变框做一些准备操作(见5,大多数情况下,不需要改渐变框);
3、调用onDraw()方法绘制视图本身(每个View都需要重载该方法,ViewGroup不需要实现该方法);
4、dispatchDraw()方法内部会遍历每个子视图,调用drawChild()去重新回调每个子视图的draw()方法(注意,这个地方“需要重绘”的视图才会调用draw()方法)。如果该View类型不为ViewGroup,即不包含子视图,不需要重载该方法。值得说明的是,ViewGroup类已经为我们重写了dispatchDraw()的功能实现, 应用程序一般不需要重写该方法,但可以重载父类函数实现具体的功能;
5、绘制滚动条。
于是,整个调用过程就这样递归下去了。
draw()方法的源代码:
-
-
-
-
-
-
-
-
-
-
- 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;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 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);
- }
- }
- }
-
-
- final int viewFlags = mViewFlags;
- boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
- boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
- if (!verticalEdges && !horizontalEdges) {
-
-
- if (!dirtyOpaque) onDraw(canvas);
-
-
- dispatchDraw(canvas);
-
-
- onDrawScrollBars(canvas);
-
-
- return;
- }
-
-
-
-
-
-
-
-
- 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;
-
-
- 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;
-
-
-
- if (verticalEdges && (top + length > bottom - length)) {
- length = (bottom - top) / 2;
- }
-
-
- 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;
- }
-
- if (horizontalEdges) {
- leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
- drawLeft = leftFadeStrength * fadeHeight > 1.0f;
- rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
- drawRight = rightFadeStrength * 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);
- }
-
- if (drawLeft) {
- canvas.saveLayer(left, top, left + length, bottom, null, flags);
- }
-
- if (drawRight) {
- canvas.saveLayer(right - length, top, right, bottom, null, flags);
- }
- } else {
- scrollabilityCache.setFadeColor(solidColor);
- }
-
-
- if (!dirtyOpaque) onDraw(canvas);
-
-
- dispatchDraw(canvas);
-
-
- 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);
- }
-
- if (drawBottom) {
- matrix.setScale(1, fadeHeight * bottomFadeStrength);
- matrix.postRotate(180);
- matrix.postTranslate(left, bottom);
- fade.setLocalMatrix(matrix);
- canvas.drawRect(left, bottom - length, right, bottom, p);
- }
-
- if (drawLeft) {
- matrix.setScale(1, fadeHeight * leftFadeStrength);
- matrix.postRotate(-90);
- matrix.postTranslate(left, top);
- fade.setLocalMatrix(matrix);
- canvas.drawRect(left, top, left + length, bottom, p);
- }
-
- if (drawRight) {
- matrix.setScale(1, fadeHeight * rightFadeStrength);
- matrix.postRotate(90);
- matrix.postTranslate(right, top);
- fade.setLocalMatrix(matrix);
- canvas.drawRect(right - length, top, right, bottom, p);
- }
-
- canvas.restoreToCount(saveCount);
-
-
- onDrawScrollBars(canvas);
- }
同样地,将上面draw()调用流程用伪代码描述如下:
-
-
-
- private void draw(){
-
-
- View mView;
- mView.draw(canvas);
-
-
- }
-
-
- private void draw(Canvas canvas){
-
-
-
-
-
-
-
- }
-
-
- @Override
- protected void dispatchDraw(Canvas canvas) {
-
-
-
- int childCount = getChildCount();
- for(int i = 0; i < childCount; i++) {
- View child = getChildAt(i);
-
- drawChild(child,canvas);
- }
- }
-
-
- protected void drawChild(View child,Canvas canvas) {
-
-
-
- child.draw(canvas);
-
- }
需要强调一点的就是,在这三个流程中,Google已经帮我们把draw()过程框架已经写好了,自定义的ViewGroup只需要实现measure()过程和layout()过程即可。
引起View树重新绘制的因素有如下几种:
1、导致视图大小发生变化;
2、导致ViewGroup重新为子视图分配位置
3、视图显示情况发生变化需要重绘
这三种情况,最终会直接或间接调用到三个方法,分别为invalidate(),requsetLaytout()以及requestFocus() ,接着这三个方法最终会调用到ViewRoot中的schedulTraversale()方法,该函数然后发起一个异步消息,消息处理中调用performTraverser()方法对整个View进行遍历。
invalidate()方法:
说明:请求重绘View树,即draw()过程,假如视图大小没有发生变化就不会调用layout()过程,并且只绘制那些“需要重绘的”视图,即谁(View的话,只绘制该View;ViewGroup,则绘制整个ViewGroup)请求invalidate()方法,就绘制该视图。
一般引起invalidate()操作的函数如下:
1、调用invalidate()方法,请求重新draw(),但只会绘制调用者本身。
2、调用setSelection()方法,请求重新draw(),但只会绘制调用者本身。
3、调用setVisibility()方法:当View可视状态在INVISIBLE转换VISIBLE时,会间接调用invalidate()方法,继而绘制该View。
4、调用setEnabled()方法:请求重新draw(),但不会重新绘制任何视图包括该调用者本身。
requestLayout()方法:
会导致调用measure()过程和layout()过程。
说明:只是对View树重新布局layout过程包括measure()和layout()过程,不会调用draw()过程,但不会重新绘制任何视图包括该调用者本身。
一般引起invalidate()操作的函数如下:
1、requestLayout()方法:当View的可视状态在INVISIBLE / VISIBLE 转换为GONE状态时,会间接调用requestLayout() 和invalidate方法。同时,由于整个个View树大小发生了变化,会请求measure()过程以及draw()过程,同样地,只绘制需要“重新绘制”的视图。
requestFocus()方法:
说明:请求View树的draw()过程,但只绘制“需要重绘”的视图。
参考资料:
1、Android中View绘制流程以及invalidate()等相关方法分析 http://blog.csdn.net/qinjuning/article/details/7110211