一、繪製的起點 - performTraversals
和測量、佈局的過程類似,繪製的起點也是從performTraversals
開始的:
private void performTraversals() {
......
final Rect dirty = mDirty;
......
canvas = mSurface.lockCanvas(dirty);
......
mView.draw(canvas);
......
}
複製程式碼
二、繪製的關鍵方法:draw(Canvas canvas) onDraw(Canvas canvas) dispatchDraw()
和繪製過程的前兩個階段不同的是,與draw
過程相關的函式,不僅有draw
、onDraw
還有一個dispatchDraw
,下面我們分析一下這幾個函式在View
和ViewGroup
中的定義:
View
- 首先是
draw(Canvas)
方法,它是public
的,我們通過註釋可以看到它最多會執行6步操作,分別是:繪製背景、儲存canvas
,繪製View
本身內容,繪製子View
,恢復canvas
,繪製裝飾類(如滾動條)。其中第二、五步不是必要的。 對於整個繪製事件的分發,我們需要關注的是第三、四步:呼叫onDraw(Canvas)
繪製View
本身的的內容,呼叫dispatchDraw(Canvas)
繪製子View
,也就是我們上面說的其它兩個方法。
@CallSuper
public void draw(Canvas canvas) {
final int privateFlags = mPrivateFlags;
final boolean dirtyOpaque = (privateFlags & PFLAG_DIRTY_MASK) == PFLAG_DIRTY_OPAQUE &&
(mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
mPrivateFlags = (privateFlags & ~PFLAG_DIRTY_MASK) | PFLAG_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) {
drawBackground(canvas);
}
// 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);
// Overlay is part of the content and draws beneath Foreground
if (mOverlay != null && !mOverlay.isEmpty()) {
mOverlay.getOverlayView().dispatchDraw(canvas);
}
// Step 6, draw decorations (foreground, scrollbars)
onDrawForeground(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;
}
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);
}
// 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);
p.setShader(fade);
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);
p.setShader(fade);
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);
p.setShader(fade);
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);
p.setShader(fade);
canvas.drawRect(right - length, top, right, bottom, p);
}
canvas.restoreToCount(saveCount);
// Overlay is part of the content and draws beneath Foreground
if (mOverlay != null && !mOverlay.isEmpty()) {
mOverlay.getOverlayView().dispatchDraw(canvas);
}
// Step 6, draw decorations (foreground, scrollbars)
onDrawForeground(canvas);
}
複製程式碼
onDraw(Canvas canvas)
方法是一個空實現,因為繪製應當由具體的控制元件來實現。- 由於單純的
View
來說,它沒有子View
,因此dispatchDraw
方法也是一個空實現。 ViewGroup
- 沒有重寫
draw(Canvas canvas)
方法。 - 沒有重寫
onDraw(Canvas canvas)
方法。 - 由於
ViewGroup
要負責將繪製的訊息通知子View
,所以它重寫了dispatchDraw
方法,並在裡面呼叫drawChild
一級級把繪製的訊息傳遞下去。
protected void dispatchDraw(Canvas canvas) {
//....
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;
}
}
int childIndex = customOrder ? getChildDrawingOrder(childrenCount, i) : i;
final View child = (preorderedList == null)
? children[childIndex] : preorderedList.get(childIndex);
if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null) {
more |= drawChild(canvas, child, drawingTime);
}
}
//...
}
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
return child.draw(canvas, this, drawingTime);
}
複製程式碼
通過分析View/ViewGroup
的這三個方法的定義,那麼整個由DecorView
到個葉節點View
的繪製過程就很清楚了:
- 首先呼叫了
mView
的draw
方法,之前我們分析過它其實就是型別為FrameLayout
的DecorView
,由於FrameLayout
和ViewGroup
都沒有重寫draw
方法,它其實是呼叫了View
當中的draw
執行前面分析過的那些步驟,首先是呼叫自己的onDraw
方法來繪製自己,然後呼叫ViewGroup
的dispatchDraw
來繪製子View
,最終會呼叫到這些子View
的draw
方法。 - 當第一步的這些子
View
的draw
方法被呼叫時,都會先執行它的onDraw
方法,這個大家都是一樣的,但是注意到對於每個具體的子View
,由於dispatchDraw
方法決定了事件是否會被分發,所以我們會有以下幾種情況: - 繼承於
ViewGroup
的控制元件,但是沒有下一級子View
,雖然ViewGroup
重寫了dispatchDraw
方法,但是此時childCount==0
,因此drawChild
不會被呼叫,整個傳遞過程終止。 - 繼承於
View
的控制元件,此時dispatchDraw
方法是一個空實現,因此不會繼續傳遞下去。 - 繼承於
ViewGroup
的控制元件,並且有子View
,由於ViewGroup
重寫了dispatchDraw
方法,因此會繼續呼叫子View
的draw
方法傳遞下去。
三、小結
對於這三個關鍵的方法,簡單的總結一下它們的作用:
draw
:作為整個父容器和子View
之間繪製事件傳遞的紐帶。onDraw
:負責View
本身的具體繪製。dispatchDraw
:負責將繪製分發給下一級的子View
。