免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1914 | 回复: 2
打印 上一主题 下一主题

Android界面刷新的方法 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-01-29 15:52 |只看该作者 |倒序浏览
Android界面刷新的方法







Android提供了Invalidate方法实现界面刷新,但是Invalidate不能直接在线程中调用,因为他是违背了单线程模型:Android UI操作并不是线程安全的,并且这些操作必须在UI线程中调用。

Android程序中可以使用的界面刷新方法有两种,分别是利用Handler和利用postInvalidate()来实现在线程中刷新界面。

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

  2.        new Thread(new GameThread()).start();、



  3.        // 实例化一个handler

  4.        Handler myHandler   = new Handler()

  5.        {

  6.               //接收到消息后处理

  7.               public void handleMessage(Message msg)

  8.               {

  9.                      switch (msg.what)

  10.                      {

  11.                      case Activity01.REFRESH:

  12.                             mGameView.invalidate();        //刷新界面

  13.                             break;

  14.                      }

  15.                      super.handleMessage(msg);

  16.               }                  

  17.        };



  18.        class GameThread implements Runnable

  19.        {

  20.               public void run()

  21.               {

  22.                      while (!Thread.currentThread().isInterrupted())

  23.                      {

  24.                             Message message = new Message();

  25.                             message.what = Activity01.REFRESH;

  26.                             //发送消息

  27.                             Activity01.this.myHandler.sendMessage(message);

  28.                             try

  29.                             {

  30.                                    Thread.sleep(100);

  31.                             }

  32.                             catch (InterruptedException e)

  33.                             {

  34.                                    Thread.currentThread().interrupt();

  35.                             }

  36.                      }

  37.               }

  38.        }
复制代码
使用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.        }
复制代码
参考:

Android应用开发揭秘

Android文档

论坛徽章:
0
2 [报告]
发表于 2012-01-29 15:53 |只看该作者
谢谢分享

论坛徽章:
0
3 [报告]
发表于 2012-01-30 22:04 |只看该作者
收藏了。谢谢分享。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP