What Does Postinvalidate() Do

What does postInvalidate() do?

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 UIThread another method is needed for when you are not in the UIThread and still want to notify the system that your View has been changed. The postInvalidate method notifies the system from a non-UIThread and the View gets redrawn in the next eventloop on the UIThread as soon as possible. It is also briefly explained in the SDK documentation.

Just compare invalidate and postInvalidate.

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

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();
}
});

Android: Use postInvalidate() or a direct call to onDraw?

Yes. Calling postInvalidate sets up the canvas to the screen and passes it to the onDraw function, as well as various other pieces of logic. Calling onDraw directly only makes sense if you want to draw a view to somewhere other than the screen.

In addition, postInvalidate will cause it to redraw after control returns to the looper and combines multiple reasons to redraw into a single redraw. It will not try to draw immediately. Calling onDraw would cause it to draw immediately, which may cause significant performance issues. It will not batch drawing requests, causing it to redraw multiple times.

There's others, but the end result is that unless you're trying to draw to a non-standard canvas, don't call onDraw directly- call invalidate or postInvalidate.

Why validate on View does not work but postInvalidate works

Typical problem of novice Android developers.
Please look into the description of the invalidate method.
According to the Google documentation:

Invalidate the whole view. If the view is visible, onDraw(android.graphics.Canvas) will be called at some point in the future. This must be called from a UI thread. To call from a non-UI thread, call postInvalidate().

So, I suppose onAnimationEnd is calling from non-UI thread (probably animation thread).
I think if you avoid the API policy it may lead to unexpected behavior and may differs between different Android versions.
Calling invalidate from non-UI thread can interrupt some UI operations (or brake some logic) in Android system which makes some misbehavior.

Look also here: What does postInvalidate() do?

Painting with Android - invalidate() or postInvalidate()

Any callback to a touch event is on the UI thread. Your understanding is correct but you are wrong about the thread that is being used. If this was on a background thread you would use postInvalidate().

One thing to remember is that code you have in the Activity class doesn't necessarily run on the UI thread. When the documentation talks about running on the UI it means that the code must be executed on the UI thread. Any code in any class can be run on the UI thread using a few different methods. With this in mind it is possible to execute the same code on the UI thread as well as in a background thread. Check out the example below which would live inside an Activity.

UI Thread:

    runOnUiThread(new Runnable() {
myFunction();
});

Background Thread:

    new Thread(new Runnable() {
myFunction();
}).start();

Custom View remove all postInvalidate()

View#clearAnimation() will cancel all animations that have been applied to the View.

In your case you are using a ValueAnimator, which means that you have to keep a reference to it and when you need to cancel the animation you should perform ValueAnimator#cancel():



private ValueAnimator circleAnimator;

...

circleAnimator = ValueAnimator.ofFloat(0f, 1f);
// setup and start `ValueAnimator`


Then, when needed to cancel the animation:



circleAnimator.cancel();




Related Topics



Leave a reply



Submit