标签:
在搜集Android view绘制流程的相关知识时,发现这里面的流程还是有些复杂的,准备了好几天,才敢提起笔来。下面就直入主题吧!
view绘制流程是从ViewRoot的performTraversals()方法中开始的,在该方法中会执行view绘制的三部曲,即:measure(测量视图的大小),layout(确定视图的位置)draw(绘制视图的内容)。下面这张图明确的展示了该过程:
public final void measure(int widthMeasureSpec, int heightMeasureSpec) { ... onMeasure(widthMeasureSpec, heightMeasureSpec); ... }可以看到该方法是final的,所以不需要子类重写,里面的实现主要就是调用了onMeasure。那么传入的两个参数是什么呢?那就涉及到MeasureSpec了,MeasureSpec由specMode(规格)和specSize(大小)组成,规格有三种,它跟大小对应关系如下:
private int getRootMeasureSpec(int windowSize, int rootDimension) { int measureSpec; switch (rootDimension) { case ViewGroup.LayoutParams.MATCH_PARENT: measureSpec = MeasureSpec.makeMeasureSpec(windowSize, MeasureSpec.EXACTLY); break; case ViewGroup.LayoutParams.WRAP_CONTENT: measureSpec = MeasureSpec.makeMeasureSpec(windowSize, MeasureSpec.AT_MOST); break; default: measureSpec = MeasureSpec.makeMeasureSpec(rootDimension, MeasureSpec.EXACTLY); break; } return measureSpec; }这个函数传入的参数是窗口大小和MATCH_PARENT,这就是为什么根视图总是铺满屏幕的原因。
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec), getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec)); }onMeasure里面主要是使用setMeasuredDimension来设置视图的大小,这样就完成了一次measure的过程,当然,一个布局中一般都会包含多个子视图,每个子视图都需要经历一次measure过程。
protected void measureChildren(int widthMeasureSpec, int heightMeasureSpec) { final int size = mChildrenCount; final View[] children = mChildren; for (int i = 0; i < size; ++i) { final View child = children[i]; if ((child.mViewFlags & VISIBILITY_MASK) != GONE) { measureChild(child, widthMeasureSpec, heightMeasureSpec); } } }里面循环调用了measureChild,其实现为:
protected void measureChild(View child, int parentWidthMeasureSpec, int parentHeightMeasureSpec) { ... child.measure(childWidthMeasureSpec, childHeightMeasureSpec); }这里面又调用到了view的measure方法,所以这其实是个递归调用,不断的去测量设置子视图的大小,直至全部测完。
public void layout(int l, int t, int r, int b) { ... setFrame(l, t, r, b); ... onLayout(changed, l, t, r, b); ... }主要是调用了setFrame(用来设置坐标)和onLayout方法,View里面OnLayout是空实现,因为onLayout()过程是为了确定视图在布局中的位置,而这个操作应该是由布局来完成的,即父视图决定子视图的显示位置。而ViewGroup里面的是抽象方法,也就是需要其子类去实现。
@Override protected void onLayout(boolean changed, int l, int t, int r, int b) { if (mOrientation == VERTICAL) { layoutVertical(l, t, r, b); } else { layoutHorizontal(l, t, r, b); } } void layoutVertical(int left, int top, int right, int bottom) { ... for (int i = 0; i < count; i++) { final View child = getVirtualChildAt(i); if (child == null) { childTop += measureNullChild(i); } else if (child.getVisibility() != GONE) {// final int childWidth = child.getMeasuredWidth(); final int childHeight = child.getMeasuredHeight(); ... setChildFrame(child, childLeft, childTop + getLocationOffset(child), childWidth, childHeight); } } } private void setChildFrame(View child, int left, int top, int width, int height) { child.layout(left, top, left + width, top + height); }可以看到其实是遍历子view,然后又去调用layout,这样就不停的循环,直到遍历完所有子view。由于view过程调用了setFrame方法,可以设置视图的位置,就跟measure的功能重合了,所以这里设置的话有可能会使之前measure的计算失效。
public void draw(Canvas canvas) { ... // Step 1, draw the background, if needed int saveCount; if (!dirtyOpaque) { final Drawable background = mBackground; 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) { ckground.draw(canvas); } else { canvas.translate(scrollX, scrollY); background.draw(canvas); canvas.translate(-scrollX, -scrollY); } } } ... // Step 3, draw the content if (!dirtyOpaque) onDraw(canvas); ... // Step 4, draw the children dispatchDraw(canvas); ... // Step 6, draw decorations (scrollbars) onDrawScrollBars(canvas); return; }这其中最主要的是调用了onDraw和dispatchDraw方法。onDraw是一个空方法,需要子view自己去实现,而ViewGroup的dispatchDraw()方法主要是遍历子view,然后调用drawChild方法,而drawChild又是调用的draw方法,这样就又构成了一个循环调用。
标签:
原文地址:http://blog.csdn.net/wdong_love_cl/article/details/51503157