Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostInvalidateOnAnimation vs postInvalidate

Tags:

android

The question is when should i use postInvalidateOnAnimation() over postInvalidate()?

Doc says about postInvalidateOnAnimation(): Cause an invalidate to happen on the next animation time step, typically the next display frame. But what is next animation time step/next display frame? When will it be called?

like image 887
dooplaye Avatar asked Mar 23 '15 20:03

dooplaye


2 Answers

I've been puzzling over an animation glitch and stumbled upon this question. Tried to answer below:

What is the next display frame?

This is when all the drawing, layouts and inputs are processed.

The concept of vertical syncing (VSync) is used in the Choreographer class after Android 4.1. This is a signal that is sent traditionally from hardware to say the screen is ready to be redrawn. (It's originates in Cathode Ray Tubes) This is typically around 60Hz on monitors.

The Choreographer is the 'UI threads' message handling loop. On every VSync, the UI messages are processed. These messages will handle inputs and redraw the display. (The Choreographer will also do other things such as skipping frames if the UI is running too slow - this is a common message on the debug console!)

This message loop handling constitutes a frame. When will it be called - every 1/60th of a second on a 60Hz frame rate.

This video from Google I/O 2012 describes this vsync and the choreographer in more detail.

What is the difference between postInvalidateOnAnimation() over postInvalidate()?

When smooth animations are required use postInvalidateOnAnimation, when redraw is required use postInvalidate().

Reasoning:

This was quite hard to answer, both of these methods are for threads communicating with the UI Thread. So I dug into the Choreographer code

and found this in doFrame()

doCallbacks(Choreographer.CALLBACK_INPUT, frameTimeNanos);
doCallbacks(Choreographer.CALLBACK_ANIMATION, frameTimeNanos);
doCallbacks(Choreographer.CALLBACK_TRAVERSAL, frameTimeNanos);

postInvalidateOnAnimation will put a callback into the animation callbacks and postInvalidate will will put into the traversal callbacks.

The key difference for me is that the animation callbacks are called before the traversal callbacks (layouts, draws).

This means that animation call backs will be called at almost exactly 60fps (processed first) whereas the traversal callbacks may have some small jitter as the layouts and view drawing is done (less than 1/60 second jitter but probably still noticeable)

like image 70
brif Avatar answered Oct 19 '22 11:10

brif


Take a look at this class: http://developer.android.com/reference/android/view/Choreographer.html

Short desc from the docs: Coordinates the timing of animations, input and drawing.

The choreographer receives timing pulses (such as vertical synchronization) from the display subsystem then schedules work to occur as part of rendering the next display frame.

Applications typically interact with the choreographer indirectly using higher level abstractions in the animation framework or the view hierarchy. Here are some examples of things you can do using the higher-level APIs.

like image 44
Teodor Avatar answered Oct 19 '22 11:10

Teodor