android中Invalidate和postInvalidate的差别
Android中实现view的更新有两组方法,一组是invalidate。还有一组是postInvalidate。当中前者是在UI线
程自身中使用,而后者在非UI线程中使用。
Android提供了Invalidate方法实现界面刷新。可是Invalidate不能直接在线程中调用。由于他是违背了单
线程模型:Android UI操作并非线程安全的,而且这些操作必须在UI线程中调用。
Android程序中能够使用的界面刷新方法有两种。各自是利用Handler和利用postInvalidate()来实现
在线程中刷新界面。
1,利用invalidate()刷新界面
实例化一个Handler对象,并重写handleMessage方法调用invalidate()实现界面刷新;而在线程中通过
sendMessage发送界面更新消息。
// 在onCreate()中开启线程
new Thread(new GameThread()).start();、
// 实例化一个handler
Handler myHandler = new Handler() {
// 接收到消息后处理
public void handleMessage(Message msg) {
switch (msg.what) {
case Activity01.REFRESH:
mGameView.invalidate(); // 刷新界面
break;
}
super.handleMessage(msg);
}
};
class GameThread implements Runnable {
public void run() {
while (!Thread.currentThread().isInterrupted()) {
Message message = new Message();
message.what = Activity01.REFRESH;
// 发送消息
Activity01.this.myHandler.sendMessage(message);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
}
2,使用postInvalidate()刷新界面
使用postInvalidate则比較简单,不须要handler,直接在线程中调用postInvalidate就可以。
class GameThread implements Runnable {
public void run() {
while (!Thread.currentThread().isInterrupted()) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
// 使用postInvalidate能够直接在线程中更新界面
mGameView.postInvalidate();
}
}
}
View 类中postInvalidate()方法源代码例如以下,可见它也是用到了handler的:
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线程上。
android中Invalidate和postInvalidate的差别的更多相关文章
- 【Android】android中Invalidate和postInvalidate的区别
Android中实现view的更新有两组方法,一组是invalidate,另一组是postInvalidate,其中前者是在UI线程自身中使用,而后者在非UI线程中使用. Android提供了Inva ...
- android中Invalidate和postInvalidate的区别
Android中实现view的更新有两组方法,一组是invalidate,另一组是postInvalidate,其中前者是在UI线程自身中使用,而后者在非UI线程中使用. Android提供了Inva ...
- Android中Invalidate与postInvalidate的区别<转>
http://www.cnblogs.com/it-tomorrow/archive/2012/11/08/2760146.html 示例:http://rayleung.iteye.com/blog ...
- Android中@+id和@id的差别
Android中的组件须要用一个int类型的值来表示.这个值也就是组件标签中的id属性值. id属性仅仅能接受资源类型的值,也就是必须以@开头的值,比如,@id/abc.@+id/xyz等. 假设在 ...
- 我的Android进阶之旅------>android中getLocationInWindow 和 getLocationOnScreen的差别
View.getLocationInWindow(int[] location) 一个控件在其父窗体中的坐标位置 View.getLocationOnScreen(int[] location) 一个 ...
- Android界面刷新之invalidate与postInvalidate的区别
Android的invalidate与postInvalidate都是用来刷新界面的. 在UI主线程中,用invalidate():本质是调用View的onDraw()绘制. 主线程之外,用postI ...
- Android中刷新Invalidate和postInvalidate的区别
Android中实现view的更新有两组方法,一组是invalidate,另一组是postInvalidate,其中前者是在UI线程自身中使用,而后者在非UI线程中使用.Android提供了Inval ...
- Android之界面刷新(invalidate和postInvalidate使用)
Android中实现view的更新有两组方法,一组是invalidate,另一组是postInvalidate,其中前者是在UI线程自身中使用,而后者在非UI线程中使用. Android提供了Inva ...
- 为何invalidate()不可以直接在UI线程中调用&invalidate与postInvalidate
1.android ui操作为什么一定要在主线程中执行? 答:Android UI操作是单线程模型,关于UI更新的相关API(包括invalidate())都是按照单线程设计的,对于多线程运行时不安全 ...
随机推荐
- Registering RHEL6 Clients into spacewalk
Before Starting(login to spacwalk server) 1.Create a base channel within Spacewalk (Channels > Ma ...
- poj 3729 Facer’s string
Facer’s string Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 2155 Accepted: 644 Des ...
- poj1039 Pipe(计算几何叉积求交点)
F - Pipe Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submit Sta ...
- knockout 表单绑定 要怎么Mapping才好
问题 之前有了解过knockout,学习过绑定语法,结合帮助文档,做个Demo倒也不成问题,但是部分地方很不爽,不知道是我的用法不对,还是功力不够. 比如说,标签里定义的data-bind属性名,必须 ...
- 杭电oj2047-2049、2051-2053、2056、2058
2047 阿牛的EOF牛肉串 #include<stdio.h> int main(){ int n,i; _int64 s[]; while(~scanf("%d" ...
- 【CString与string转换】不存在从 "LPWSTR" 转换到 "std::basic_string<char, std::char_traits<char>, std::allocator(转)
原文转自 http://blog.csdn.net/qq_23536063/article/details/52291332 [问题描述] CString cstr: sring str(cstr.G ...
- 非常好!!!Linux源代码阅读——环境准备【转】
Linux源代码阅读——环境准备 转自:http://home.ustc.edu.cn/~boj/courses/linux_kernel/0_prepare.html 目录 Linux 系统环境准备 ...
- c# 防止重复运行 弹出已运行窗口并传递消息
最近在写一款软件 软件是用来接收其他程序传递过来的命令行,并形成列表 大概的最终效果就像下图一样 原本为了程序美观是打算用listbox自绘列表,字和图片都绘制好了发现自己不会绘制按钮 所以最终采用了 ...
- QueryDict对象
所在的包: django.http.QueryDict HttpRequest 对象中的 GET 和 POST 属性 都是 QueryDict类型 与python字典不同:QueryDict对象一个键 ...
- shell 练习 (免密钥登陆脚本)
脚本说明 本地服务器ip 10.0.0.5 远程服务器地址 10.0.0.223 #!/bin/bashremote_ip=$ if [ ! -n "$1" ] ;then ech ...