Invalidate () is used to refresh the view and must be working in the UI thread. For example, when you modify the display of a view, call invalidate () to see the redrawn interface. Invalidate () is called to pop the old view from the main UI thread queue.
For screen refreshes there are the following concentrations to consider:
1. Do not use multithreading and double buffering
This is the simplest scenario, and it's generally just a wish to redraw the UI when the view changes. You only need to explicitly invoke the Invalidate () method in the View object in the activity. The OnDraw () method of the view is automatically called.
2. Use multithreading and do not use double buffering
This situation requires a new thread to be opened, and the new thread will not have access to the View object. Forced access will be reported: Android.view.viewroot$calledfromwrongthreadexception:only the original thread that created a view hierarchy can touch its views.
At this point you need to create a subclass that inherits the Android.os.Handler and override the Handlemessage (Message msg) method. Android.os.Handler is capable of sending and processing messages, you need to emit a message in the activity that updates the UI, and then your handler (you can use anonymous inner classes) to process the message (because the anonymous inner class can access the parent class variable, You can directly invoke the invalidate () method in the View object. That is, a message is created and sent on a new thread, and then captured and processed in the main threads.
3. Using multithreading and double buffering
Surfaceview is a subclass of view in Android, and she also implements double buffering. You can define a subclass of her and implement the Surfaceholder.callback interface. With the implementation of the Surfaceholder.callback interface, the new thread does not need Android.os.Handler to help. Surfaceholder the Lockcanvas () method can lock the canvas, draw a new image after it is called unlockcanvasandpost (canvas) to unlock (display), or more convenient.
Android invalidate () Auto clear screen, screen refresh