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

前端 未结 1 1270
北荒
北荒 2020-12-29 04:40

What is the difference between Android\'s invalidate() and postInvalidate() methods? When does each one get called? Must the methods be called only

相关标签:
1条回答
  • 2020-12-29 05:23

    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 讨论(0)
提交回复
热议问题