Android View 的繪製流程之 Layout 和 Draw 過程詳解 (二)

View 的繪製系列文章:

 

在上一篇 Android View 的繪製流程之 Measure 過程詳解 (一),已經詳細的分析了 DecorView 和其子 View 的測量過程,接下去就要開始講  layout 和 draw 流程。下面開始進入分析:html

DecorView Layout 階段

在 ViewRootImpl 中,調用 performLayout 方法來肯定 DecorView 在屏幕中的位置,下面看下具體的代碼邏輯:canvas

// ViewRootImpl 
private void performLayout(WindowManager.LayoutParams lp, int desiredWindowWidth, int desiredWindowHeight) { mLayoutRequested = false; mScrollMayChange = true; mInLayout = true; final View host = mView; if (host == null) { return; } if (DEBUG_ORIENTATION || DEBUG_LAYOUT) { Log.v(mTag, "Laying out " + host + " to (" + host.getMeasuredWidth() + ", " + host.getMeasuredHeight() + ")"); } Trace.traceBegin(Trace.TRACE_TAG_VIEW, "layout"); try {
       // 根據測量結果進行繪製 host.layout(
0, 0, host.getMeasuredWidth(), host.getMeasuredHeight()); mInLayout = false; int numViewsRequestingLayout = mLayoutRequesters.size(); if (numViewsRequestingLayout > 0) { // requestLayout() was called during layout. // If no layout-request flags are set on the requesting views, there is no problem. // If some requests are still pending, then we need to clear those flags and do // a full request/measure/layout pass to handle this situation. ArrayList<View> validLayoutRequesters = getValidLayoutRequesters(mLayoutRequesters, false); if (validLayoutRequesters != null) { // Set this flag to indicate that any further requests are happening during // the second pass, which may result in posting those requests to the next // frame instead mHandlingLayoutInLayoutRequest = true; // Process fresh layout requests, then measure and layout int numValidRequests = validLayoutRequesters.size(); for (int i = 0; i < numValidRequests; ++i) { final View view = validLayoutRequesters.get(i); Log.w("View", "requestLayout() improperly called by " + view + " during layout: running second layout pass"); view.requestLayout(); } measureHierarchy(host, lp, mView.getContext().getResources(), desiredWindowWidth, desiredWindowHeight); mInLayout = true; host.layout(0, 0, host.getMeasuredWidth(), host.getMeasuredHeight()); mHandlingLayoutInLayoutRequest = false; // Check the valid requests again, this time without checking/clearing the // layout flags, since requests happening during the second pass get noop'd validLayoutRequesters = getValidLayoutRequesters(mLayoutRequesters, true); if (validLayoutRequesters != null) { final ArrayList<View> finalRequesters = validLayoutRequesters; // Post second-pass requests to the next frame getRunQueue().post(new Runnable() { @Override public void run() { int numValidRequests = finalRequesters.size(); for (int i = 0; i < numValidRequests; ++i) { final View view = finalRequesters.get(i); Log.w("View", "requestLayout() improperly called by " + view + " during second layout pass: posting in next frame"); view.requestLayout(); } } }); } } } } finally { Trace.traceEnd(Trace.TRACE_TAG_VIEW); } mInLayout = false; }

當在 layout 繪製過程當中,收到了關於從新 layout 的請求,會先判斷這些請求裏面哪些是有效的,若是是有效的,那麼就必須先處理,清除 layout-request flags (View.PFLAG_FORCE_LAYOUT)這些標記,再作一次完全的重繪工做:從新測量,layout。緩存

那麼對於 DecorView 來講,調用 layout 方法,就是對它自身進行佈局,注意到傳遞的參數分別是 0,0, host.getMeasuredWidth, host.getMeasuredHeigh,它們分別表明了一個  View 的上下左右四個位置,顯然,DecorView 的左上位置爲 0,而後寬高爲它的測量寬高,下面來看 layout 的具體代碼:app

// ViewGroup 
public final void layout(int l, int t, int r, int b) {
        if (!mSuppressLayout && (mTransition == null || !mTransition.isChangingLayout())) {
            if (mTransition != null) {
                mTransition.layoutChange(this);
            }
            super.layout(l, t, r, b);
        } else {
            // record the fact that we noop'd it; request layout when transition finishes
            mLayoutCalledWhileSuppressed = true;
        }
    }

 因爲 ViewGroup 的 layout 方法是 final 類型,子類不能重寫,這裏調用了父類的  View#layout 方法,下面看看該方法是如何操做的:less

// View   
public void layout(int l, int t, int r, int b) { if ((mPrivateFlags3 & PFLAG3_MEASURE_NEEDED_BEFORE_LAYOUT) != 0) { onMeasure(mOldWidthMeasureSpec, mOldHeightMeasureSpec); mPrivateFlags3 &= ~PFLAG3_MEASURE_NEEDED_BEFORE_LAYOUT; } int oldL = mLeft; int oldT = mTop; int oldB = mBottom; int oldR = mRight;      // 設置界面的顯示大小 boolean changed = isLayoutModeOptical(mParent) ? setOpticalFrame(l, t, r, b) : setFrame(l, t, r, b);      // 發生了改變,或者是須要從新 layout,那麼就會進入 onLayout 邏輯 if (changed || (mPrivateFlags & PFLAG_LAYOUT_REQUIRED) == PFLAG_LAYOUT_REQUIRED) {
// 開始 onLayout(changed, l, t, r, b);
if (shouldDrawRoundScrollbar()) { if(mRoundScrollbarRenderer == null) { mRoundScrollbarRenderer = new RoundScrollbarRenderer(this); } } else { mRoundScrollbarRenderer = null; }        // 清除請求 layout 的標記 mPrivateFlags &= ~PFLAG_LAYOUT_REQUIRED; ListenerInfo li = mListenerInfo; if (li != null && li.mOnLayoutChangeListeners != null) { ArrayList<OnLayoutChangeListener> listenersCopy = (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone(); int numListeners = listenersCopy.size(); for (int i = 0; i < numListeners; ++i) { listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB); } } } final boolean wasLayoutValid = isLayoutValid(); mPrivateFlags &= ~PFLAG_FORCE_LAYOUT; mPrivateFlags3 |= PFLAG3_IS_LAID_OUT; if (!wasLayoutValid && isFocused()) { mPrivateFlags &= ~PFLAG_WANTS_FOCUS; if (canTakeFocus()) { // We have a robust focus, so parents should no longer be wanting focus. clearParentsWantFocus(); } else if (getViewRootImpl() == null || !getViewRootImpl().isInLayout()) { // This is a weird case. Most-likely the user, rather than ViewRootImpl, called // layout. In this case, there's no guarantee that parent layouts will be evaluated // and thus the safest action is to clear focus here. clearFocusInternal(null, /* propagate */ true, /* refocus */ false); clearParentsWantFocus(); } else if (!hasParentWantsFocus()) { // original requestFocus was likely on this view directly, so just clear focus clearFocusInternal(null, /* propagate */ true, /* refocus */ false); } // otherwise, we let parents handle re-assigning focus during their layout passes. } else if ((mPrivateFlags & PFLAG_WANTS_FOCUS) != 0) { mPrivateFlags &= ~PFLAG_WANTS_FOCUS; View focused = findFocus(); if (focused != null) { // Try to restore focus as close as possible to our starting focus. if (!restoreDefaultFocus() && !hasParentWantsFocus()) { // Give up and clear focus once we've reached the top-most parent which wants // focus. focused.clearFocusInternal(null, /* propagate */ true, /* refocus */ false); } } } if ((mPrivateFlags3 & PFLAG3_NOTIFY_AUTOFILL_ENTER_ON_LAYOUT) != 0) { mPrivateFlags3 &= ~PFLAG3_NOTIFY_AUTOFILL_ENTER_ON_LAYOUT; notifyEnterOrExitForAutoFillIfNeeded(true); } }

 調用了 setFrame 方法,並把四個位置信息傳遞進去,這個方法用於肯定 View 的四個頂點的位置,看下具體的代碼:ide

protected boolean setFrame(int left, int top, int right, int bottom) {
        boolean changed = false;

        if (DBG) {
            Log.d(VIEW_LOG_TAG, this + " View.setFrame(" + left + "," + top + ","
                    + right + "," + bottom + ")");
        }
     // 有一個不同說明發生了改變 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
            changed = true;

            // Remember our drawn bit
            int drawn = mPrivateFlags & PFLAG_DRAWN;

            int oldWidth = mRight - mLeft;
            int oldHeight = mBottom - mTop;
            int newWidth = right - left;
            int newHeight = bottom - top;
            boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);

            // Invalidate our old position
            invalidate(sizeChanged);

            mLeft = left;
            mTop = top;
            mRight = right;
            mBottom = bottom;
            mRenderNode.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
       // 作好標記             mPrivateFlags |= PFLAG_HAS_BOUNDS;

       // size 改變的回調 if (sizeChanged) {
                sizeChange(newWidth, newHeight, oldWidth, oldHeight);
            }

            if ((mViewFlags & VISIBILITY_MASK) == VISIBLE || mGhostView != null) {
                // If we are visible, force the DRAWN bit to on so that
                // this invalidate will go through (at least to our parent).
                // This is because someone may have invalidated this view
                // before this call to setFrame came in, thereby clearing
                // the DRAWN bit.
                mPrivateFlags |= PFLAG_DRAWN;
                invalidate(sizeChanged);
                // parent display list may need to be recreated based on a change in the bounds
                // of any child
                invalidateParentCaches();
            }

            // Reset drawn bit to original value (invalidate turns it off)
            mPrivateFlags |= drawn;

            mBackgroundSizeChanged = true;
            mDefaultFocusHighlightSizeChanged = true;
            if (mForegroundInfo != null) {
                mForegroundInfo.mBoundsChanged = true;
            }

            notifySubtreeAccessibilityStateChangedIfNeeded();
        }
        return changed;
    }
