首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >简单讲下postInvalidate与invalidate的区别

简单讲下postInvalidate与invalidate的区别

作者头像
吴延宝
发布2018-10-18 15:07:25
2K0
发布2018-10-18 15:07:25
举报

invalidate

这里我们从源码中去简单扒一下View的invalidate()的方法:

 1    /**
 2     * Invalidate the whole view. If the view is visible,
 3     * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
 4     * the future.
 5     * <p>
 6     * This must be called from a UI thread. To call from a non-UI thread, call
 7     * {@link #postInvalidate()}.
 8     */
 9    public void invalidate() {
10        invalidate(true);
11    }

从方法中的注释中看,我们知道invalidate方法会刷新整个View,并且当这个View的可见性为VISIBLE的时候,View的onDraw()方法将会被调用。另外注意的是这个方法只能在UI线程中去调用。

上面就能够基本知道invalidate方法是干什么的了。我们往下接着看源码:

 1    /**
 2     * This is where the invalidate() work actually happens. A full invalidate()
 3     * causes the drawing cache to be invalidated, but this function can be
 4     * called with invalidateCache set to false to skip that invalidation step
 5     * for cases that do not need it (for example, a component that remains at
 6     * the same dimensions with the same content).
 7     *
 8     * @param invalidateCache Whether the drawing cache for this view should be
 9     *            invalidated as well. This is usually true for a full
10     *            invalidate, but may be set to false if the View's contents or
11     *            dimensions have not changed.
12     * @hide
13     */
14    public void invalidate(boolean invalidateCache) {
15        invalidateInternal(0, 0, mRight - mLeft, mBottom - mTop, invalidateCache, true);
16    }

我们看到,invalidate()方法中是调用invalidate(true),参数true的意思是需要整体刷新,当View的内容和大小没有任何变化时我们可以传入false。刷新的细节这里不做详细介绍。

postInvalidate

接下来看下postInvalidate()的实现:

 1    /**
 2     * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
 3     * Use this to invalidate the View from a non-UI thread.</p>
 4     *
 5     * <p>This method can be invoked from outside of the UI thread
 6     * only when this View is attached to a window.</p>
 7     *
 8     * @see #invalidate()
 9     * @see #postInvalidateDelayed(long)
10     */
11    public void postInvalidate() {
12        postInvalidateDelayed(0);
13    }
14
15    public void postInvalidateDelayed(long delayMilliseconds) {
16        // We try only with the AttachInfo because there's no point in invalidating
17        // if we are not attached to our window
18        final AttachInfo attachInfo = mAttachInfo;
19        if (attachInfo != null) {
20            attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
21        }
22    }
23
24    public void dispatchInvalidateDelayed(View view, long delayMilliseconds) {
25        Message msg = mHandler.obtainMessage(MSG_INVALIDATE, view);
26        mHandler.sendMessageDelayed(msg, delayMilliseconds);
27    }

从上面的方法注释中可以知道,postInvalidate是可以在非UI线程中去调用刷新UI的,那是如何做到的呢?从上面的方法调用栈中可以看出来,调用postInvalidate方法最后会调用View中的mHander发送一个MSG_INVALIDATE的消息。mHandler是ViewRootHandler的一个实例,从ViewRootHandler的handleMessage()方法中一探究竟(方法较长,只截取部分):

 1        @Override
 2        public void handleMessage(Message msg) {
 3            switch (msg.what) {
 4            case MSG_INVALIDATE:
 5                ((View) msg.obj).invalidate();
 6                break;
 7            case MSG_INVALIDATE_RECT:
 8                final View.AttachInfo.InvalidateInfo info = (View.AttachInfo.InvalidateInfo) msg.obj;
 9                info.target.invalidate(info.left, info.top, info.right, info.bottom);
10                info.recycle();
11                break;
12            case MSG_PROCESS_INPUT_EVENTS:
13                mProcessInputEventsScheduled = false;
14                doProcessInputEvents();
15                break;

在Handler中最后还是会调用View的invalidate()方法去刷新,只不过postInvalidate()方法是通过Handler将刷新事件通知发到Handler的handlerMessage中去执行invalidate的。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-09-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 南京Android部落 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • invalidate
  • postInvalidate
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档