performTraversals()中執行了performmeasure确定了每一個View的width,和height, 就會調用performLayout,确定這些View在螢幕
的位置,調用performDraw()在每一個View将圖像畫在螢幕上自己layout确定的位置上就可以了。
調用如下performDraw()->draw()->drawSoftware()->從DecorView開始遞歸調用繪制,代碼如下:
mView.draw(canvas);
ViewGroup,FrameLayout中都沒有重寫此方法,回到View中檢視,View中代碼也好長,關鍵是調用了一個diapatchDraw(),在ViewGroup中
重寫了這個方法,關鍵點在于
for (int i = 0; i < childrenCount; i++) {
while (transientIndex >= 0 && mTransientIndices.get(transientIndex) == i) {
final View transientChild = mTransientViews.get(transientIndex);
if ((transientChild.mViewFlags & VISIBILITY_MASK) == VISIBLE ||
transientChild.getAnimation() != null) {
more |= drawChild(canvas, transientChild, drawingTime);
}
transientIndex++;
if (transientIndex >= transientCount) {
transientIndex = -1;
}
}
final int childIndex = getAndVerifyPreorderedIndex(childrenCount, i, customOrder);
final View child = getAndVerifyPreorderedView(preorderedList, children, childIndex);
if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null) {
more |= drawChild(canvas, child, drawingTime);
}
}
周遊自己的chlidView一次調用DrawChlid,這個方法如下
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
return child.draw(canvas, this, drawingTime);
}
調用Chlid中的額draw方法,
draw方法會調用onDraw這個回調,在這個回調中可以自定義View的繪制。終于自上而下完成View的繪制。
其他說明draw方法的說明如下:
/*
* 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)
*/
第一步,Draw the background是發生在onDraw之前的,還有剩下的一些畫布的處理,應該注意相對回調onDraw的先後的順序。