Animation at the beginning of activity jumps
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.
Change phone number in emulator - Android development Change clickable TextView's color on focus and click? Will Eclipse ADT be Deprecated? When exactly is it leak safe to use (anonymous) inner classes? Track info of currently playing music Android: digital signature using Bezier
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
- 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.
2 Solutions collect form web for “Animation at the beginning of activity jumps”
Since there are no codes attached, I’m assuming you have some transition animation to the activity. If this is the case, it might cause the problem.Since there are two animations running simultaneously. Disable the transition and give it try.
startActivity(intent); getActivity().overridePendingTransition(0, 0);
It sounds like your draw loop UI thread is getting starved.
I’d use traceview to be sure that there aren’t any methods blocking your draw calls. http://tools.android.com/tips/traceview
This should help you determine what’s being invoked instead of the onDraw method.