新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Android兩種實現(xiàn)界面的刷新操作的方法
利用Handler刷新界面
實例化一個Handler對象,并重寫handleMessage方法調(diào)用invalidate()實現(xiàn)界面刷新;而在線程中通過sendMessage發(fā)送界面更新消息。

成都創(chuàng)新互聯(lián)公司是一家專注于成都做網(wǎng)站、網(wǎng)站設(shè)計、外貿(mào)營銷網(wǎng)站建設(shè)與策劃設(shè)計,豐縣網(wǎng)站建設(shè)哪家好?成都創(chuàng)新互聯(lián)公司做網(wǎng)站,專注于網(wǎng)站建設(shè)十余年,網(wǎng)設(shè)計領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:豐縣等地區(qū)。豐縣做網(wǎng)站價格咨詢:18980820575
- // 在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;
- //發(fā)送消息
- Activity01.this.myHandler.sendMessage(message);
- try
- {
- Thread.sleep(100);
- }
- catch (InterruptedException e)
- {
- Thread.currentThread().interrupt();
- }
- }
- }
- }
使用postInvalidate()刷新界面
使用postInvalidate則比較簡單,不需要handler,直接在線程中調(diào)用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();
- }
- }
- }
當前文章:Android兩種實現(xiàn)界面的刷新操作的方法
網(wǎng)站地址:http://www.5511xx.com/article/dheggeg.html


咨詢
建站咨詢
