开发者

What is the difference between Android's invalidate() and postInvalidate() methods?

开发者 https://www.devze.com 2023-04-09 22:44 出处:网络
What is the difference between Android\'s invalidate() and 开发者_Go百科postInvalidate() methods? When does each one get called? Must the methods be called only in classes which extend View?If you wan

What is the difference between Android's invalidate() and 开发者_Go百科postInvalidate() methods? When does each one get called? Must the methods be called only in classes which extend View?


If you want to re-draw your view from the UI thread you can call invalidate() method.

If you want to re-draw your view from a non-UI thread you can call postInvalidate() method.

Each class which is derived from the View class has the invalidate and the postInvalidate method. If invalidate gets called it tells the system that the current view has changed and it should be redrawn as soon as possible. As this method can only be called from your UI thread another method is needed for when you are not in the UI thread and still want to notify the system that your View has been changed. The postInvalidate method notifies the system from a non-UI thread and the view gets redrawn in the next event loop on the UI thread as soon as possible. It is also shortly explained in the SDK documentation:

CLICK HERE

UPDATE:

There are some problems that arise when using postInvalidate from other threads (like not having the UI updated right-away), this will be more efficient:

runOnUiThread(new Runnable() {
    public void run() {
    myImageView.setImageBitmap(image);
    imageView.invalidate();
    }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号