您的位置:首页 > 产品设计 > UI/UE

invalidate()与postInvalidate()

2016-02-17 15:10 417 查看
invalidate()与postInvalidate()都是用来请求重绘UI的,但是invalidate()方法只能在UI线程调用,

postInvalidate()方法可以在非UI线程调用,代码如下:

invalidate();

Handler myHandler = new Handler() {
// 接收到消息后处理
public void handleMessage(Message msg) {
switch (msg.what) {
case MainActivity.REFRESH:
// 刷新界面
mGameView.invalidate();
break;
}
}
};


class GameThread implements Runnable {
public void run() {
while (!Thread.currentThread().isInterrupted()) {
Message message = new Message();
message.what = MainActivity.REFRESH;
// 发送消息
MainActivity.this.myHandler.sendMessage(message);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
}


postInvalidate();

class GameThread implements Runnable {
public void run() {
while (!Thread.currentThread().isInterrupted()) {
// 使用postInvalidate可以在非UI线程中更新界面
mGameView.postInvalidate();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ui 线程 Android invalidate