In the multi-thread in the drawing in Android, The invalidate and postinvalidate methods are used to refresh the interface. After the two methods are called, The ondraw method is called to redraw the interface.
By default, an android program has only one process, but a process can have many threads. In such multithreading, the thread that is mainly responsible for controlling the display, update, and control interaction of the UI interface is called the UI thread, because the oncreate () method is executed by the UI thread, therefore, you can also understand the UI thread as the main thread. Other threads can be understood as worker threads.
Invalidate () must be transferred in the UI thread. In the worker thread, handler can be used to notify the UI thread to update the interface.
Postinvalidate () is called in the worker thread.
See examples:
Package COM. gameview01; <br/> Import android. content. context; <br/> Import android. graphics. canvas; <br/> Import android. graphics. color; <br/> Import android. graphics. paint; <br/> Import android. view. view; <br/> public class gameview01 extends view <br/> {<br/> int micount = 0; <br/> int y = 0; </P> <p> Public gameview01 (context) <br/>{< br/> super (context ); <br/>}</P> <p> Public void ondraw (canvas) <br/>{< br/> If (micount <100) <br/>{< br/> micount ++; <br/>}< br/> else <br/>{< br/> micount = 0; <br/>}</P> <p> // drawing <br/> paint mpaint = new paint (); <br/> switch (micount % 4) <br/>{< br/> case 0: <br/> mpaint. setcolor (color. black); <br/> break; <br/> case 1: <br/> mpaint. setcolor (color. green); <br/> break; <br/> case 2: <br/> mpaint. setcolor (color. red); <br/> break; <br/> case 3: <br/> mpaint. setcolor (color. yellow); <br/> break; <br/> default: <br/> mpaint. setcolor (color. white); <br/> break; <br/>}</P> <p> // draw a rectangle <br/> canvas. drawrect (320-80)/2, Y, (320-80)/2 + 80, Y + 40, mpaint); <br/>}< br/>}
Package COM. gameview01; <br/> Import android. app. activity; <br/> Import android. OS. bundle; <br/> Import android. OS. handler; <br/> Import android. OS. message; <br/> Import android. view. keyevent; <br/> Import android. view. motionevent; <br/> public class activity01 extends activity <br/>{< br/> Private Static final int refresh = 0x000001; </P> <p>/* declare a gameview Class Object */<br/> private gameview01 mgameview = NULL; <B R/>/** called when the activity is first created. */<br/> @ override <br/> Public void oncreate (bundle savedinstancestate) <br/>{< br/> super. oncreate (savedinstancestate); <br/>/* instantiate the gameview object */<br/> This. mgameview = new gameview01 (this); <br/> // set to display the custom view (gameview) <br/> setcontentview (mgameview ); <br/> // enable the thread <br/> New thread (New gamethread ()). start (); <br/>}< br/> handler myhand Ler = new handler () <br/>{< br/> // After receiving the message <br/> Public void handlemessage (Message MSG) <br/>{< br/> switch (MSG. what) <br/>{< br/> case activity01.refresh: <br/> mgameview. invalidate (); <br/> break; <br/>}< br/> super. handlemessage (MSG); <br/>}< br/>}; <br/> class gamethread implements runnable <br/>{< br/> Public void run () <br/>{< br/> while (! Thread. currentthread (). isinterrupted () <br/>{< br/> message = new message (); <br/> message. what = activity01.refresh; <br/> // send a message <br/> activity01.this. myhandler. sendmessage (Message); <br/> try <br/>{< br/> thread. sleep (100); <br/>}< br/> catch (interruptedexception e) <br/>{< br/> thread. currentthread (). interrupt (); <br/>}< br/>/** <br/> * Of course, you can use the gamethread class Write <br/> * You can also update the interface, and it does not require <br/> * Handler to receive messages <br/> class gamethread implements runnable <br/>{< br/> Public void run () <br/>{< br/> while (! Thread. currentthread (). isinterrupted () <br/>{< br/> try <br/>{< br/> thread. sleep (100); <br/>}< br/> catch (interruptedexception e) <br/>{< br/> thread. currentthread (). interrupt (); <br/>}< br/> // use postinvalidate to directly update the interface in the thread <br/> mgameview. postinvalidate (); <br/>}< br/>} */</P> <p> // For details about event handling, see Chapter 3 <br/> // of course. events can also be written in gameview <br/> // trigger event <br/> Public Boolean ontouchevent (motionevent event) <br/>{< br/> return true; <br/>}</P> <p> // press the button <br/> Public Boolean onkeydown (INT keycode, keyevent event) <br/>{< br/> return true; <br/>}</P> <p> // press the button to bring up the event <br/> Public Boolean onkeyup (INT keycode, keyevent event) <br/>{< br/> switch (keycode) <br/>{< br/> // The up arrow key <br/> case keyevent. keycode_dpad_up: <br/> mgameview. y-= 3; <br/> break; <br/> // arrow key <br/> case keyevent. keycode_dpad_down: <br/> mgameview. Y + = 3; <br/> break; <br/>}< br/> return false; <br/>}</P> <p> Public Boolean onkeymultiple (INT keycode, int repeatcount, keyevent event) <br/>{< br/> return true; <br/>}</P> <p>}