I am working on animating custom Views for my Android app. I have accomplished this via Property Animations and calling invalidate()
on the View in the onAnimationUpdate()
callback, as per https://developer.android.com/guide/topics/graphics/prop-animation.html:
Depending on what property or object you are animating, you might need to call the invalidate() method on a View to force the screen to redraw itself with the updated animated values. You do this in the onAnimationUpdate() callback.
My problem is that when these animations are run at the beginning of a newly launched Activity, they skip frames at the beginning, causing them to jump very noticeably. I have tried both:
- Starting the animation immediately from the Activity's
onCreate()
method - Starting the animation upon the
OnGlobalLayout()
callback using the ViewTreeObserver of the Activity's root view.
I did the latter since I thought maybe the animation was invoked before the layout had been finalized, but the result is the same.
With logs, I determined that the onAnimationUpdate()
callback is called consistently throughout the animation (that is, every 10-20 ms or so, from start to end). onAnimationUpdate()
simply calls invalidate()
, which should force the View to redraw itself, ideally immediately (but the documentation only claims this happens "at some point in the future"). That seems to be precisely the problem: onDraw()
is only called once or twice at the very start before not being called for about 250 ms. After this, it resumes being called every 10-20 ms, as it should have the whole time. But that block of time causes very noticeable lag in the animation.
To be clear, this problem only happens at the beginning of the Activity. If I simply set a 300 ms delay before starting the animation, it runs smoothly all the way through. But I don't like that solution, since it's hacky. It seems the problem is that onDraw()
is not called immediately upon invalidate()
near the beginning of the Activity. But, I can't figure out why this is, what's blocking onDraw()
, or how to fix it at all.
I found only this StackOverFlow thread: Animation at the beginning of activity skips frames where the poster has the identical problem. The basic code is there and the videos make the problem clear. I can post my code too, but I think that the fact that the problem appears in the most basic test app shows that there's something else going on.