invalidate
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的。
相关阅读
现在,各大银行的信用贷款产品越来越多,网商银行也不例外。很多朋友一直都搞不清淘宝信用贷款和网商贷的区别。小编今天就为大家介绍
Springboot @Validated和@Valid的区别 及使用
概述: @Valid是使用Hibernate validation的时候使用 @Validated是只用Spring Validator校验机制使用 说明:java的JSR303声明了@Val
在学习嵌入式的路上,我们可能会接触到这两个比较典型的MCU。其中最大的区别就是S3C2440能跑linux操作系统,常常作为学习嵌入式linux
编者按:这篇文章出自UXPin的首席执行官Marcin Treder。线框图并非是原型,但即使是经验丰富的设计师也可能会将两者混为一谈。实际上
不少网友都存在疑惑小米盒子3s和天猫魔盒3pro哪个好?小米盒子3s和天猫魔盒3pro哪款更值得买?下面seo实验室的小编带来小米盒子3s和