android View 重绘机制
来源:互联网 发布:阿里云服务器承载量 编辑:程序博客网 时间:2024/05/22 04:47
android中实现view的更新有两组方法,一组是invalidate,另一组是postInvalidate,其中前者是在UI线程自身中使用,而后者在非UI线程中使用。
以下是我在android文档中找到的说明,
public void invalidate()
Invalidate the whole view. If the view is visible, onDraw(Canvas) will be called at some point in the future. This must be called from a UI thread. To call from a non-UI thread, call postInvalidate().
public void postInvalidate ()
Cause an invalidate to happen on a subsequent cycle through the event loop. Use this to invalidate the View from a non-UI thread.
google的文档的说明实在是简单,往往看了开发中都会遇到这两个问题:
1. 没有任何异常,view没能刷新。
2. android应用异常终止,打开logcat会看到这样的异常信息, Only the original thread that created a view hierarchy can touch its views。
最后,通过查文档,上网查询才知道,invalidate和postInvalidate方法需要使用android提供的handler,才能实现重绘,而在文档的说明中却只字不提,真是简单啊。具体是在需要重绘的地方调用handler的sendMessage方法发送消息,紧接着会os会触发handler中的handlerMessage方法,在handlerMessage方法中再调用view的invalidate或者postInvalidate方法就能实现重绘。
下面是我分别针对invalidate方法,给出view重绘代码,仅供参考:
或者这样实现也可以。
以下是我在android文档中找到的说明,
引用
public void invalidate()
Invalidate the whole view. If the view is visible, onDraw(Canvas) will be called at some point in the future. This must be called from a UI thread. To call from a non-UI thread, call postInvalidate().
public void postInvalidate ()
Cause an invalidate to happen on a subsequent cycle through the event loop. Use this to invalidate the View from a non-UI thread.
google的文档的说明实在是简单,往往看了开发中都会遇到这两个问题:
1. 没有任何异常,view没能刷新。
2. android应用异常终止,打开logcat会看到这样的异常信息, Only the original thread that created a view hierarchy can touch its views。
最后,通过查文档,上网查询才知道,invalidate和postInvalidate方法需要使用android提供的handler,才能实现重绘,而在文档的说明中却只字不提,真是简单啊。具体是在需要重绘的地方调用handler的sendMessage方法发送消息,紧接着会os会触发handler中的handlerMessage方法,在handlerMessage方法中再调用view的invalidate或者postInvalidate方法就能实现重绘。
下面是我分别针对invalidate方法,给出view重绘代码,仅供参考:
- class CustomizeView extends WhichView {
- public CustomizeView(Context context) {
- super(context);
- final Handler handler = new Handler();
- new Thread(new Runnable() {
- @Override
- public void run() {
- // delay some minutes you desire.
- /*try {
- Thread.sleep(3000);
- } catch (InterruptedException e) {
- }*/
- handler.post(new Runnable() {
- public void run() {
- concreteUpdateUI();
- invalidate();
- }
- });
- }
- }).start();
- }
- protected void concreteUpdateUI() {
- // Add concrete movement for UI updates.
- // ...
- }
- }
或者这样实现也可以。
- class CustomizeView extends TextView {
- public CustomizeView(Context context) {
- super(context);
- new Thread(new UIUpdateThread()).start();
- }
- class UIUpdateThread implements Runnable {
- final Handler mHandler = new Handler();
- final Runnable mUpdateResults = new Runnable() {
- public void run() {
- concreteUpdateUI();
- invalidate();
- }
- };
- public void run() {
- // delay some minutes you desire.
- /*try {
- Thread.sleep(1000 * 5);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }*/
- mHandler.post(mUpdateResults);
- }
- }
- protected void concreteUpdateUI() {
- // Add concrete movement for UI updates.
- // ...
- }
- }
- android View 重绘机制
- android view 的重绘机制
- Android之view重绘
- Android之view重绘
- android,view的重绘
- Android UI绘制之View重绘
- Android 重绘 View(2017)
- Android View原理(View树遍历,View重绘,View动画)
- 【View】Android View绘制机制
- Android View刷新机制
- Android View刷新机制
- Android View绘制机制
- Android View绘制机制
- android View 绘制机制
- Android View刷新机制
- Android View绘图机制
- Android View刷新机制
- Android View刷新机制
- WINDOWS关闭共享后怎样去掉图标上的小锁
- 最近安排。
- 如何在MFC单文档中创建多视图
- 关于struts2中表单提交时,中文乱码问题的解决
- 浏览器的渲染原理简介
- android View 重绘机制
- 选择物质还是选择爱情
- 最近总结 整理下行囊
- PHP Zend Studio9.0怎么把代码搞成和服务器端的同步(就是直接在服务器端修改)
- OpenStack Grizzly 安装指南
- 使用PDO查询Mysql来避免SQL注入风险
- 括号配对问题
- sql over开窗函数 和group by的区别
- spring mvc forward乱码的一种解决办法