Android兩種實現介面的重新整理操作的方法

來源:互聯網
上載者:User

利用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.       } 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.