這裏咱們看到它對 mLeft、mTop、mRight、mBottom 這四個值進行了從新賦值,對於每個View,包括 ViewGroup 來講,以上四個值保存了 Viwe 的位置信息,因此這四個值是最終寬高,也便是說,若是要獲得 View 的位置信息, 那麼就應該在 layout 方法完成後調用 getLeft()、getTop() 等方法來取得最終寬高,若是是在此以前調用相應的方法,只能獲得 0 的結果。當初始化完畢後,ViewGroup 的佈局流程也就完成了。

賦值後,先後對比大小,若是發生了改變,就會調用了 sizeChange()方法,最終會回調 onSizeChanged,標明 view 的尺寸發生了變化,第一次 laout 的時候也會調用。在自定義view 的時候,能夠在這裏獲取控件的寬和高度。oop

  private void sizeChange(int newWidth, int newHeight, int oldWidth, int oldHeight) {
        onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
      ......
}

子 View Layout 流程

當 DecorView 肯定好了本身的位置以後,開始調用 onLayout 來肯定子 view 的位置。對於 onLayout 方法,View 和 ViewGroup 類是空實現,接下來看 FrameLayout 的實現:佈局

// FrameLayout 
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        layoutChildren(left, top, right, bottom, false /* no force left gravity */);
    }

 能夠看到,該方法調用了 layoutChildren 來肯定子 view 的位置。post

