美文网首页
view的更新 requestLayout 与 重绘invali

view的更新 requestLayout 与 重绘invali

作者: 大盗海洲 | 来源:发表于2019-05-21 15:48 被阅读0次

view.requestLayout()

//View.java
    /**
     * 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()) {
            // protected ViewParent mParent;
            mParent.requestLayout();
        }
        if (mAttachInfo != null && mAttachInfo.mViewRequestingLayout == this) {
            mAttachInfo.mViewRequestingLayout = null;
        }
    }
//ViewRootImpl.java
  @Override
    public void requestLayout() {
        if (!mHandlingLayoutInLayoutRequest) {
            checkThread();
            //调用performMeasure() performLayout()的判断因素
            mLayoutRequested = true;
            scheduleTraversals();
        }
    }

检查是否在UI线程

void checkThread() {
        if (mThread != Thread.currentThread()) {
            throw new CalledFromWrongThreadException(
                    "Only the original thread that created a view hierarchy can touch its views.");
        }
    }
  void scheduleTraversals() {
        if (!mTraversalScheduled) {
            mTraversalScheduled = true;
            mTraversalBarrier = mHandler.getLooper().getQueue().postSyncBarrier();
            mChoreographer.postCallback(
                    Choreographer.CALLBACK_TRAVERSAL, mTraversalRunnable, null);
            if (!mUnbufferedInputDispatch) {
                scheduleConsumeBatchedInput();
            }
            notifyRendererOfFramePending();
            pokeDrawLockIfNeeded();
        }
    }
 final TraversalRunnable mTraversalRunnable = new TraversalRunnable();
  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;
            }
        }
    }
 private void performTraversals() {
          ···
        boolean layoutRequested = mLayoutRequested && (!mStopped || mReportNextDraw);
        if (layoutRequested) {
        performMeasure(childWidthMeasureSpec, childHeightMeasureSpec);
         }
        
          ····
         final boolean didLayout = layoutRequested && (!mStopped || mReportNextDraw);
             if (didLayout) {
            performLayout(lp, mWidth, mHeight);
      }

      ···
     performDraw();
     ·····
}

总结:requestLayout 执行 performMeasure()performLayout()
performDraw()

view.invalidate();

    /**
     * Invalidate the whole view. If the view is visible,
     * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
     * the future.
     * <p>
     * This must be called from a UI thread. To call from a non-UI thread, call
     * {@link #postInvalidate()}.
     */
    //UI线程调用invalidate ,子线程调用postInvalidate
    public void invalidate() {
        invalidate(true);
    }
    /**
     * This is where the invalidate() work actually happens. A full invalidate()
     * causes the drawing cache to be invalidated, but this function can be
     * called with invalidateCache set to false to skip that invalidation step
     * for cases that do not need it (for example, a component that remains at
     * the same dimensions with the same content).
     *
     * @param invalidateCache Whether the drawing cache for this view should be
     *            invalidated as well. This is usually true for a full
     *            invalidate, but may be set to false if the View's contents or
     *            dimensions have not changed.
     * @hide
     */
  //invalidateCache 默认true,false 是view的视图和尺寸没有发生任何改变
    public void invalidate(boolean invalidateCache) {
        invalidateInternal(0, 0, mRight - mLeft, mBottom - mTop, invalidateCache, true);
    }
    void invalidateInternal(int l, int t, int r, int b, boolean invalidateCache,
            boolean fullInvalidate) {
               // 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);
            }
}

view 跳转到viewRootImpl

//ViewRootImpl.java
    @Override
    public void invalidateChild(View child, Rect dirty) {
        invalidateChildInParent(null, dirty);
    }

 @Override
    public ViewParent invalidateChildInParent(int[] location, Rect dirty) {
    //检查是否是UI线程
    checkThread();
      invalidateRectOnScreen(dirty);

        return null;

}
 private void invalidateRectOnScreen(Rect dirty) {
  if (!mWillDrawSoon && (intersected || mIsAnimating)) {
            scheduleTraversals();
        }
}

总结:scheduleTraversals 接下来的流程就和requestLayout#scheduleTraversals 的流程一致了,但会有疑惑,requestLayout就会走performMeasure() performLayout() performDraw(),而invalidate只会走performDraw(),却别在于requestLayout 方法内mLayoutRequested赋值为true

postInvalidate

 /**
     * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
     * Use this to invalidate the View from a non-UI thread.</p>
     *
     * <p>This method can be invoked from outside of the UI thread
     * only when this View is attached to a window.</p>
     *
     * @see #invalidate()
     * @see #postInvalidateDelayed(long)
     */
  //可以在非UI线程调用
    public void postInvalidate() {
        postInvalidateDelayed(0);
    }
 public void postInvalidateDelayed(long delayMilliseconds) {
        // We try only with the AttachInfo because there's no point in invalidating
        // if we are not attached to our window
        final AttachInfo attachInfo = mAttachInfo;
        if (attachInfo != null) {
            attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
        }
    }
//ViewRootImpl.java
   public void dispatchInvalidateDelayed(View view, long delayMilliseconds) {
        Message msg = mHandler.obtainMessage(MSG_INVALIDATE, view);
        mHandler.sendMessageDelayed(msg, delayMilliseconds);
    }
    final ViewRootHandler mHandler = new ViewRootHandler();
    final class ViewRootHandler extends Handler {
     @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case MSG_INVALIDATE:
                ((View) msg.obj).invalidate();
                break;
            
}}

总结:postInvalidate 内部通过ViewRootHandler 发送消息到主线程,然后调用invalidate
参考链接:https://mp.weixin.qq.com/s/_A2PPOam9HIfV-_AFLdgiw

相关文章

网友评论

      本文标题:view的更新 requestLayout 与 重绘invali

      本文链接:https://www.haomeiwen.com/subject/vuskzqtx.html