Differences between invalidate of and postInvalidate and of in Android

  • 2021-07-09 09:17:43
  • OfStack

There are two sets of methods for implementing view updates in Android, one is invalidate, which is used in the UI thread itself, and the other is postInvalidate, which is used in non-UI threads.

Android provides the Invalidate method for interface refreshing, but Invalidate cannot be called directly from a thread because it violates the single-threaded model: Android UI operations are not thread safe, and they must be called from an UI thread.

invalidate () is used to refresh View and must work in an UI thread. For example, when modifying the display of an view, call invalidate () to see the redrawn interface. The call to invalidate () is to remove the old view from the main UI thread queue pop. An Android program has only one process by default, but there can be many threads under one process.

Among so many threads, the thread that is mainly responsible for controlling the display, update and control interaction of UI interface is called UI thread. Because onCreate () method is executed by UI thread, UI thread can also be understood as the main thread. The rest of the threads can be understood as worker threads.

invalidate () has to be transferred in the UI thread, and UI thread can be notified to update the interface through Handler in the worker thread; postInvalidate () is called in the worker thread.

Refresh the interface with invalidate ()

Instantiate an Handler object, rewrite the handleMessage method and call invalidate () to refresh the interface; The interface update message is sent through sendMessage in the thread.


//  In onCreate() Open thread in  
new Thread( new GameThread()).start(); ,  
//  Instantiation 1 A handler 
Handler myHandler = new Handler() { 
//  Post-processing after receiving messages  
public void handleMessage(Message msg) { 
switch (msg.what) { 
case Activity01.REFRESH: 
mGameView.invalidate(); //  Refresh interface  
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(); 
} 
} 
} 
} 
//  In onCreate() Open thread in 
new Thread(new GameThread()).start(); , 
//  Instantiation 1 A handler
Handler myHandler = new Handler() {
//  Post-processing after receiving messages 
public void handleMessage(Message msg) {
switch (msg.what) {
case Activity01.REFRESH:
mGameView.invalidate(); //  Refresh interface 
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();
}
}
}
}

Refresh the interface using postInvalidate ()

Using postInvalidate is relatively simple, without handler, just call postInvalidate directly 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 You can update the interface directly in a thread  
mGameView.postInvalidate(); 
} 
} 
}

The above is this site to share invalidate () and postInvalidate () differences and usage, I hope to help you!


Related articles: