View 繪製體系知識梳理(6) 繪製過程之 requestLayout 和 invalidate 詳解

1、概述

通過前面三篇文章的分析:bash

對於繪製的整個分發過程已經有了一個大體的瞭解,咱們能夠發現一個規律,不管是測量、佈局仍是繪製,對於任何一個View/Group來講,它都是一個至上而下的遞歸事件調用,直到到達整個View樹的葉節點爲止。 下面,咱們來分析幾個平時經常使用的方法:app

  • requestLayout
  • invalidate
  • postInvalidate

2、requestLayout

requestLayout是在View中定義的,而且在ViewGroup中沒有重寫該方法,它的註釋是這樣解釋的:在須要刷新View的佈局時調用這個函數,它會安排一個佈局的傳遞。咱們不該該在佈局的過程當中(isInLayout())調用這個函數,若是當前正在佈局,那麼這一請求有可能在如下時刻被執行:當前佈局結束、當前幀被繪製完或者下次佈局發生時。ide

/**
     * Call this when something has changed which has invalidated the
     * layout of this view. This will schedule a layout pass of the view
     * tree. This should not be called while the view hierarchy is currently in a layout
     * pass ({@link #isInLayout()}. If layout is happening, the request may be honored at the
     * end of the current layout pass (and then layout will run again) or after the current
     * frame is drawn and the next layout occurs.
     *
     * <p>Subclasses which override this method should call the superclass method to
     * handle possible request-during-layout errors correctly.</p>
     */
    @CallSuper
    public void requestLayout() {
        if (mMeasureCache != null) mMeasureCache.clear();

        if (mAttachInfo != null && mAttachInfo.mViewRequestingLayout == null) {
            // Only trigger request-during-layout logic if this is the view requesting it,
            // not the views in its parent hierarchy
            ViewRootImpl viewRoot = getViewRootImpl();
            if (viewRoot != null && viewRoot.isInLayout()) {
                if (!viewRoot.requestLayoutDuringLayout(this)) {
                    return;
                }
            }
            mAttachInfo.mViewRequestingLayout = this;
        }

        mPrivateFlags |= PFLAG_FORCE_LAYOUT;
        mPrivateFlags |= PFLAG_INVALIDATED;

        if (mParent != null && !mParent.isLayoutRequested()) {
            mParent.requestLayout();
        }
        if (mAttachInfo != null && mAttachInfo.mViewRequestingLayout == this) {
            mAttachInfo.mViewRequestingLayout = null;
        }
    }
複製代碼

在上面的代碼當中,設置了兩個標誌位:PFLAG_FORCE_LAYOUT/PFLAG_INVALIDATED,除此以外最關鍵的一句話是:函數

protected ViewParent mParent;
//....
mParent.requestLayout();
複製代碼

這個mParent存儲的時候該View所對應的父節點,而當調用父節點的requestLayout()時,它又會調用它的父節點的requestLayout,就這樣,以調用requestLayoutView爲起始節點,一步步沿着View樹傳遞上去,那麼這個過程何時會終止呢? 根據前面的分析,咱們知道整個View樹的根節點是DecorView,那麼咱們須要看一下DecorViewmParent變量是什麼,回到ViewRootImplsetView方法當中,有這麼一句:oop

view.assignParent(this);
複製代碼

所以,DecorView中的mParent就是ViewRootImpl,而ViewRootImpl中的mView就是DecorView,因此,這一傳遞過程的終點就是ViewRootImplrequestLayout方法:佈局

//ViewRootImpl中的requestLayout方法.
    @Override
    public void requestLayout() {
        if (!mHandlingLayoutInLayoutRequest) {
            checkThread();
            mLayoutRequested = true;
            scheduleTraversals();
        }
    }

    void scheduleTraversals() {
        if (!mTraversalScheduled) {
            mTraversalScheduled = true;
            mTraversalBarrier = mHandler.getLooper().getQueue().postSyncBarrier();
            //該Runnable進行操做doTraversal.
            mChoreographer.postCallback(Choreographer.CALLBACK_TRAVERSAL, mTraversalRunnable, null);
            if (!mUnbufferedInputDispatch) {
                scheduleConsumeBatchedInput();
            }
            notifyRendererOfFramePending();
            pokeDrawLockIfNeeded();
        }
    }

    final class TraversalRunnable implements Runnable {
        @Override
        public void run() {
            doTraversal();
        }
    }

    void doTraversal() {
        if (mTraversalScheduled) {
            mTraversalScheduled = false;
            mHandler.getLooper().getQueue().removeSyncBarrier(mTraversalBarrier);

            if (mProfile) {
                Debug.startMethodTracing("ViewAncestor");
            }
            //這裏最終會進行佈局.
            performTraversals();

            if (mProfile) {
                Debug.stopMethodTracing();
                mProfile = false;
            }
        }
    }
複製代碼

其中scheduleTraversals()中會執行一個mTraversalRunnable,該Runnable中最終會調用doTraversal,而doTraversal中執行的就是咱們前面一直在談到的performTraversals。 那麼,前面咱們分析過,performTraversalsmeasure方法會從根節點調用子節點的測量操做,並依次傳遞下去,那麼是否全部的子View都有必要從新測量呢,這就須要咱們在調用ViewrequestLayout是設置的標誌位PFLAG_FORCE_LAYOUT來判斷,在measure當中,調用onMeasure以前,會有這麼一個判斷條件:post

