invalidate、postInvalidate的区别

来源:互联网 发布:syslog 监听多个端口 编辑:程序博客网 时间:2024/05/22 06:49

共同点

  • 两者都是刷新界面的方法
  • 原理:将UI线程中之前旧的view删掉,重新创建一个view

区别

  • invalidate()只能在UI线程中调用,也符合主线程更新UI的原则
  • postInvalidate()可以直接在子线程中刷新UI,源码如下:

        /**    *This method can be invoked from outside of the UI thread    * only when this View is attached to a window    */  public void postInvalidate() {     postInvalidateDelayed(0);  } public void postInvalidateDelayed(long delayMilliseconds) {    final AttachInfo attachInfo = mAttachInfo;    if (attachInfo != null) {        attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);    } }
  • postInvalidate()是当View加载到window中的时候可以在UI线程外更新界面,再往下的dispatchInvalidateDelayed方法就是封装不可见得了