探究Android View繪制流程

1.簡(jiǎn)介

在開發(fā)中,我們經(jīng)常會(huì)遇到各種各樣的View,這些View有的是系統(tǒng)提供的,有的是我們自定義的View,可見View在開發(fā)中的重要性,那么了解Android View的繪制流程對(duì)于我們更好地理解View的工作原理和自定義View相當(dāng)有益,本文將依據(jù)Android源碼(API=30)探究View的繪制流程,加深大家對(duì)其的理解和認(rèn)知。

2.View繪制流程概覽

應(yīng)用的一個(gè)頁(yè)面是由各種各樣的View組合而成的,它們能夠按照我們的期望呈現(xiàn)在屏幕上,實(shí)現(xiàn)我們的需求,其背后是有一套復(fù)雜的繪制流程的,主要涉及到以下三個(gè)過程:

  1. measure:顧名思義,是測(cè)量的意思,在這個(gè)階段,做的主要工作是測(cè)量出View的尺寸大小并保存。

  2. layout:這是布局階段,在這個(gè)階段主要是根據(jù)上個(gè)測(cè)量階段得到的View尺寸大小以及View本身的參數(shù)設(shè)置來確定View應(yīng)該擺放的位置。

  3. draw:這是階段相當(dāng)重要,主要執(zhí)行繪制的任務(wù),它根據(jù)測(cè)量和布局的結(jié)果,完成View的繪制,這樣我們就能看到豐富多彩的界面了。

    這些階段執(zhí)行的操作都比較復(fù)雜,幸運(yùn)的是系統(tǒng)幫我們處理了很多這樣的工作,并且當(dāng)我們需要實(shí)現(xiàn)自定義View的時(shí)候,系統(tǒng)又給我們提供了onMeasure()、onLayout()、onDraw()方法,一般來說,我們重寫這些方法,在其中加入我們自己的業(yè)務(wù)邏輯,就可以實(shí)現(xiàn)我們自定義View的需求了。

3.View繪制的入口

講到View繪制的流程,就要提到ViewRootImpl類中的performTraversals()方法,這個(gè)方法中涉及到performMeasure()、performLayout()、performDraw()三個(gè)方法,其中performMeasure()方法是從ViewTree的根節(jié)點(diǎn)開始遍歷執(zhí)行測(cè)量View的工作,performLayout()方法是從ViewTree的根節(jié)點(diǎn)開始遍歷執(zhí)行View的布局工作,而performDraw()方法是從ViewTree的根節(jié)點(diǎn)開始遍歷執(zhí)行繪制View的工作,ViewTree的根節(jié)點(diǎn)是DecorView。performTraversals()方法內(nèi)容很長(zhǎng),以下只是部分代碼。

//ViewRootImpl
private void performTraversals() {
    final View host = mView;
    ...
    int childWidthMeasureSpec = getRootMeasureSpec(mWidth, lp.width);
    int childHeightMeasureSpec = getRootMeasureSpec(mHeight, lp.height);
    ...
    performMeasure(childWidthMeasureSpec, childHeightMeasureSpec);
    ...
    performLayout(lp, mWidth, mHeight);
    ...
    performDraw();
}

4.measure階段

measure是繪制流程的第一個(gè)階段,在這個(gè)階段主要是通過測(cè)量來確定View的尺寸大小。

4.1 MeasureSpec介紹

  1. MeasureSpec封裝了從父View傳遞到子View的布局要求,MeasureSpec由大小和模式組成,它可能有三種模式。
  2. UNSPECIFIED模式:父View沒有對(duì)子View施加任何約束,子View可以是它想要的任何大小。
  3. EXACTLY模式:父View已經(jīng)為子View確定了精確的尺寸,不管子View想要多大尺寸,它都要在父View給定的界限內(nèi)。
  4. AT_MOST模式:在父View指定的大小范圍內(nèi),子View可以是它想要的大小。