// FrameLaout   
void layoutChildren(int left, int top, int right, int bottom, boolean forceLeftGravity) { final int count = getChildCount();      // 獲取 DecoverView 剩餘的空間範圍 final int parentLeft = getPaddingLeftWithForeground(); final int parentRight = right - left - getPaddingRightWithForeground(); final int parentTop = getPaddingTopWithForeground(); final int parentBottom = bottom - top - getPaddingBottomWithForeground(); for (int i = 0; i < count; i++) { final View child = getChildAt(i); if (child.getVisibility() != GONE) { final LayoutParams lp = (LayoutParams) child.getLayoutParams(); final int width = child.getMeasuredWidth(); final int height = child.getMeasuredHeight(); int childLeft; int childTop;
          // DEFAULT_CHILD_GRAVITY = Gravity.TOP | Gravity.START 默認是在左上角
int gravity = lp.gravity;
                if (gravity == -1) {
                    gravity = DEFAULT_CHILD_GRAVITY;
                }

                final int layoutDirection = getLayoutDirection();
                final int absoluteGravity = Gravity.getAbsoluteGravity(gravity, layoutDirection);
                final int verticalGravity = gravity & Gravity.VERTICAL_GRAVITY_MASK;
          // 肯定左邊起始點 switch (absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
                    case Gravity.CENTER_HORIZONTAL:
                        childLeft = parentLeft + (parentRight - parentLeft - width) / 2 +
                        lp.leftMargin - lp.rightMargin;
                        break;
                    case Gravity.RIGHT:
                        if (!forceLeftGravity) {
                            childLeft = parentRight - width - lp.rightMargin;
                            break;
                        }
                    case Gravity.LEFT:
                    default:
                        childLeft = parentLeft + lp.leftMargin;
                }
          // 肯定上邊起始點 switch (verticalGravity) {
                    case Gravity.TOP:
                        childTop = parentTop + lp.topMargin;
                        break;
                    case Gravity.CENTER_VERTICAL:
                        childTop = parentTop + (parentBottom - parentTop - height) / 2 +
                        lp.topMargin - lp.bottomMargin;
                        break;
                    case Gravity.BOTTOM:
                        childTop = parentBottom - height - lp.bottomMargin;
                        break;
                    default:
                        childTop = parentTop + lp.topMargin;
                }

                child.layout(childLeft, childTop, childLeft + width, childTop + height);
            }
        }
    }

