Android 在View中更新View
直接用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 在View中更新View的更多相关文章
- android 反纠结app开发: 在线程中更新view
大体上想实现一个思路: 对一个view 的内容进行不停地变化, 通过按钮停止这种变化,以达到随机选择的目的. 开发过程中 使用textview 模拟, 建立线程 mythread = new Thr ...
- Android中不能在子线程中更新View视图的原因
这是一条规律,很多coder知道,但原因是什么呢? 如下: When a process is created for your application, its main thread is ded ...
- Android 和iOS 中关于View 的一点知识
View的概念和方法十分重要,这里将对Android 和iOS中出现的,关于视图的一些知识点进行总结,预计文章会比较长,要许多时间慢慢补充. 先转载一部分资料,感谢原作者! 原链接为:http://b ...
- android 从contentView中删除view
1. ((ViewGroup)view.getParent()).removeView(view) 2. ViewGroup rootView = (ViewGroup) this.findViewB ...
- android应用程序中获取view 的位置
1. 相对位置: getLeft() , getRight(), getTop(), getBottom() 在Android中可以把left相当于X轴值, top相当于Y轴值, 通过这两个值Andr ...
- android应用程序中获取view的位置
我们重点在获取view的y坐标,你懂的... 依次介绍以下四个方法: 1.getLocationInWindow int[] position = new int[2]; textview.getLo ...
- 老问题:Android子线程中更新UI的3种方法
在Android项目中经常有碰到这样的问题,在子线程中完成耗时操作之后要更新UI,下面就自己经历的一些项目总结一下更新的方法: 方法一:用Handler 1.主线程中定义Handler: Handle ...
- Android子线程中更新UI的4种方法
方法一:用Handler 1.主线程中定义Handler: Handler mHandler = new Handler() { @Override public void handleMessage ...
- Android中自定义View和自定义动画
Android FrameWork 层给我们提供了很多界面组件,但是在实际的商业开发中这些组件往往并不能完全满足我们的需求,这时候我们就需要自定义我们自己的视图和动画. 我们要重写系统的View就必须 ...
随机推荐
- 攻破JAVA NIO技术壁垒
转载自攻破JAVA NIO技术壁垒 概述 NIO主要有三大核心部分:Channel(通道),Buffer(缓冲区), Selector.传统IO基于字节流和字符流进行操作,而NIO基于Channel和 ...
- Redis小结
一. Redis简介 Redis是一个速度非常快的高性能的key-value存储系统.redis的出现,很大程度补偿了memcached这类key/value存储的不足.Redis支持存储五种valu ...
- java中枚举(enum)小例子。之前学过枚举但是一直没用,这里有个枚举类帮你我理解下(很肤浅)
直接上枚举类,代码简单易懂. package com.jy.modules.cims.data.interact.tbj.loan.request; /** * * @author shengzhou ...
- 使用DataOutputStream写入int类型数字不能显示
前段时间做Android系统项目需要使用DataOutputStream数据流向文件里写入数据,写入的有String类型和int类型.写入之后在代码中使用DataInputStream读出是没有问题的 ...
- Json格式的字符串转换为正常显示的日期格式
//返回自定义格式日期: 2015-07-17 13:53:37function ChangeDateFormat(jsondate) { jsondate = jsondate.replace(&q ...
- 用ADMM求解大型机器学习问题
[本文链接:http://www.cnblogs.com/breezedeus/p/3496819.html,转载请注明出处] 从等式约束的最小化问题说起: ...
- ORA-00060:等待资源时检测到死锁的一种处理方法
先执行脚本: SELECT p.sipid, a.serial#, c.object_name, b.session_id, b.oracle_username, b.os_user_name FRO ...
- Minimum configuration for openldap to proxy multiple AD into a single search base
[root@localhost ~]# cd /etc/openldap[root@localhost openldap]# cat slapd.confloglevel 0x900include / ...
- C++回调函数的应用<转自:http://blog.csdn.net/wubin1124/article/details/4386269>
一回调函数 我们经常在C++设计时通过使用回调函数可以使有些应用(如定时器事件回调处理.用回调函数记录某操作进度等)变得非常方便和符合逻辑,那么它的内在机制如何呢,怎么定义呢?它和其它函数(比如钩子函 ...
- Mac OS环境下配置Myeclipse2015的经验
反复测试装了多次,现在把成功安装的方法陈列如下: 1. 相关的资源: (1)下载 myeclipse-2015-stable-2.0-offline-installer-macosx.dmg 链接:h ...