4.2 View測(cè)量的相關(guān)方法

  1. ViewRootImpl.performMeasure()方法

    private void performMeasure(int childWidthMeasureSpec, int childHeightMeasureSpec) {
            if (mView == null) {
                return;
            }
            Trace.traceBegin(Trace.TRACE_TAG_VIEW, "measure");
            try {
                mView.measure(childWidthMeasureSpec, childHeightMeasureSpec);
            } finally {
                Trace.traceEnd(Trace.TRACE_TAG_VIEW);
            }
    }
    

    在performMeasure()中,從根布局DecorView開始遍歷執(zhí)行measure()操作。

  2. View.measure()方法

    public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
            boolean optical = isLayoutModeOptical(this);
            if (optical != isLayoutModeOptical(mParent)) {
                Insets insets = getOpticalInsets();
                int oWidth  = insets.left + insets.right;
                int oHeight = insets.top  + insets.bottom;
                widthMeasureSpec  = MeasureSpec.adjust(widthMeasureSpec,  optical ? -oWidth  : oWidth);
                heightMeasureSpec = MeasureSpec.adjust(heightMeasureSpec, optical ? -oHeight : oHeight);
            }
    
            ...
    
            if (forceLayout || needsLayout) {
                // first clears the measured dimension flag
                mPrivateFlags &= ~PFLAG_MEASURED_DIMENSION_SET;
    
                resolveRtlPropertiesIfNeeded();
    
                int cacheIndex = forceLayout ? -1 : mMeasureCache.indexOfKey(key);
                if (cacheIndex < 0 || sIgnoreMeasureCache) {
                    // measure ourselves, this should set the measured dimension flag back
                    onMeasure(widthMeasureSpec, heightMeasureSpec);
                    mPrivateFlags3 &= ~PFLAG3_MEASURE_NEEDED_BEFORE_LAYOUT;
                } else {
                    long value = mMeasureCache.valueAt(cacheIndex);
                    // Casting a long to int drops the high 32 bits, no mask needed
                    setMeasuredDimensionRaw((int) (value >> 32), (int) value);
                    mPrivateFlags3 |= PFLAG3_MEASURE_NEEDED_BEFORE_LAYOUT;
                }
             ...
            }
    
            ...
    }
    

    調(diào)用這個(gè)方法是為了找出視圖應(yīng)該有多大,父View在寬度和高度參數(shù)中提供約束信息,其中widthMeasureSpec參數(shù)是父View強(qiáng)加的水平空間要求,heightMeasureSpec參數(shù)是父View強(qiáng)加的垂直空間要求,這是一個(gè)final方法,實(shí)際的測(cè)量工作是通過調(diào)用onMeasure()方法執(zhí)行的,因此只有onMeasure()方法可以被子類重寫。

  3. View.onMeasure()方法

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
                    getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
    }
    

    這個(gè)方法的作用是測(cè)量視圖及其內(nèi)容,以確定測(cè)量的寬度和高度,這個(gè)方法被measure()方法調(diào)用,并且應(yīng)該被子類重寫去對(duì)它們的內(nèi)容進(jìn)行準(zhǔn)確和有效的測(cè)量,當(dāng)重寫此方法時(shí),必須調(diào)用setMeasuredDimension()方法去存儲(chǔ)這個(gè)View被測(cè)量出的寬度和高度。

  4. View.setMeasuredDimension()方法

    protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
            boolean optical = isLayoutModeOptical(this);
            if (optical != isLayoutModeOptical(mParent)) {
                Insets insets = getOpticalInsets();
                int opticalWidth  = insets.left + insets.right;
                int opticalHeight = insets.top  + insets.bottom;
    
                measuredWidth  += optical ? opticalWidth  : -opticalWidth;
                measuredHeight += optical ? opticalHeight : -opticalHeight;
            }
            setMeasuredDimensionRaw(measuredWidth, measuredHeight);
    }
    

    setMeasuredDimension()方法必須被onMeasure()方法調(diào)用去存儲(chǔ)被測(cè)量出的寬度和高度,在測(cè)量的時(shí)候如果setMeasuredDimension()方法執(zhí)行失敗將會(huì)拋出異常。

    private void setMeasuredDimensionRaw(int measuredWidth, int measuredHeight) {
            mMeasuredWidth = measuredWidth;
            mMeasuredHeight = measuredHeight;
    
            mPrivateFlags |= PFLAG_MEASURED_DIMENSION_SET;
    }
    

    setMeasuredDimensionRaw()方法被setMeasuredDimension()方法調(diào)用來設(shè)置出被測(cè)量出的寬度和高度給View的變量mMeasuredWidth和mMeasuredHeight。

    public static int getDefaultSize(int size, int measureSpec) {
            int result = size;
            int specMode = MeasureSpec.getMode(measureSpec);
            int specSize = MeasureSpec.getSize(measureSpec);
    
            switch (specMode) {
            case MeasureSpec.UNSPECIFIED:
                result = size;
                break;
            case MeasureSpec.AT_MOST:
            case MeasureSpec.EXACTLY:
                result = specSize;
                break;
            }
            return result;
    }
    

    參數(shù)size是這個(gè)View的默認(rèn)大小,參數(shù)measureSpec是父View對(duì)子View施加的約束,通過計(jì)算的得出這個(gè)View 應(yīng)該的大小,如果MeasureSpec沒有施加約束則使用提供的大小,如果是MeasureSpec.AT_MOST或MeasureSpec.EXACTLY模式則會(huì)使用specSize。

    protected int getSuggestedMinimumWidth() {
            return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
    }
    

    getSuggestedMinimumWidth()方法返回View應(yīng)該使用的最小寬度,這個(gè)返回值是View的最小寬度和背景的最小寬度二者之中較大的那一個(gè)值。當(dāng)在onMeasure()方法內(nèi)被使用的時(shí)候,調(diào)用者依然應(yīng)該確保返回的寬度符合父View的要求。

