View re-painting for Android

Source: Internet
Author: User
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
  1. Class
    Customizeview
    Extends
    Whichview {
  2. Public
    Customizeview (context ){
  3. Super
    (Context );
  4. Final
    Handler handler =
    New
    Handler ();
  5. New
    Thread (
    New
    Runnable (){
  6. @ Override

  7. Public
     
    Void
    Run (){
  8. // Delay some minutes you desire.

  9. /* Try {
     
  10. Thread. Sleep (3000 );
     
  11. } Catch (interruptedexception e ){
     
  12. }*/

  13. Handler. Post (New
    Runnable (){
  14. Public
     
    Void
    Run (){
  15. Concreteupdateui ();
  16. Invalidate ();
  17. }
  18. });
  19. }
  20. }). Start ();
  21. }
  22. Protected
     
    Void
    Concreteupdateui (){
  23. // Add Concrete movement for UI updates.

  24. //...

  25. }
  26. }
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
  1. Class
    Customizeview
    Extends
    Textview {
  2. Public
    Customizeview (context ){
  3. Super
    (Context );
  4. New
    Thread (
    New
    Uiupdatethread (). Start ();
  5. }
  6. Class
    Uiupdatethread
    Implements
    Runnable {
  7. Final
    Handler mhandler =
    New
    Handler ();
  8. Final
    Runnable mupdateresults =
    New
    Runnable (){
  9. Public
     
    Void
    Run (){
  10. Concreteupdateui ();
  11. Invalidate ();
  12. }
  13. };
  14. Public
     
    Void
    Run (){
  15. // Delay some minutes you desire.

  16. /* Try {
     
  17. Thread. Sleep (1000*5 );
     
  18. } Catch (interruptedexception e ){
     
  19. E. printstacktrace ();
     
  20. }*/

  21. Mhandler. Post (mupdateresults );
  22. }
  23. }
  24. Protected
     
    Void
    Concreteupdateui (){
  25. // Add Concrete movement for UI updates.

  26. //...

  27. }
  28. }

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.  // ...  }}

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.