引言
? ? ? ? 上期View的繪制流程(二):從Activity的生命周期到顯示頁面講到在WindowManagerGlobal.addView()調用了ViewRootImpl.setView()方法,然后在ViewRootImpl的setView()方法中調用requestLayout(),然后調用performTraversals()進入View的測量、布局、繪制流程。這期將從performTraversals(),詳細分析performTraversals()中的三個重要的方法 performMeasure(childWidthMeasureSpec, childHeightMeasureSpec) 、performLayout(lp, mWidth, mHeight) 、performDraw()。
所以,一個完整的繪制流程包括measure、layout、draw三個步驟,其中:
measure:測量。系統(tǒng)會先根據xml布局文件和代碼中對控件屬性的設置,來獲取或者計算出每個View和ViewGrop的尺寸,并將這些尺寸保存下來
layout:布局。根據測量出的結果以及對應的參數,來確定每一個控件應該顯示的位置
draw:繪制。確定好位置后,就將這些控件繪制到屏幕上
每個View負責繪制自己,而ViewGroup還需要負責通知自己的子View進行繪制操作
Measure — 測量
MeasureSpec
從上面的源碼我們可以發(fā)現,系統(tǒng)會用一個int型的值(childWidthMeasureSpec和childHeightMeasureSpec)來存儲View的寬高的信息
上文中用于生成childWidthMeasureSpec和childHeightMeasureSpec的getRootMeasureSpec()方法
這個方法實際上調用的就是MeasureSpec.makeMeasureSpec()
那么這個MeasureSpec到底是什么意思呢?MeasureSpec概括了從父布局傳遞給子view的布局要求,包括了測量模式和測量大小。分析代碼可知,int長度為32位,高2位表示mode(模式),后30位用于表示size(大小)
有三種mode:
UNSPECIFIED:不對View大小做限制,如:ListView,ScrollView
EXACTLY:確切的大小,如:100dp或者march_parent
AT_MOST:大小不可超過某數值,如:wrap_content
子View的LayoutParams / 父view的MeasureSpecEXACTLYAT_MOSTUNSPECIFIED
具體大小(如100dp)EXACTLYEXACTLYEXACTLY
match_parentEXACTLYAT_MOSTUNSPECIFIED
wrap_contentAT_MOSTAT_MOSTUNSPECIFIED
當View采用固定寬高時(即設置固定的dp/px),不管父容器是什么模式,View都是EXACTLY模式,并且大小遵循我們設置的值
當View的寬高是match_parent時,如果父容器的是EXACTLY模式,那么View也是EXACTLY模式且其大小是父容器的剩余空間;如果父容器是AT_MOST模式那么View也是AT_MOST模式并且其大小不會超過父容器的剩余空間
當View的寬高是wrap_content時,View都是AT_MOST模式并且其大小不能超過父容器的剩余空間
只要提供父容器的MeasureSpec和子元素的LayoutParams,就可以確定出子元素的MeasureSpec,進一步便可以確定出測量后的大小
onMeasure
mView.measure()內部會調用onMeasure()
一個View的實際測量工作是在onMeasure()中實現的,onMeasure()已經默認為我們的控件測量了寬高
在自定義ViewGroup時,默認的onMeasure()往往不能滿足我們的需求,這時候就要重寫該方法,在該方法內測量子View的尺寸。當重寫onMeasure()時,必須調用setMeasuredDimension(width,height)來存儲該View測量出的寬和高。如果不這樣做將會觸發(fā)IllegalStateException
ViewGroup提供了三個方法測量子View的寬高
measureChildren(intwidthMeasureSpec,intheightMeasureSpec)
measureChild(Viewchild,intparentWidthMeasureSpec)
protectedvoidmeasureChildWithMargins(Viewchild,..)
View和ViewGroup重寫onMeasure的差異
下面用兩個例子分別來展示一下View和ViewGroup重寫onMeasure的差異
View
View一般只關心自身尺寸的測量
ViewGroup
ViewGroup一般會先遍歷子View,調用子View的測量方法,然后在再結合子View的尺寸來確定自身的大小
Layout - 布局
前面measure的作用是測量每個View的尺寸,而layout的作用是根據前面測量的尺寸以及設置的其它屬性值,共同來確定View的位置
ViewGroup的layout()
從源碼中可以看出實際上調用的還是View的layout()方法
View的layout()
從源碼可以看出layout()最終通過setFrame()方法對view的四個屬性(mLeft、mTop、mRight、mBottom)進行了賦值,從而去確定View的大小和位置,如果發(fā)生改變則調用onLayout()方法
onLayout
我們先來看看ViewGroup的onLayout()方法,該方法是一個抽象方法。因為layout過程是父布局容器布局子View的過程,onLayout()方法對子View沒有意義,只有ViewGroup才有用,所以ViewGroup應該重寫該方法并為每一個子View調用layout()
protectedabstractvoidonLayout(booleanchanged,intl,intt,intr,intb);
我們再來看看頂層ViewGroup,也就是DecorView的onLayout()方法。DecerView繼承自FrameLayout,所以我們直接看FrameLayout的onLayout()方法
@OverrideprotectedvoidonLayout(booleanchanged,intleft,inttop,intright,intbottom){layoutChildren(left,top,right,bottom,false/* no force left gravity */);}voidlayoutChildren(intleft,inttop,intright,intbottom,booleanforceLeftGravity){finalintcount=getChildCount();......for(inti=0;i<count;i++){finalViewchild=getChildAt(i);if(child.getVisibility()!=GONE){finalLayoutParamslp=(LayoutParams)child.getLayoutParams();finalintwidth=child.getMeasuredWidth();finalintheight=child.getMeasuredHeight();......child.layout(childLeft,childTop,childLeft+width,childTop+height);}}
我們可以看到,這里面會對每一個child調用layout()方法。如果該child仍然是ViewGroup,會繼續(xù)遞歸下去;如果是葉子View,則會走到View的onLayout空方法,該葉子View布局流程就走完了。另外,width和height分別來源于measure階段存儲的測量值
Draw - 繪制
當layout完成后,就進入到draw階段了,在這個階段,會根據layout中確定的各個view的位置將它們畫出來
前面說過,mView就是DecorView,所以我們直接來看DecorView的draw()方法
@Overridepublicvoiddraw(Canvascanvas){super.draw(canvas);if(mMenuBackground!=null){mMenuBackground.draw(canvas);}}
調用完super.draw()后,還畫了菜單背景。我們繼續(xù)關注super.draw()方法,會發(fā)現FrameLayout和ViewGroup都沒有重寫該方法,直接進到了View的draw()方法
@CallSuperpublicvoiddraw(Canvascanvas){......intsaveCount;// Step 1, draw the background, if neededif(!dirtyOpaque){drawBackground(canvas);}// Step 2, If necessary, save the canvas' layers to prepare for fading......// Step 3, draw the contentif(!dirtyOpaque)onDraw(canvas);// Step 4, draw the childrendispatchDraw(canvas);//Step 5, If necessary, draw the fading edges and restore layers......// Step 6, draw decorations (foreground, scrollbars)onDrawForeground(canvas);45......}
主要是就是如下幾步,其中最重要的就是畫內容和畫子View
畫背景。對應我我們在xml布局文件中設置的android:background屬性
畫內容。通過重寫onDraw()方法
畫子View。dispatchDraw()方法用于幫助ViewGroup來遞歸畫它的子View
畫裝飾。這里指畫滾動條和前景。其實平時的每一個View都有滾動條,只是沒有顯示而已
onDraw()
當自定義View需要進行繪制的時候,我們往往會重寫onDraw()方法,這里放一個簡單的例子感受一下
PaintmPaint=newPaint(Paint.ANTI_ALIAS_FLAG);@OverrideprotectedvoidonDraw(Canvascanvas){mPaint.setColor(Color.YELLOW);canvas.drawRect(0,0,getWidth(),getHeight(),mPaint);mPaint.setColor(Color.BLUE);mPaint.setTextSize(20);Stringtext="Hello View";canvas.drawText(text,0,getHeight()/2,mPaint);}