4.3 ViewGroup測(cè)量的相關(guān)方法

ViewGroup繼承View,是一個(gè)可以包含其他子View的一個(gè)特殊的View,在執(zhí)行測(cè)量工作的時(shí)候,它有幾個(gè)比較重要的方法,measureChildren()、measureChild()和getChildMeasureSpec()。

protected void measureChildren(int widthMeasureSpec, int heightMeasureSpec) {
        final int size = mChildrenCount;
        final View[] children = mChildren;
        for (int i = 0; i < size; ++i) {
            final View child = children[i];
            if ((child.mViewFlags & VISIBILITY_MASK) != GONE) {
                measureChild(child, widthMeasureSpec, heightMeasureSpec);
            }
        }
}

measureChildren()方法要求這個(gè)View的子View們?nèi)y(cè)量它們自己,處于GONE狀態(tài)的子View不會(huì)執(zhí)行measureChild()方法。

protected void measureChild(View child, int parentWidthMeasureSpec,
            int parentHeightMeasureSpec) {
        final LayoutParams lp = child.getLayoutParams();

        final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
                mPaddingLeft + mPaddingRight, lp.width);
        final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
                mPaddingTop + mPaddingBottom, lp.height);

        child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
}

measureChild()方法要求子View去測(cè)量它自身,測(cè)量的同時(shí)需要考慮到父布局的MeasureSpec要求和它自身的padding。

public static int getChildMeasureSpec(int spec, int padding, int childDimension) {
        int specMode = MeasureSpec.getMode(spec);
        int specSize = MeasureSpec.getSize(spec);

        int size = Math.max(0, specSize - padding);

        int resultSize = 0;
        int resultMode = 0;

        switch (specMode) {
        // Parent has imposed an exact size on us
        case MeasureSpec.EXACTLY:
            if (childDimension >= 0) {
                resultSize = childDimension;
                resultMode = MeasureSpec.EXACTLY;
            } else if (childDimension == LayoutParams.MATCH_PARENT) {
                // Child wants to be our size. So be it.
                resultSize = size;
                resultMode = MeasureSpec.EXACTLY;
            } else if (childDimension == LayoutParams.WRAP_CONTENT) {
                // Child wants to determine its own size. It can't be
                // bigger than us.
                resultSize = size;
                resultMode = MeasureSpec.AT_MOST;
            }
            break;

        // Parent has imposed a maximum size on us
        case MeasureSpec.AT_MOST:
            if (childDimension >= 0) {
                // Child wants a specific size... so be it
                resultSize = childDimension;
                resultMode = MeasureSpec.EXACTLY;
            } else if (childDimension == LayoutParams.MATCH_PARENT) {
                // Child wants to be our size, but our size is not fixed.
                // Constrain child to not be bigger than us.
                resultSize = size;
                resultMode = MeasureSpec.AT_MOST;
            } else if (childDimension == LayoutParams.WRAP_CONTENT) {
                // Child wants to determine its own size. It can't be
                // bigger than us.
                resultSize = size;
                resultMode = MeasureSpec.AT_MOST;
            }
            break;

        // Parent asked to see how big we want to be
        case MeasureSpec.UNSPECIFIED:
            if (childDimension >= 0) {
                // Child wants a specific size... let him have it
                resultSize = childDimension;
                resultMode = MeasureSpec.EXACTLY;
            } else if (childDimension == LayoutParams.MATCH_PARENT) {
                // Child wants to be our size... find out how big it should
                // be
                resultSize = View.sUseZeroUnspecifiedMeasureSpec ? 0 : size;
                resultMode = MeasureSpec.UNSPECIFIED;
            } else if (childDimension == LayoutParams.WRAP_CONTENT) {
                // Child wants to determine its own size.... find out how
                // big it should be
                resultSize = View.sUseZeroUnspecifiedMeasureSpec ? 0 : size;
                resultMode = MeasureSpec.UNSPECIFIED;
            }
            break;
        }
        //noinspection ResourceType
        return MeasureSpec.makeMeasureSpec(resultSize, resultMode);
}

