Android 在View中更新View
2024-10-14 06:41:42
直接用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);
}
}
最新文章
- Android课程---关于Service的学习(后台运行)
- sublime text 3 快捷键大全
- js table的笔记,实现添加 td,实现搜索功能
- JAVAWEB安全开发
- load get selectone 或者selectlist 以及hql查询语句不用提交事务
- android 网络_网络源码查看器
- java 泛型类
- php(LAMP)开发环境配置相关问题及解决办法
- POJ 3905 Perfect Election
- python调用SOA服务
- 实现简易版的moment.js
- Java之面向对象概述,类,构造方法,static,主方法,对象
- Android异步处理技术
- [JLOI2013]删除物品 树状数组
- OpenShift实战(六):OpenShift日志监控EFK
- 时间序列函数resamlpe详解
- Mac上在终端上解压与压缩
- Linux 默认线程栈大小 调优
- Spring之IOC容器
- SpringMVC运行流称总结(DispatcherServlet-doDispatch)
热门文章
- ROS BY EXAMPLE 1 -- 环境设置与安装
- android混淆那些事
- comet4j
- Win7 64位 VS2015及MinGW环境编译矢量库agg-2.5和cairo-1.14.6
- DeliciousRetouch加强中文版v3.0-支持CS6--C鲁中C 2015.5 影楼磨破插件
- Ubuntu搭建Ruby on Rails环境
- hdu 1394 Minimum Inversion Number(树状数组)
- android 的闪屏效果
- 《LINUX内核设计与实现》读书笔记之第五章
- 第58讲:Scala中Abstract Types实战详解