Android两种实现界面的刷新操作的方法

移动开发 Android
Android提供了Invalidate方法实现界面刷新,但是Invalidate不能直接在线程中调用,因为他是违背了单线程模型:Android UI操作并不是线程安全的,并且这些操作必须在UI线程中调用。 Android程序中可以使用的界面刷新方法有两种,分别是利用Handler和利用postInvalidate()来实现在线程中刷新界面。

利用Handler刷新界面

实例化一个Handler对象,并重写handleMessage方法调用invalidate()实现界面刷新;而在线程中通过sendMessage发送界面更新消息。

  1. // 在onCreate()中开启线程 
  2.        new Thread(new GameThread()).start();、 
  3.   
  4.  
  5.        // 实例化一个handler 
  6.        Handler myHandler   = new Handler()  
  7.        { 
  8.               //接收到消息后处理 
  9.               public void handleMessage(Message msg) 
  10.               { 
  11.                      switch (msg.what) 
  12.                      { 
  13.                      case Activity01.REFRESH: 
  14.                             mGameView.invalidate();        //刷新界面 
  15.                             break
  16.                      } 
  17.                      super.handleMessage(msg); 
  18.               }                    
  19.        }; 
  20.   
  21.  
  22.        class GameThread implements Runnable 
  23.        { 
  24.               public void run() 
  25.               { 
  26.                      while (!Thread.currentThread().isInterrupted()) 
  27.                      { 
  28.                             Message message = new Message(); 
  29.                             message.what = Activity01.REFRESH; 
  30.                             //发送消息 
  31.                             Activity01.this.myHandler.sendMessage(message); 
  32.                             try 
  33.                             { 
  34.                                    Thread.sleep(100); 
  35.                             } 
  36.                             catch (InterruptedException e) 
  37.                             { 
  38.                                    Thread.currentThread().interrupt(); 
  39.                             } 
  40.                      } 
  41.               } 
  42.  
  43.        } 

使用postInvalidate()刷新界面

使用postInvalidate则比较简单,不需要handler,直接在线程中调用postInvalidate即可。

  1. class GameThread implements Runnable 
  2.       { 
  3.              public void run() 
  4.              { 
  5.                     while (!Thread.currentThread().isInterrupted()) 
  6.                     { 
  7.                            try 
  8.                            { 
  9.                                   Thread.sleep(100); 
  10.                            } 
  11.                            catch (InterruptedException e) 
  12.                            { 
  13.                                   Thread.currentThread().interrupt(); 
  14.                            } 
  15.                            //使用postInvalidate可以直接在线程中更新界面 
  16.                            mGameView.postInvalidate(); 
  17.                     } 
  18.              } 
  19.       } 

 

责任编辑:张叶青 来源: feisky博客园
相关推荐

2012-10-16 09:40:38

洗牌算法

2011-08-09 13:50:01

iPhone动画UIView

2010-10-14 14:33:15

MySQL多表联查

2010-09-28 15:12:27

Javascript

2022-02-09 07:03:01

SpringNacos服务注册

2010-04-25 17:34:30

负载均衡实现

2010-01-21 11:13:29

Linux桌面计算器

2020-09-23 09:24:01

堆栈开发实现

2011-06-23 09:07:16

2017-11-16 09:20:20

内存虚拟化技术

2022-02-21 08:18:38

option编程模式

2010-07-14 10:30:26

Perl多线程

2010-09-17 09:37:27

Java安装方法

2009-09-25 14:04:09

Hibernate eHibernate h

2021-12-08 10:47:35

RabbitMQ 实现延迟

2010-11-19 11:57:15

Oracle密码丢失

2011-03-03 17:00:37

pure-ftpdchroot

2011-04-08 13:54:32

2010-03-18 08:55:50

C#

2010-05-28 09:49:48

MySQL远程连接
点赞
收藏

51CTO技术栈公众号