Android介面重新整理

來源:互聯網
上載者:User

標籤:

Android的invalidate與postInvalidate都是用來重新整理介面的,用法區別在於:

  1)invalidate():執行個體化一個Handler對象,並重寫handleMessage方法調用invalidate()實現介面重新整理;而線上程中通過sendMessage發送介面更新訊息。 

 

// 在onCreate()中開啟線程new Thread(new GameThread()).start();// 執行個體化一個handlerHandler 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;      // 發送訊息      Activity01.this.myHandler.sendMessage(message);      try {        Thread.sleep(100);      } catch (InterruptedException e) {        Thread.currentThread().interrupt();      }    }  }}

2)使用postInvalidate則比較簡單,不需要handler,直接線上程中調用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介面重新整理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.