這個(gè)方法做了測(cè)量子View過程中復(fù)雜的工作,計(jì)算出MeasureSpec傳遞給特定的子節(jié)點(diǎn),目標(biāo)是根據(jù)來自MeasureSpec的信息以及子View的LayoutParams信息去得到一個(gè)最可能的結(jié)果。

4.4 DecorView的測(cè)量

DecorView繼承了FrameLayout,F(xiàn)rameLayout又繼承了ViewGroup,它重寫了onMeasure()方法,并且調(diào)用了父類的onMeasure()方法,在遍歷循環(huán)去測(cè)量它的子View,之后又調(diào)用了setMeasuredDimension()。

//DecorView
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
     final DisplayMetrics metrics = getContext().getResources().getDisplayMetrics();
     final boolean isPortrait = getResources().getConfiguration().orientation == ORIENTATION_PORTRAIT;
  
     final int widthMode = getMode(widthMeasureSpec);
     final int heightMode = getMode(heightMeasureSpec);     
     ...
     super.onMeasure(widthMeasureSpec, heightMeasureSpec);
     ...   
}
//FrameLayout
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
   int count = getChildCount();
   ...
   for (int i = 0; i < count; i++) {
            final View child = getChildAt(i);
            if (mMeasureAllChildren || child.getVisibility() != GONE) {
                measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0);
                final LayoutParams lp = (LayoutParams) child.getLayoutParams();
                maxWidth = Math.max(maxWidth,
                        child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin);
                maxHeight = Math.max(maxHeight,
                        child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin);
                childState = combineMeasuredStates(childState, child.getMeasuredState());
                if (measureMatchParentChildren) {
                    if (lp.width == LayoutParams.MATCH_PARENT ||
                            lp.height == LayoutParams.MATCH_PARENT) {
                        mMatchParentChildren.add(child);
                    }
                }
            }
    }
    ...
    setMeasuredDimension(resolveSizeAndState(maxWidth, widthMeasureSpec, childState),
                resolveSizeAndState(maxHeight, heightMeasureSpec,
                        childState << MEASURED_HEIGHT_STATE_SHIFT));
    ...
}

5.layout階段

當(dāng)measure階段完成后,就會(huì)進(jìn)入到layout布局階段,根據(jù)View測(cè)量的結(jié)果和其他參數(shù)來確定View應(yīng)該擺放的位置。

5.1 performLayout()方法

測(cè)量完成后,在performTraverserals()方法中,會(huì)執(zhí)行performLayout()方法,開始布局過程。

private void performLayout(WindowManager.LayoutParams lp, int desiredWindowWidth,
             int desiredWindowHeight) {
    mScrollMayChange = true;
    mInLayout = true;
    final View host = mView;
    if (host == null) {
        return;
    }
    ...
    host.layout(0, 0, host.getMeasuredWidth(), host.getMeasuredHeight());
    ...
 }

5.2 layout()方法

//ViewGroup
@Override
    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;
        }
}

這個(gè)是ViewGroup的layout()方法,它是一個(gè)final類型的方法,在其內(nèi)部又調(diào)用了父類View的layout()方法。

