本篇終于來到了View的三大流程的最后一個(gè)流程了,本次會(huì)帶著大家探究一下View的performDraw()究竟是如何工作的。
先看performDraw()
由于performDraw()的代碼并不多,那我們就先看看它的實(shí)現(xiàn)吧!
private void performDraw() {
if (mAttachInfo.mDisplayState == Display.STATE_OFF && !mReportNextDraw) {
return;
}
final boolean fullRedrawNeeded = mFullRedrawNeeded;
mFullRedrawNeeded = false;
mIsDrawing = true;
Trace.traceBegin(Trace.TRACE_TAG_VIEW, "draw");
try {
// 1. 調(diào)用繪制方法
draw(fullRedrawNeeded);
} finally {
mIsDrawing = false;
Trace.traceEnd(Trace.TRACE_TAG_VIEW);
}
// For whatever reason we didn't create a HardwareRenderer, end any
// hardware animations that are now dangling
if (mAttachInfo.mPendingAnimatingRenderNodes != null) {
final int count = mAttachInfo.mPendingAnimatingRenderNodes.size();
for (int i = 0; i < count; i++) {
mAttachInfo.mPendingAnimatingRenderNodes.get(i).endAllAnimators();
}
mAttachInfo.mPendingAnimatingRenderNodes.clear();
}
if (mReportNextDraw) {
mReportNextDraw = false;
if (mAttachInfo.mHardwareRenderer != null) {
mAttachInfo.mHardwareRenderer.fence();
}
if (LOCAL_LOGV) {
Log.v(TAG, "FINISHED DRAWING: " + mWindowAttributes.getTitle());
}
if (mSurfaceHolder != null && mSurface.isValid()) {
mSurfaceHolderCallback.surfaceRedrawNeeded(mSurfaceHolder);
SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks();
if (callbacks != null) {
for (SurfaceHolder.Callback c : callbacks) {
if (c instanceof SurfaceHolder.Callback2) {
((SurfaceHolder.Callback2)c).surfaceRedrawNeeded(
mSurfaceHolder);
}
}
}
}
try {
mWindowSession.finishDrawing(mWindow);
} catch (RemoteException e) {
}
}
}
從上面的代碼可以看出,View的繪制流程并不復(fù)雜,其中最主要的就是調(diào)用了draw()方法。那我們來看看draw()方法的代碼。我們只探究繪制流程的基本過程,所以我只把關(guān)鍵的代碼貼出。其實(shí)其中關(guān)鍵的代碼也就那么一句。
private void draw(boolean fullRedrawNeeded) {
......
mAttachInfo.mTreeObserver.dispatchOnDraw();
......
}
android就是通過上面的那句代碼將繪制的任務(wù)分發(fā)到view tree中的每一個(gè)View中。那我們?cè)賮砜纯催@個(gè)ViewTreeObserver對(duì)象是什么。下面是該類的官方注釋聲明。
/**
* A view tree observer is used to register listeners that can be notified of global
* changes in the view tree. Such global events include, but are not limited to,
* layout of the whole tree, beginning of the drawing pass, touch mode change....
*
* A ViewTreeObserver should never be instantiated by applications as it is provided
* by the views hierarchy. Refer to {@link android.view.View#getViewTreeObserver()}
* for more information.
*/
public final class ViewTreeObserver
ViewTreeObserver用于注冊(cè)關(guān)于view tree的各種事件的監(jiān)聽器,例如繪制事件,布局事件,滾動(dòng)事件等等。View的繪制過程就是通過ViewTreeObserver注冊(cè)監(jiān)聽來進(jìn)行事件分發(fā)的。
/**
* Interface definition for a callback to be invoked when the view tree is about to be drawn.
*/
public interface OnDrawListener {
/**
* <p>Callback method to be invoked when the view tree is about to be drawn. At this point,
* views cannot be modified in any way.</p>
*
* <p>Unlike with {@link OnPreDrawListener}, this method cannot be used to cancel the
* current drawing pass.</p>
*
* <p>An {@link OnDrawListener} listener <strong>cannot be added or removed</strong>
* from this method.</p>
*
* @see android.view.View#onMeasure
* @see android.view.View#onLayout
* @see android.view.View#onDraw
*/
public void onDraw();
}
/**
* Notifies registered listeners that the drawing pass is about to start.
*/
public final void dispatchOnDraw() {
if (mOnDrawListeners != null) {
final ArrayList<OnDrawListener> listeners = mOnDrawListeners;
int numListeners = listeners.size();
for (int i = 0; i < numListeners; ++i) {
listeners.get(i).onDraw();
}
}
}
通過監(jiān)聽器來調(diào)用view tree中的各個(gè)View的onDraw()方法。
View的draw()
到這里我們基本的performDraw()的流程已經(jīng)清楚了?,F(xiàn)在我們?cè)倩氐?code>View類中來看看它的draw()是如何實(shí)現(xiàn)的。下面就是View中的draw()方法的代碼。
/**
* Manually render this view (and all of its children) to the given Canvas.
* The view must have already done a full layout before this function is
* called. When implementing a view, implement
* {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
* If you do need to override this method, call the superclass version.
*
* @param canvas The Canvas to which the View is rendered.
*/
@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;
}
代碼中的注釋說了,一次繪制的流程必須有序的執(zhí)行下面的六步的操作:
- 繪制背景
- 必要時(shí)保存
canvas層來為View的fading準(zhǔn)備 - 繪制View的內(nèi)容
- 繪制View的孩子
- 必要時(shí)繪制View的fading并且恢復(fù)第2步保存的
canvas層 - 繪制View的decoration,例如前景和滾動(dòng)條
以上的就是View的繪制步驟。當(dāng)我們需要繼承一個(gè)View并需要對(duì)View的繪制進(jìn)行自定義時(shí),我們只需要完成第3步就可以了,其他的步驟View已經(jīng)幫我們完成了。我們只需重寫View的onDraw()方法即可。
總結(jié)
View的基本繪制過程算是比較簡(jiǎn)單吧,按照慣例我們也用張圖片來進(jìn)行總結(jié)。