先梳理一下以上邏輯:動畫

  • 首先先獲取父容器的 padding 值,獲得 DecorView 的可用於顯示的空間範圍。

  • 而後遍歷其每個子 View,根據子 View 的 layout_gravity 屬性、子 View 的測量寬高、父容器的 padding 值、來肯定子 View 的左上角的座標位置

  • 而後調用 child.layout 方法,參數是左上角座標和自身寬高結合起來的,這樣就能夠肯定子 View 的位置。

最終調用 layout 是 View#layout,前面已經分析過,就不在分析了。

能夠看到子 View 的佈局流程也很簡單,若是子 View 是一個 ViewGroup,那麼就會重複以上步驟,若是是一個 View,那麼會直接調用 View#layout 方法,根據以上分析,在該方法內部會設置 view 的四個佈局參數,接着調用 onLayout 方法 :

protected void onLayout(boolean changed, int left, int top, int right, int bottom) {

}

此方法是一個空方法,也就是說須要子類去實現此方法,不一樣的 View 實現方式不一樣,這裏就不分析了。

layout階段的基本思想也是由根View開始,遞歸地完成整個控件樹的佈局(layout)工做。 

對於寬高的獲取這裏在總結下:

 

 

 這裏須要注意一下,在非通常狀況下,也就是經過人爲設置,重寫View的layout()強行設置,這種狀況下,測量的值與最終的值是不同的。

Layout 總體的流程圖

上面分別從 View 和 ViewGroup 的角度講解了佈局流程,這裏再以流程圖的形式概括一下整個 Layout 過程,便於加深記憶:

 

DecorView Draw 流程

Draw 的入口也是在 ViewRootImpl 中,執行 ViewRootImpl#performTraversals 中會執行 ViewRootIml#performDraw:

private void performDraw() {
...
//fullRedrawNeeded,它的做用是判斷是否須要從新繪製所有視圖
draw(fullRedrawNeeded);
...
}

而後會執行到 ViewRootImpl#draw:

private void draw(boolean fullRedrawNeeded) {
 ...
 //獲取mDirty,該值表示須要重繪的區域
 final Rect dirty = mDirty;
 if (mSurfaceHolder != null) {
  // The app owns the surface, we won't draw.
  dirty.setEmpty();
  if (animating) {
   if (mScroller != null) {
    mScroller.abortAnimation();
   }
   disposeResizeBuffer();
  }
  return;
 }

 //若是fullRedrawNeeded爲真,則把dirty區域置爲整個屏幕,表示整個視圖都須要繪製
 //第一次繪製流程,須要繪製全部視圖
 if (fullRedrawNeeded) {
  mAttachInfo.mIgnoreDirtyState = true;
  dirty.set(0, 0, (int) (mWidth * appScale + 0.5f), (int) (mHeight * appScale + 0.5f));
 }
 ...
 if (!drawSoftware(surface, mAttachInfo, xOffset, yOffset, scalingRequired, dirty)) {
    return;
  }
}

 接着會執行到 ViewRootIml#drawSoftware,而後在 ViewRootIml#drawSoftware 會執行到 mView.draw(canvas)。

