直接用Invalidate()方法会导致错误:只有主线程才能更新UI

取而代之的是可以使用postInvalidate();

原因:

最终会调用ViewRootImpl类的dispatchInvalidateDelayed(View view, long delayMilliseconds) 方法;

代码如下:

public void dispatchInvalidateDelayed(View view, long delayMilliseconds) {
Message msg = mHandler.obtainMessage(MSG_INVALIDATE, view);
mHandler.sendMessageDelayed(msg, delayMilliseconds);
//看到Handler就不用说为什么它能更新ui了吧
}

postInvalidate()方法:

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);
}
} public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
int right, int bottom) { // 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) {
final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.obtain();
info.target = this;
info.left = left;
info.top = top;
info.right = right;
info.bottom = bottom; //这个最终调用ViewRootImpl类的dispatchInvalidateRectDelayed(info, delayMilliseconds)方法;
attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
}
}

最新文章

  1. Android课程---关于Service的学习(后台运行)
  2. sublime text 3 快捷键大全
  3. js table的笔记,实现添加 td,实现搜索功能
  4. JAVAWEB安全开发
  5. load get selectone 或者selectlist 以及hql查询语句不用提交事务
  6. android 网络_网络源码查看器
  7. java 泛型类
  8. php(LAMP)开发环境配置相关问题及解决办法
  9. POJ 3905 Perfect Election
  10. python调用SOA服务
  11. 实现简易版的moment.js
  12. Java之面向对象概述,类,构造方法,static,主方法,对象
  13. Android异步处理技术
  14. [JLOI2013]删除物品 树状数组
  15. OpenShift实战(六):OpenShift日志监控EFK
  16. 时间序列函数resamlpe详解
  17. Mac上在终端上解压与压缩
  18. Linux 默认线程栈大小 调优
  19. Spring之IOC容器
  20. SpringMVC运行流称总结(DispatcherServlet-doDispatch)

热门文章

  1. ROS BY EXAMPLE 1 -- 环境设置与安装
  2. android混淆那些事
  3. comet4j
  4. Win7 64位 VS2015及MinGW环境编译矢量库agg-2.5和cairo-1.14.6
  5. DeliciousRetouch加强中文版v3.0-支持CS6--C鲁中C 2015.5 影楼磨破插件
  6. Ubuntu搭建Ruby on Rails环境
  7. hdu 1394 Minimum Inversion Number(树状数组)
  8. android 的闪屏效果
  9. 《LINUX内核设计与实现》读书笔记之第五章
  10. 第58讲:Scala中Abstract Types实战详解