//View
@SuppressWarnings({"unchecked"})
public void layout(int l, int t, int r, int b) {
         ...
         boolean changed = isLayoutModeOptical(mParent) ?
                 setOpticalFrame(l, t, r, b) : setFrame(l, t, r, b);  
         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;
              }
          ...
          }
          ...
}

View的layout()方法作用是為它本身及其后代View分配大小和位置,派生類不應(yīng)重寫此方法,帶有子View的派生類應(yīng)該重寫onLayout()方法,參數(shù)l、t、r、b指的是相對(duì)于父View的位置。

5.3 setFrame()方法

//View
protected boolean setFrame(int left, int top, int right, int bottom) {
         boolean changed = false;
         ...
         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;
           
           
            if (sizeChanged) {
                sizeChange(newWidth, newHeight, oldWidth, oldHeight);
            }
             ...
         }
         return changed;
}

在View的layout()方法內(nèi)會(huì)調(diào)用setFrame()方法,其作用是給這個(gè)視圖分配一個(gè)大小和位置,如果新的大小和位置與原來的不同,那么返回值為true。

5.4 onLayout()方法

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

View的onLayout()方法是一個(gè)空方法,內(nèi)部沒有代碼實(shí)現(xiàn),帶有子節(jié)點(diǎn)的派生類應(yīng)該重寫此方法,并在其每個(gè)子節(jié)點(diǎn)上調(diào)用layout。

//ViewGroup
@Override
protected abstract void onLayout(boolean changed,
            int l, int t, int r, int b);

ViewGroup的onLayout()方法是一個(gè)抽象方法,因此直接繼承ViewGroup的類需要重寫此方法。

//DecorView
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    super.onLayout(changed, left, top, right, bottom);
    ...
}

5.5 DecorView的布局

//DecorView
@Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        if (mApplyFloatingVerticalInsets) {
            offsetTopAndBottom(mFloatingInsets.top);
        }
        if (mApplyFloatingHorizontalInsets) {
            offsetLeftAndRight(mFloatingInsets.left);
        }

        // If the application changed its SystemUI metrics, we might also have to adapt
        // our shadow elevation.
        updateElevation();
        mAllowUpdateElevation = true;

        if (changed
                && (mResizeMode == RESIZE_MODE_DOCKED_DIVIDER
                    || mDrawLegacyNavigationBarBackground)) {
            getViewRootImpl().requestInvalidateRootRenderNode();
        }
}

DecorView重寫了onLayout()方法,并且調(diào)用了其父類FrameLayout的onLayout()方法。

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

void layoutChildren(int left, int top, int right, int bottom, boolean forceLeftGravity) {
     final int count = getChildCount();
  
     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();
             ...
             child.layout(childLeft, childTop, childLeft + width, childTop + height);
         }
      }
}

在FrameLayout的onLayout()方法中,調(diào)用了layoutChildren()方法,在此方法內(nèi)開啟循環(huán),讓子View調(diào)用layout()去完成布局。

6.draw階段

當(dāng)測(cè)量和布局階段完成后,就進(jìn)入了繪制階段,在這個(gè)階段,將View繪制到畫布上。

6.1 performDraw()方法

在performTraverserals()方法中,會(huì)執(zhí)行performDraw()方法,開始繪制過程。

private void performDraw() {
       if (mAttachInfo.mDisplayState == Display.STATE_OFF && !mReportNextDraw) {
            return;
       } else if (mView == null) {
            return;
       }
       ...
       boolean canUseAsync = draw(fullRedrawNeeded);
       ...
}

private boolean draw(boolean fullRedrawNeeded) {
        Surface surface = mSurface;
        if (!surface.isValid()) {
            return false;
        }
       ...
       if (!drawSoftware(surface, mAttachInfo, xOffset, yOffset,
                         scalingRequired, dirty, surfaceInsets)) {
                     return false;
       }
       ...
}

private boolean drawSoftware(Surface surface, AttachInfo attachInfo, int xoff, int yoff,
            boolean scalingRequired, Rect dirty, Rect surfaceInsets) {
         // Draw with software renderer.
       final Canvas canvas;
       ...
       mView.draw(canvas);
       ...
}

6.2 draw()方法