private boolean drawSoftware(Surface surface, AttachInfo attachInfo, int xoff, int yoff,
   boolean scalingRequired, Rect dirty) {
 final Canvas canvas;
  //鎖定canvas區域,由dirty區域決定
  //這個canvas就是咱們想在上面繪製東西的畫布
  canvas = mSurface.lockCanvas(dirty);
  ...
 //畫布支持位圖的密度,和手機分辨率相關
  canvas.setDensity(mDensity);
 ...
   if (!canvas.isOpaque() || yoff != 0 || xoff != 0) {
                canvas.drawColor(0, PorterDuff.Mode.CLEAR);
            }
   ...
      canvas.translate(-xoff, -yoff);
   ...
   //正式開始繪製
   mView.draw(canvas);
  ...
 //提交須要繪製的東西
  surface.unlockCanvasAndPost(canvas);
}

 mView.draw(canvas) 開始真正的繪製。此處 mView 就是 DecorView,先看 DecorView 中 Draw 的方法:

  public void draw(Canvas canvas) {
        super.draw(canvas);

        if (mMenuBackground != null) {
            mMenuBackground.draw(canvas);
        }
    }

 裏面會調用 View#draw:

    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);
        }

        // 若是能夠跳過2和5步
        final int viewFlags = mViewFlags;
      //判斷是否有繪製衰退邊緣的標示
        boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
        boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
     // 若是沒有繪製衰退邊緣只須要3,4,6步
        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);
    }

能夠看到,draw過程比較複雜,可是邏輯十分清晰,而官方註釋也清楚地說明了每一步的作法。咱們首先來看一開始的標記位 dirtyOpaque,該標記位的做用是判斷當前 View 是不是透明的,若是 View 是透明的,那麼根據下面的邏輯能夠看出,將不會執行一些步驟,好比繪製背景、繪製內容等。這樣很容易理解,由於一個 View 既然是透明的,那就不必繪製它了。接着是繪製流程的六個步驟,這裏先小結這六個步驟分別是什麼,而後再展開來說。繪製流程的六個步驟:

  1. 對 View 的背景進行繪製

  2. 保存當前的圖層信息(可跳過)

  3. 繪製 View 的內容

  4. 對 View 的子 View 進行繪製(若是有子 View )

  5. 繪製 View 的褪色的邊緣,相似於陰影效果(可跳過)

  6. 繪製 View 的裝飾(例如:滾動條)

其中第2步和第5步是能夠跳過的,咱們這裏不作分析,咱們重點來分析其它步驟。

ViewGroup子類默認狀況下就是不執行 onDraw 方法的,在 ViewGroup 源碼中的 initViewGroup() 方法中設置了一個標記,源碼以下:

private void initViewGroup() {
        // ViewGroup doesn't draw by default
        if (!debugDraw()) {
            setFlags(WILL_NOT_DRAW, DRAW_MASK);
        }
        ......
}

看第二行註釋也知道,ViewGroup 默認狀況下是不會 draw 的。第四行調用 setFlags 方法設置標記 WILL_NOT_DRAW,在回到 View 中 draw 方法看第2行代碼:

1  final int privateFlags = mPrivateFlags;
2  final boolean dirtyOpaque = (privateFlags & PFLAG_DIRTY_MASK) == PFLAG_DIRTY_OPAQUE &&
3      (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);

 

setFlags 方法就是對 View中mPrivateFlags 值進行相應改變,咱們設置標記 WILL_NOT_DRAW 那麼 dirtyOpaque 獲得的值就爲 true,從而 if (!dirtyOpaque) 不成立,也就不會執行onDraw 方法。

1. 繪製背景

View#drawBackground

    private void drawBackground(Canvas canvas) {
       //獲取背景的Drawable,沒有就不須要繪製
        final Drawable background = mBackground;
        if (background == null) {
            return;
        }
       //肯定背景Drawable邊界
        setBackgroundBounds();
        ...

       //若是有偏移量先偏移畫布再將drawable繪製上去
        final int scrollX = mScrollX;
        final int scrollY = mScrollY;
        if ((scrollX | scrollY) == 0) {
            background.draw(canvas);
        } else {
            canvas.translate(scrollX, scrollY);
            //此處會執行各類Drawable對應的draw方法
            background.draw(canvas);
            //把畫布的原點移回去,drawable在屏幕上的位置不動
            canvas.translate(-scrollX, -scrollY);
        }
    }

