Android Interface Refresh

Source: Internet
Author: User

Android Invalidate and postinvalidate are used to refresh the interface, the difference is:

1) Invalidate (): Instantiate a Handler object and override the Handlemessage method call invalidate () to implement the interface refresh, while the thread updates the message through the SendMessage send interface.

Open thread new Thread (new Gamethread ()) in OnCreate (). Start ();//Instantiate a handlerhandler MyHandler = new Handler () {//Receive message after processing p ublic void Handlemessage (Message msg) {switch (msg.what) {case Activity01.REFRESH:mGameView.invalidate () ;    Refresh interface break;  } super.handlemessage (msg); }};class Gamethread implements Runnable {public void run () {while (!      Thread.CurrentThread (). isinterrupted ()) {Message message = new Message ();      Message.what = Activity01.refresh;      Send Message Activity01.this.myHandler.sendMessage (messages);      try {thread.sleep (100);      } catch (Interruptedexception e) {thread.currentthread (). interrupt (); }    }  }}

2) using Postinvalidate is relatively simple and does not require handler to call Postinvalidate directly in the thread.

Class Gamethread implements Runnable {public void run () {while (!      Thread.CurrentThread (). isinterrupted ()) {try {thread.sleep (100);      } catch (Interruptedexception e) {thread.currentthread (). interrupt ();    }//Use Postinvalidate to update the interface Mgameview.postinvalidate () directly in the thread; }  }}


3 There is also a custom linearlayout, need to pass a lot of parameters, in the initialization of the use has been passed the initial display value, the middle may also need a variety of changes, at this time on the custom layout to write an interface to listen for changes.

Android Interface Refresh

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.