//View
@CallSuper
public void draw(Canvas canvas) {
        final int privateFlags = mPrivateFlags;
        mPrivateFlags = (privateFlags & ~PFLAG_DIRTY_MASK) | PFLAG_DRAWN;
        // Step 1, draw the background, if needed
        int saveCount;

        drawBackground(canvas);
            ...
        // Step 3, draw the content
        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;
            ...
        canvas.restoreToCount(saveCount);

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

        // Step 7, draw the default focus highlight
        drawDefaultFocusHighlight(canvas);

        if (isShowingLayoutBounds()) {
            debugDrawFocus(canvas);
        }   
}

View的draw()方法將View和它的子View渲染到給定的畫布,此方法在被調(diào)用之前需要先完成布局工作。

6.3 onDraw()方法

//View
protected void onDraw(Canvas canvas) {
  
}

View的onDraw()方法是一個(gè)空方法,內(nèi)部沒有代碼實(shí)現(xiàn),通過重寫這個(gè)方法,可以實(shí)現(xiàn)我們自己的繪制。

6.4 dispatchDraw()方法

//View
protected void dispatchDraw(Canvas canvas) {

}

View的dispatchDraw()方法是一個(gè)空方法,它被draw()調(diào)用去繪制子View,在派生類的子節(jié)點(diǎn)View被繪制之前,它可以被派生類重寫,這樣派生類就獲得了控制權(quán)。

//ViewGroup
@Override
protected void dispatchDraw(Canvas canvas) {
        boolean usingRenderNodeProperties = canvas.isRecordingFor(mRenderNode);
        final int childrenCount = mChildrenCount;
        final View[] children = mChildren;  
            ...
        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);
            }
        }
        ... 
}

ViewGroup的dispatchDraw()方法重寫了View的dispatchDraw()方法,并在循環(huán)體內(nèi)調(diào)用drawChild()方法繪制其子View。

protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
     return child.draw(canvas, this, drawingTime);
}

drawChild()方法的作用是繪制ViewGroup的子View。

6.5 DecorView的繪制

//DecorView
@Override
public void draw(Canvas canvas) {
     super.draw(canvas);

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

DecorView重寫了draw()方法,在其內(nèi)部調(diào)用了super.draw(canvas),因?yàn)镕rameLayout和ViewGroup都沒有重寫draw()方法,所以super.draw(canvas)調(diào)用的其實(shí)是View的draw()方法,在執(zhí)行onDraw()后,會(huì)調(diào)用dispatchDraw()方法去遍歷繪制子view。

7.總結(jié)

Android View的繪制流程經(jīng)歷measure、layout、draw三個(gè)階段,ViewRootImpl類中的performTraversals()方法是繪制流程開始的地方,performTraversals()方法中包含有performMeasure()、performLayout()、performDraw()三個(gè)與View繪制相關(guān)的方法,測(cè)量、布局和繪制都是從ViewTree的根節(jié)點(diǎn)DecorView遍歷執(zhí)行的,一般我們自定義View的時(shí)候,重寫onMeasure()、onLayout()、onDraw()方法,在其中加入我們自己的業(yè)務(wù)邏輯去實(shí)現(xiàn)需求。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • View的加載流程view布局一直貫穿于整個(gè)android應(yīng)用中,不管是activity還是fragment都給我...
    ZEKI安卓學(xué)弟閱讀 378評(píng)論 0 0
  • Android中Activity是作為應(yīng)用程序的載體存在,代表著一個(gè)完整的用戶界面,提供了一個(gè)窗口來繪制各種視圖,...
    yujunjun閱讀 595評(píng)論 0 1
  • 前言 本文的目的有兩個(gè): 給對(duì)自定義View感興趣的人一些入門的指引 給正在使用自定義View的人一些更深入的解析...
    BrotherChen閱讀 451評(píng)論 0 0
  • 一、需要了解的知識(shí) DecorViewDecroView 其實(shí)是一個(gè) FrameLayout,它包含了一個(gè)垂直的 ...
    Marker_Sky閱讀 2,889評(píng)論 0 3
  • 表情是什么,我認(rèn)為表情就是表現(xiàn)出來的情緒。表情可以傳達(dá)很多信息。高興了當(dāng)然就笑了,難過就哭了。兩者是相互影響密不可...
    Persistenc_6aea閱讀 129,700評(píng)論 2 7

友情鏈接更多精彩內(nèi)容