if ((mPrivateFlags & PFLAG_FORCE_LAYOUT) == PFLAG_FORCE_LAYOUT ||
                widthMeasureSpec != mOldWidthMeasureSpec ||
                heightMeasureSpec != mOldHeightMeasureSpec) {
    onMeasure(widthMeasureSpec, heightMeasureSpec);
}
複製代碼

這個標誌位會在layout完成以後被恢復:ui

public void layout(int l, int t, int r, int b) {
        if (changed || (mPrivateFlags & PFLAG_LAYOUT_REQUIRED) == PFLAG_LAYOUT_REQUIRED) {
            onLayout(changed, l, t, r, b);
        }
        mPrivateFlags &= ~PFLAG_FORCE_LAYOUT;
        mPrivateFlags3 |= PFLAG3_IS_LAID_OUT;
    }
複製代碼

在進行完layout以後,requestLayout()所引起的過程就此終止了,它不會調用draw,不會從新繪製任何視圖包括該調用者自己。this

3、invalidate

invalidate最終會調用到下面這個方法:spa

void invalidateInternal(int l, int t, int r, int b, boolean invalidateCache,
            boolean fullInvalidate) {
        if (mGhostView != null) {
            mGhostView.invalidate(true);
            return;
        }

        if (skipInvalidate()) {
            return;
        }

        if ((mPrivateFlags & (PFLAG_DRAWN | PFLAG_HAS_BOUNDS)) == (PFLAG_DRAWN | PFLAG_HAS_BOUNDS)
                || (invalidateCache && (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID)
                || (mPrivateFlags & PFLAG_INVALIDATED) != PFLAG_INVALIDATED
                || (fullInvalidate && isOpaque() != mLastIsOpaque)) {
            if (fullInvalidate) {
                mLastIsOpaque = isOpaque();
                mPrivateFlags &= ~PFLAG_DRAWN;
            }

            mPrivateFlags |= PFLAG_DIRTY;

            if (invalidateCache) {
                mPrivateFlags |= PFLAG_INVALIDATED;
                mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
            }

            // Propagate the damage rectangle to the parent view.
            final AttachInfo ai = mAttachInfo;
            final ViewParent p = mParent;
            if (p != null && ai != null && l < r && t < b) {
                final Rect damage = ai.mTmpInvalRect;
                damage.set(l, t, r, b);
                p.invalidateChild(this, damage);
            }

            // Damage the entire projection receiver, if necessary.
            if (mBackground != null && mBackground.isProjected()) {
                final View receiver = getProjectionReceiver();
                if (receiver != null) {
                    receiver.damageInParent();
                }
            }

            // Damage the entire IsolatedZVolume receiving this view's shadow. if (isHardwareAccelerated() && getZ() != 0) { damageShadowReceiver(); } } } 複製代碼

其中,關鍵的一句是:

p.invalidateChild(this, damage);
複製代碼

在這裏,p必定不爲空而且它必定是一個ViewGroup,那麼咱們來看一下ViewGroup的這個方法:

public final void invalidateChild(View child, final Rect dirty) {
    do {
        parent = parent.invalidateChildInParent(location, dirty);
    } while (parent != null);
}
複製代碼

ViewGroup當中的invalidateChildInParent會根據傳入的區域來決定本身的繪製區域,和requestLayout相似,最終會調用ViewRootImpl的該方法:

@Override
    public ViewParent invalidateChildInParent(int[] location, Rect dirty) {
        checkThread();
        if (DEBUG_DRAW) Log.v(TAG, "Invalidate child: " + dirty);

        if (dirty == null) {
            invalidate();
            return null;
        } else if (dirty.isEmpty() && !mIsAnimating) {
            return null;
        }

        if (mCurScrollY != 0 || mTranslator != null) {
            mTempRect.set(dirty);
            dirty = mTempRect;
            if (mCurScrollY != 0) {
                dirty.offset(0, -mCurScrollY);
            }
            if (mTranslator != null) {
                mTranslator.translateRectInAppWindowToScreen(dirty);
            }
            if (mAttachInfo.mScalingRequired) {
                dirty.inset(-1, -1);
            }
        }
        invalidateRectOnScreen(dirty);
        return null;
    }
複製代碼

這其中又會調用invalidate

void invalidate() {
        mDirty.set(0, 0, mWidth, mHeight);
        if (!mWillDrawSoon) {
            scheduleTraversals();
        }
    }
複製代碼

這裏,最終又會走到前面說的performTraversals()方法,請求重繪View樹,即draw()過程,假如視圖發生大小沒有變化就不會調用layout()過程,而且只繪製那些須要重繪的視圖。

3、其它知識點

  • invalidate,請求從新draw,只會繪製調用者自己。
  • setSelection,同上。
  • setVisibility:當ViewINVISIBLE變爲VISIBILE,會間接調用invalidate方法,繼而繪製該View,而從INVISIBLE/VISIBLE變爲GONE以後,因爲View樹的大小發生了變化,會進行measure/layout/draw,一樣,他只會繪製須要重繪的視圖。
  • setEnable:請求從新draw,只會繪製調用者自己。
  • requestFocus:請求從新draw,只會繪製須要重繪的視圖。

4、參考文獻

1.http://blog.csdn.net/yanbober/article/details/46128379/ 2.http://blog.csdn.net/a553181867/article/details/51583060

相關文章
相關標籤/搜索