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