Differences between Invalidate and postInvalidate in android, androidinvalidate

Source: Internet
Author: User

Differences between Invalidate and postInvalidate in android, androidinvalidate
In Android, there are two sets of methods to update the view. One is invalidate, and the other is postInvalidate. The former is in the UI line.


And the latter is used in non-UI threads.
Android provides the Invalidate method to refresh the interface, but Invalidate cannot be called directly in the thread, because it violates


Thread model: Android UI operations are not thread-safe and must be called in the UI thread.


There are two interface refresh methods available in the Android program: Handler and postInvalidate ().


Refresh the interface in the thread.


1. Use invalidate () to refresh the interface
Instantiate a Handler object and rewrite the handleMessage method to call invalidate () to refresh the interface.


The sendMessage sending interface updates the message.


// Enable the thread in onCreate ()




New Thread (new GameThread (). start ();,


// Instantiate a handler


Handler myHandler = new Handler (){
// After receiving the message
Public void handleMessage (Message msg ){
Switch (msg. what ){
Case Activity01.REFRESH:
MGameView. invalidate (); // refresh the page
Break;
}


Super. handleMessage (msg );
}
};


Class GameThread implements Runnable {
Public void run (){
While (! Thread. currentThread (). isInterrupted ()){
Message message = new Message ();
Message. what = Activity01.REFRESH;
// Send a message
Activity01.this. myHandler. sendMessage (message );
Try {
Thread. sleep (100 );
} Catch (InterruptedException e ){
Thread. currentThread (). interrupt ();
}
}
}
}


2. Use postInvalidate () to refresh the page
Using postInvalidate is relatively simple and requires no handler. You can directly call postInvalidate 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 directly update the interface in the thread
MGameView. postInvalidate ();
}
}
}

In the View class, the source code of the postInvalidate () method is as follows. It can be seen that handler is also used:
Public void postInvalidate (){
PostInvalidateDelayed (0 );
}


Public void postInvalidateDelayed (long delayMilliseconds ){
// We try only with the AttachInfo because there's no point in invalidating
// If we are not attached to our window
If (mAttachInfo! = Null ){
Message msg = Message. obtain ();
Msg. what = AttachInfo. INVALIDATE_MSG;
Msg. obj = this;
MAttachInfo. mHandler. sendMessageDelayed (msg, delayMilliseconds );
}
}


Except that onCreate () is run on the UI thread, most of the other methods are run on the UI thread.


When you start a new thread, your code basically runs on the UI thread.
What are the comprehension questions about android multithreading invalidate and postInvalidate ??

ANR is a processing method for UI threads. If the UI thread does not return after 5 seconds, the ANR dialog box will pop up to prevent applications from responding for a long time and being unable to operate.

Why does android call postInvalidate () or invalidate () without triggering onDraw?

The api seems to say that this only works in the Main drawing thread. If an independent thread is used for plotting or asynchronous, this is unnecessary.

Surface view solves all problems

Related Article

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.