android-invalidate和postInvalidate 的區別及使用
> android中實現view的更新有兩組方法,一組是invalidate,另一組是postInvalidate,其中前者是在UI執行緒自身中使用,而後者在非UI執行緒中使用。 Android提供了Invalidate方法實現介面重新整理,但是Invalidate不能直接線上程中呼叫,因為他是違背了單執行緒模型:Android UI操作並不是執行緒安全的,並且這些操作必須在UI執行緒中呼叫。 Android程式中可以使用的介面重新整理方法有兩種,分別是利用Handler和利用postInvalidate()來實現線上程中重新整理介面。 利用invalidate()重新整理介面
例項化一個Handler物件,並重寫handleMessage方法呼叫invalidate()實現介面重新整理;而線上程中通過sendMessage傳送介面更新訊息。
> 使用invalidate() 重新整理介面
new Thread( new GameThread()).start();、 // 例項化一個handler Handler myHandler = new Handler() { // 接收到訊息後處理 public void handleMessage(Message msg) { switch (msg.what) { case Activity01.REFRESH: mGameView.invalidate(); // 重新整理介面 break ; } super .handleMessage(msg); } }; class GameThread implements Runnable { public void run() { while (!Thread.currentThread().isInterrupted()) { Message message = new Message(); message.what = Activity01.REFRESH; // 傳送訊息 Activity01.this .myHandler.sendMessage(message); try { Thread.sleep(100 ); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } } }> 使用postInvalidate()重新整理介面
使用postInvalidate則比較簡單,不需要handler,直接線上程中呼叫postInvalidate即可。
class GameThread implements Runnable {
public void run() {
while (!Thread.currentThread().isInterrupted()) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
// 使用postInvalidate可以直接線上程中更新介面
mGameView.postInvalidate();
}
}
}
View 類中postInvalidate()方法原始碼如下,可見它也是用到了handler的:
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; m
AttachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
} }
...除了onCreate()是執行在UI執行緒上的,其實其他大部分方法都是執行在UI執行緒上的,其實其實只要你沒有開啟新的執行緒,你的程式碼基本上都執行在UI執行緒上。
相關文章
- View—requestLayout、invalidate 和 postInvalidate 三者的區別View
- new 和 malloc 的區別 及使用
- localStorage,sessionStorage和cookie的區別及使用SessionCookie
- SurfaceView和普通view的區別及簡單使用View
- 使用jquery和使用框架的區別jQuery框架
- npm 和 yarn 的使用區別NPMYarn
- WebGL和OpenGL的區別及關係Web
- 2020 blur()和onblur的使用區別
- Python中eval與exec的使用及區別Python
- TCP和UDP的優缺點及區別TCPUDP
- Timer和ScheduledThreadPoolExecutor的區別及原始碼分析thread原始碼
- BeautifulSoup和etree的區別和使用場景
- YII 的 with 與 joinwith 的區別和使用
- js--typeof 和 instanceof 判斷資料型別的區別及開發中的使用JS資料型別
- CountDownLatch和CyclicBarrier區別及詳解CountDownLatch
- Linux中&&和&,|和||用法及區別詳解!Linux
- javascript原始值和引用值型別及區別JavaScript型別
- apply 、call 以及 bind 的使用和區別APP
- ../和./和/的區別
- LinkedList和ArrayList的區別、Vector和ArrayList的區別
- http和https的區別/get和post的區別HTTP
- union 和union all 使用區別
- ||和??的區別
- /*和/**的區別
- Golang切片的三種簡單使用方式及區別Golang
- 一文搞懂Session和Cookie的用法及區別SessionCookie
- GO語言中string和[]byte的區別及轉換Go
- Cookie、localStorage 和 sessionStorage 的區別及應用例項CookieSession
- python語言中類和函式的作用及區別!Python函式
- PHP 中 include 和 require 的概要及區別介紹PHPUI
- Android自定義View之invalidate方法和postInvalidate方法AndroidView
- Python中threading的join和setDaemon的區別及用法[例子]Pythonthread
- rem與em的使用和區別詳解REM
- 使用者態和核心態的區別
- python中 _、__、__xx__() 區別及使用場景Python
- vue watch 和 computed 區別與使用Vue
- ArrayList和LinkedList底層原理的區別和使用場景
- BufferedReader和Scanner的用法和區別(建議多使用BufferedReader)
- button 和input 的區別及在表單form中的用法ORM