View re-painting for Android
Http://qaohao.javaeye.com/blog/478314
Keywords: Android
There are two sets of methods to update the view in Android: invalidate and postinvalidate. The former is used in the UI thread itself, the latter is used in non-UI threads.
The following is a description I found in the android document,
Reference
Public void invalidate ()
Invalidate the whole view. If the view is visible, ondraw (canvas)
Will be called at some point in the future. This must be called from
Ui thread. To call from a non-UI thread, call postinvalidate ().
Public void postinvalidate ()
Cause an invalidate to happen on a subsequent cycle through
Event loop. Use this to invalidate the view from a non-UI thread.
The instructions in Google's documents are simple, and these two problems are often encountered during development:
1. The view failed to be refreshed without any exception.
2. The Android Application terminates abnormally. When logcat is enabled, this exception message is displayed. Only the original thread that created a view hierarchy can touch its views.
Finally, you can only query documents online. The invalidate and postinvalidate methods must use the handler provided by Android
Implementation of re-painting, but not mentioned in the description of the document, is really simple. Specifically, call the sendmessage method of handler to send a message in the place where re-painting is required, and then the OS will trigger
The handlermessage method in handler is sent, and the invalidate or
The postinvalidate method can be used for re-painting.
The following is the view redrawing code for the invalidate method. It is for reference only:
Java code
- Class
Customizeview
Extends
Whichview {
- Public
Customizeview (context ){
- Super
(Context );
- Final
Handler handler =
New
Handler ();
- New
Thread (
New
Runnable (){
- @ Override
- Public
Void
Run (){
- // Delay some minutes you desire.
- /* Try {
- Thread. Sleep (3000 );
- } Catch (interruptedexception e ){
- }*/
- Handler. Post (New
Runnable (){
- Public
Void
Run (){
- Concreteupdateui ();
- Invalidate ();
- }
- });
- }
- }). Start ();
- }
- Protected
Void
Concreteupdateui (){
- // Add Concrete movement for UI updates.
- //...
- }
- }
class CustomizeView extends WhichView {public CustomizeView(Context context) {super(context);final Handler handler = new Handler();new Thread(new Runnable() {@Overridepublic void run() {// delay some minutes you desire./*try {Thread.sleep(3000);} catch (InterruptedException e) {}*/handler.post(new Runnable() {public void run() {concreteUpdateUI();invalidate();}});}}).start();}protected void concreteUpdateUI() {// Add concrete movement for UI updates.// ...}}
You can also achieve this.
Java code
- Class
Customizeview
Extends
Textview {
- Public
Customizeview (context ){
- Super
(Context );
- New
Thread (
New
Uiupdatethread (). Start ();
- }
- Class
Uiupdatethread
Implements
Runnable {
- Final
Handler mhandler =
New
Handler ();
- Final
Runnable mupdateresults =
New
Runnable (){
- Public
Void
Run (){
- Concreteupdateui ();
- Invalidate ();
- }
- };
- Public
Void
Run (){
- // Delay some minutes you desire.
- /* Try {
- Thread. Sleep (1000*5 );
- } Catch (interruptedexception e ){
- E. printstacktrace ();
- }*/
- Mhandler. Post (mupdateresults );
- }
- }
- Protected
Void
Concreteupdateui (){
- // Add Concrete movement for UI updates.
- //...
- }
- }
The Code provided above is just a shelf. There are only two possible scenarios in this shelf:
1) The first is the latency refresh of the android control. The modification method is to add a latency before the run method of the uiupdatethread class sends out the POST method of the handler.
Try {
Thread. Sleep (1000*5 );
} Catch (interruptedexception e ){
E. printstacktrace ();
}
For example, to describe where it will be applied. For example, if the animation before the game is started is imageview, you can use this delayed refresh to start the animation, at the beginning, I loaded a painting and changed it later.
2) The second is to regularly refresh the android control, so you have to modify the customizeview constructor and add uiupdatethread to the timer, so that you can implement regular refresh.
If you need to refresh it from time to time, the shelf I provided will not apply. You have to call the POST method of handler by yourself. This should be the most common case.
class CustomizeView extends TextView {public CustomizeView(Context context) {super(context);new Thread(new UIUpdateThread()).start();}class UIUpdateThread implements Runnable {final Handler mHandler = new Handler();final Runnable mUpdateResults = new Runnable() {public void run() {concreteUpdateUI();invalidate();}};public void run() {// delay some minutes you desire. /*try {Thread.sleep(1000 * 5);} catch (InterruptedException e) {e.printStackTrace();}*/mHandler.post(mUpdateResults);}}protected void concreteUpdateUI() {// Add concrete movement for UI updates. // ... }}