3. 繪製 View 的內容

先跳過第 2 步,是由於不是全部的 View 都需繪製褪色邊緣。DecorView#onDraw:

   public void onDraw(Canvas c) {
        super.onDraw(c);

        mBackgroundFallback.draw(this, mContentRoot, c, mWindow.mContentParent,
                mStatusColorViewState.view, mNavigationColorViewState.view);
    }

View#onDraw

  protected void onDraw(Canvas canvas) {
}

onDraw 是空實現,須要子 View 本身去繪製。對於DecorView 通常也沒啥內容,除了須要背景顏色等,因此自己並須要繪製啥。

4. 繪製子View

DecorView 繪製完成後,開始繪製子 View,因此 ViewGroup 的繪製須要繪製子 View,直接看看 ViewGroup#dispatchDraw:

protected void dispatchDraw(Canvas canvas) {
       boolean usingRenderNodeProperties = canvas.isRecordingFor(mRenderNode);
       final int childrenCount = mChildrenCount;
       final View[] children = mChildren;
       int flags = mGroupFlags;

//ViewGroup是否有設置子View入場動畫,若是有綁定到View
// 啓動動畫控制器
      ...

//指定修改區域
       int clipSaveCount = 0;
       final boolean clipToPadding = (flags & CLIP_TO_PADDING_MASK) == CLIP_TO_PADDING_MASK;
    // 不讓子view繪製在pandding裏面,也就是去除padding
if (clipToPadding) { clipSaveCount = canvas.save(); canvas.clipRect(mScrollX + mPaddingLeft, mScrollY + mPaddingTop, mScrollX + mRight - mLeft - mPaddingRight, mScrollY + mBottom - mTop - mPaddingBottom); } ... for (int i = 0; i < childrenCount; i++) { //先取mTransientViews中的View,mTransientViews中的View經過addTransientView添加,它們只是容器渲染的一個item 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); } } ... }

ViewGroup#dispatchDraw 的流程是先啓動第一次加到佈局中的動畫,而後肯定繪製區域,遍歷繪製 View,遍歷 View 的時候優先繪製渲染的 mTransientViews,繪製 View 調用到ViewGroup#drawChild:

protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
        //View中有兩個draw方法
        //這個多參數的draw用於view繪製自身內容
        return child.draw(canvas, this, drawingTime);
    }

 View#draw(canvas, this, drawingTime)

boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
 
       boolean drawingWithRenderNode = mAttachInfo != null
                && mAttachInfo.mHardwareAccelerated
                && hardwareAcceleratedCanvas;
      ...

     //主要判斷是否有繪製緩存,若是有,直接使用緩存,若是沒有,調用 draw(canvas)方法
        if (!drawingWithDrawingCache) {
            if (drawingWithRenderNode) {
                mPrivateFlags &= ~PFLAG_DIRTY_MASK;
                ((DisplayListCanvas) canvas).drawRenderNode(renderNode);
            } else {
                // Fast path for layouts with no backgrounds
                if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
                    mPrivateFlags &= ~PFLAG_DIRTY_MASK;
                    dispatchDraw(canvas);
                } else {
                    draw(canvas);
                }
            }
        } else if (cache != null) {
            mPrivateFlags &= ~PFLAG_DIRTY_MASK;
            if (layerType == LAYER_TYPE_NONE) {
                // no layer paint, use temporary paint to draw bitmap
                Paint cachePaint = parent.mCachePaint;
                if (cachePaint == null) {
                    cachePaint = new Paint();
                    cachePaint.setDither(false);
                    parent.mCachePaint = cachePaint;
                }
                cachePaint.setAlpha((int) (alpha * 255));
                canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
            } else {
                // use layer paint to draw the bitmap, merging the two alphas, but also restore
                int layerPaintAlpha = mLayerPaint.getAlpha();
                mLayerPaint.setAlpha((int) (alpha * layerPaintAlpha));
                canvas.drawBitmap(cache, 0.0f, 0.0f, mLayerPaint);
                mLayerPaint.setAlpha(layerPaintAlpha);
            }
      }
}

首先判斷是否已經有緩存,即以前是否已經繪製過一次了,若是沒有,則會調用 draw(canvas) 方法,開始正常的繪製,即上面所說的六個步驟,不然利用緩存來顯示。

