android中Invalidate和postInvalidate的更新view區(qū)別
更新時間:2013年01月08日 16:31:24 作者:
Android中實現(xiàn)view的更新有兩組方法,一組是invalidate,另一組是postInvalidate,其中前者是在UI線程自身中使用,而后者在非UI線程中使用,感興趣的朋友可以了解下哦
Android中實現(xiàn)view的更新有兩組方法,一組是invalidate,另一組是postInvalidate,其中前者是在UI線程自身中使用,而后者在非UI線程中使用。
Android提供了Invalidate方法實現(xiàn)界面刷新,但是Invalidate不能直接在線程中調(diào)用,因為他是違背了單線程模型:Android UI操作并不是線程安全的,并且這些操作必須在UI線程中調(diào)用。
Android程序中可以使用的界面刷新方法有兩種,分別是利用invalidate和利用postInvalidate()來實現(xiàn)在線程中刷新界面。
1,利用invalidate()刷新界面
實例化一個Handler對象,并重寫handleMessage方法調(diào)用invalidate()實現(xiàn)界面刷新;而在線程中通過sendMessage發(fā)送界面更新消息。
// 在onCreate()中開啟線程
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;
// 發(fā)送消息
Activity01.this.myHandler.sendMessage(message);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
}
2,使用postInvalidate()刷新界面
使用postInvalidate則比較簡單,不需要handler,直接在線程中調(diào)用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;
mAttachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
}
}
除了onCreate()不是運行在UI線程上的,其實其他大部分方法都是運行在UI線程上的,其實只要你沒有開啟新的線程,你的代碼基本上都運行在UI線程上。
Android提供了Invalidate方法實現(xiàn)界面刷新,但是Invalidate不能直接在線程中調(diào)用,因為他是違背了單線程模型:Android UI操作并不是線程安全的,并且這些操作必須在UI線程中調(diào)用。
Android程序中可以使用的界面刷新方法有兩種,分別是利用invalidate和利用postInvalidate()來實現(xiàn)在線程中刷新界面。
1,利用invalidate()刷新界面
實例化一個Handler對象,并重寫handleMessage方法調(diào)用invalidate()實現(xiàn)界面刷新;而在線程中通過sendMessage發(fā)送界面更新消息。
復(fù)制代碼 代碼如下:
// 在onCreate()中開啟線程
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;
// 發(fā)送消息
Activity01.this.myHandler.sendMessage(message);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
}
2,使用postInvalidate()刷新界面
使用postInvalidate則比較簡單,不需要handler,直接在線程中調(diào)用postInvalidate即可。
復(fù)制代碼 代碼如下:
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;
mAttachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
}
}
除了onCreate()不是運行在UI線程上的,其實其他大部分方法都是運行在UI線程上的,其實只要你沒有開啟新的線程,你的代碼基本上都運行在UI線程上。
相關(guān)文章
kotlin 定義接口并實現(xiàn)回調(diào)的例子
這篇文章主要介紹了kotlin 定義接口并實現(xiàn)回調(diào)的例子,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03Android RecyclerView自定義上拉和下拉刷新效果
這篇文章主要為大家詳細(xì)介紹了Android RecyclerView自定義上拉和下拉刷新效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-02-02Android Studio中通過CMake使用NDK并編譯自定義庫和添加預(yù)編譯庫
這篇文章是基于Android Studio 3.01版本的,NDK是R16。本文重點給大家介紹Android Studio中通過CMake使用NDK并編譯自定義庫和添加預(yù)編譯庫的相關(guān)知識,感興趣的朋友一起看看吧2018-01-01Android PickerView底部選擇框?qū)崿F(xiàn)流程詳解
本次主要介紹Android中底部彈出框的使用,使用兩個案例來說明,首先是時間選擇器,然后是自定義底部彈出框的選擇器,以下來一一說明他們的使用方法2022-09-09Android BottomNavigationView底部導(dǎo)航效果
這篇文章主要為大家詳細(xì)介紹了Android BottomNavigationView底部導(dǎo)航效果的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-01-01Android App中ViewPager所帶來的滑動沖突問題解決方法
Android中我們經(jīng)常使用ViewPager配合Fragment實現(xiàn)視圖滑動,但在實際操作中又會經(jīng)常發(fā)生方向上的沖突問題,這里我們就來總結(jié)一下Android App中ViewPager所帶來的滑動沖突問題解決方法:2016-06-06