转--Invalidate和postInvalidate的更新view区别
Android中实现view的更新有两组方法,一组是invalidate,另一组是postInvalidate,其中前者是在UI线程自身中使用,而后者在非UI线程中使用。
Android提供了Invalidate方法实现界面刷新,但是Invalidate不能直接在线程中调用,因为他是违背了单线程模型:Android
UI操作并不是线程安全的,并且这些操作必须在UI线程中调用。
Android程序中可以使用的界面刷新方法有两种,分别是利用invalidate和利用postInvalidate()来实现在线程中刷新界面。
1,利用invalidate()刷新界面
实例化一个Handler对象,并重写handleMessage方法调用invalidate()实现界面刷新; 而在线程中通过sendMessage发送界面更新消息。
代码如下:
new Thread(new
GameThread()).start();、
Handler myHandler = new
Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case Activity01.REFRESH:
mGameView.invalidate();
// 刷新界面
break;
}
super.handleMessage(msg);
}
};
GameThread implements Runnable {
public void run() {
while
(!Thread.currentThread().isInterrupted()) {
Message message = new Message();
message.what = Activity01.REFRESH;
// 发送消息
Activity01.this.myHandler.sendMessage(message);
Thread.sleep(100);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
}
2,使用postInvalidate()刷新界面
使用postInvalidate则比较简单,不需要handler,直接在线程中调用postInvalidate即可。
代码如下:
public void run() {
while (!Thread.currentThread().isInterrupted()) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
使用postInvalidate可以直接在线程中更新界面
mGameView.postInvalidate();
}
}
}
public void
postInvalidate() {
postInvalidateDelayed(0);
}
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
if (mAttachInfo != null) {
Message msg =
Message.obtain();
msg.what = AttachInfo.INVALIDATE_MSG;
msg.obj = this;
mAttachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
}
}
除了onCreate()不是运行在UI线程上的,其实其他大部分方法都是运行在UI线程上的,其实只要你没有开启新的线程,你的代码基本上都运行在UI线程上。
转--Invalidate和postInvalidate的更新view区别的更多相关文章
- invalidate()和postInvalidate()的使用与区别
Android提供了Invalidate方法实现界面刷新,但是Invalidate不能直接在线程中调用,因为他是违背了单线程模型: Android UI操作并不是线程安全的,并且这些操作必须在UI线程 ...
- Android界面刷新之invalidate与postInvalidate的区别
Android的invalidate与postInvalidate都是用来刷新界面的. 在UI主线程中,用invalidate():本质是调用View的onDraw()绘制. 主线程之外,用postI ...
- invalidate()和postInvalidate() 的区别及使用
Android提供了Invalidate方法实现界面刷新,但是Invalidate不能直接在线程中调用,因为他是违背了单线程模型:Android UI操作并不是线程安全的,并且这些操作必须在UI线程中 ...
- Android笔记:invalidate()和postInvalidate() 的区别及使用
http://blog.csdn.net/mars2639/article/details/6650876 Android提供了Invalidate方法实现界面刷新,但是Invalidate不能直接在 ...
- 【Android】android中Invalidate和postInvalidate的区别
Android中实现view的更新有两组方法,一组是invalidate,另一组是postInvalidate,其中前者是在UI线程自身中使用,而后者在非UI线程中使用. Android提供了Inva ...
- Android invalidate() 和 postInvalidate()的区别
Android提供了Invalidate方法实现界面刷新,但是Invalidate不能直接在线程中调用,因为他是违背了单线程模型:Android UI操作并不是线程安全的,并且这些操作必须在UI线程中 ...
- droid invalidate和postinvalidate的区别
Android提供了Invalidate方法实现界面刷新,但是Invalidate不能直接在线程中调用,因为他是违背了单线程模型:Android UI操作并不是线程安全的,并且这些操作必须在UI线程中 ...
- Android View 深度分析requestLayout、invalidate与postInvalidate
前言 前几篇文章中,笔者对View的三大工作流程进行了详细分析,而这篇文章则详细讲述与三大工作流程密切相关的两个方法,分别是requestLayout和invalidate,如果对Viwe的三个工作流 ...
- Android笔记:invalidate()和postInvalidate() 的区别及使用——刷新ui
Android提供了Invalidate方法实现界面刷新,但是Invalidate不能直接在线程中调用,因为他是违背了单线程模型:Android UI操作并不是线程安全的,并且这些操作必须在UI线程中 ...
随机推荐
- lua 初接触 --- The first time use Lua for programing
The first time use Lua for programing Wang Xiao 1. 关于 lua 的变量类型: lua 变量的定义与matlab有点不同: local d , f ...
- Compiler ,Interpreter, Linker
https://en.wikipedia.org/wiki/Interpreter_(computing) https://en.wikipedia.org/wiki/Compiler https:/ ...
- firework便捷截LOGO
1.魔术棒选空白部分 2.按delete键 3.符合画布
- 木匠ing[索引]
古人有云,一个不会写代码的木匠不会是个好厨子. 为了响应这个号召,开始我的木工之路. 首先介绍一个网站,www.zuojiaju.com 木工爱好者 ,里面有大量的关于木匠的帖子,感谢一下. 以前只是 ...
- linux服务之git
http://www.cnblogs.com/fnng/archive/2011/08/25/2153807.html http://www.cnblogs.com/sunada2005/archiv ...
- 【转】ASP.NET中服务器控件Table动态生成表格及其属性介绍
下文所有内容转自开源中国:http://www.oschina.net/question/565065_86453#tags_nav ================================= ...
- Innodb IO优化 — 数据库表设计 转
数据库表设计这块学问比较多,我这里单从互联网角度出发同时结合Innodb的特性给出一些设计方法供大家参考.本文构建大概分两分部分:Innodb的特性及设计中如何利用这种特性. Innodb特性: In ...
- javascript 返回数组中不重复的元素
这是实现结构伪类type-of-type的部分代码: <script type="text/javascript"> var ret= ["span" ...
- eclipse中tomcat加gc日志输出
-XX:ParallelGCThreads=4 -XX:+PrintGCDetails
- Android MVC模式
Android MVC模式 下面是我对Android MVC模式的理解 Model 模型层 包括实体模型层,存放程序中调用的实体. 业务模型层,存放程序中调用的业务逻辑. View 显示层 An ...