這一步也能夠概括爲 ViewGroup 繪製過程,它對子 View 進行了繪製,而子 View 又會調用自身的 draw 方法來繪製自身,這樣不斷遍歷子 View 及子 View 的不斷對自身的繪製,從而使得 View 樹完成繪製。

對於自定義 View ,若是須要繪製東西的話,直接從新 onDraw 就能夠了。

6. 繪製裝飾

    public void onDrawForeground(Canvas canvas) {
     //繪製滑動指示
        onDrawScrollIndicators(canvas);
     //繪製ScrollBar
        onDrawScrollBars(canvas);
     //獲取前景色的Drawable,繪製到canvas上
        final Drawable foreground = mForegroundInfo != null ? mForegroundInfo.mDrawable : null;
        if (foreground != null) {
            if (mForegroundInfo.mBoundsChanged) {
                mForegroundInfo.mBoundsChanged = false;
                final Rect selfBounds = mForegroundInfo.mSelfBounds;
                final Rect overlayBounds = mForegroundInfo.mOverlayBounds;
                if (mForegroundInfo.mInsidePadding) {
                    selfBounds.set(0, 0, getWidth(), getHeight());
                } else {
                    selfBounds.set(getPaddingLeft(), getPaddingTop(),
                            getWidth() - getPaddingRight(), getHeight() - getPaddingBottom());
                }
                final int ld = getLayoutDirection();
                Gravity.apply(mForegroundInfo.mGravity, foreground.getIntrinsicWidth(),
                        foreground.getIntrinsicHeight(), selfBounds, overlayBounds, ld);
                foreground.setBounds(overlayBounds);
            }
            foreground.draw(canvas);
        }
    }

 2和5.繪製View的褪色邊緣

當 horizontalEdges 或者 verticalEdges 有一個 true 的時候,表示須要繪製 View 的褪色邊緣:

     boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
     boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;

這時候先計算出是否須要繪製上下左右的褪色邊緣和它的參數,而後保存視圖層:

        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;
        if (verticalEdges && (top + length > bottom - length)) {
            length = (bottom - top) / 2;
        }
        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);
        }

繪製褪色邊緣,恢復視圖層 :

        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);

所謂的繪製裝飾,就是指 View 除了背景、內容、子 View 的其他部分,例如滾動條等。

最後附上 View 的 draw 流程:

 

 

到此,View 的繪製流程就講完了,下一篇會講自定義 View。

小結:

  • view 不停找 parent 能夠一直找到 DecorView,按理說 DecorView 是頂點了,可是 DecorView 還有個虛擬父 view,ViewRootImpl。 ViewRootImpl 不是一個 View 或者ViewGroup,他有個成員 mView 是 DecorView,全部的操做從 ViewRootImpl 開始自上而下分發

  • view 的 invalidate 不會致使 ViewRootImpl 的 invalidate 被調用,而是遞歸調用父 view的invalidateChildInParent,直到 ViewRootImpl 的 invalidateChildInParent,而後觸發peformTraversals,會致使當前 view 被重繪,因爲 mLayoutRequested 爲 false,不會致使 onMeasure 和 onLayout 被調用,而 OnDraw 會被調用

  • 一個 view 的 invalidate 會致使自己 PFLAG_INVALIDATED 置 1,致使自己以及父族 viewgroup 的 PFLAG_DRAWING_CACHE_VALID 置 0

  • requestLayout 會直接遞歸調用父窗口的 requestLayout,直到 ViewRootImpl,而後觸發 peformTraversals,因爲 mLayoutRequested 爲 true,會致使 onMeasure 和onLayout 被調用。不必定會觸發 OnDraw

  • requestLayout 觸發 onDraw 多是由於在在 layout 過程當中發現 l, t, r, b 和之前不同,那就會觸發一次 invalidate,因此觸發了onDraw,也多是由於別的緣由致使 mDirty 非空(好比在跑動畫)

  • requestLayout 會致使本身以及父族 view 的 PFLAG_FORCE_LAYOUT 和 PFLAG_INVALIDATED 標誌被設置。

  • 通常來講,只要刷新的時候就調用 invalidate,須要從新 measure 就調用 requestLayout,後面再跟個 invalidate(爲了保證重繪),

相關文章
相關標籤/搜索