Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do the Android animations work under the hood?

During the past few months, I built an open-source tweening engine in Java (Universal Tween Engine) to be able to easily add smooth animations and transitions to my android games. It works like a breeze for games and is successfully used by many people (mostly in the LibGDX community). The library is generic and can be used to animate anything (Swing UI components, opengl game objects, etc). Now, I want to create an addon to the lib dedicated to android UIs, since I believe it can greatly ease the creation of very complex animations compared to the built-in animation framework.

My lib exposes a .update(float deltaTime) method that has to be called each time you want to update all the running animations. It was tailored for games since every game exposes an infinite loop, but that's not the case for UIs.

Therefore, I was wondering how the animation framework of the Android API works under the hood. Is there a static thread dedicated to animations that runs continuously and updates animations frame-by-frame and get paused until there is a new animation running?

I was thinking about something like that, but I'm not really happy with this code since it doesn't take the device refresh rate into account for instance.

like image 956
Aurelien Ribon Avatar asked Jun 01 '12 08:06

Aurelien Ribon


1 Answers

A good place to start is to take a look at how the Android view system implements it. The joy of open source.

When you call .animate() on a view, you get back a ViewPropertyAnimator which, upon startAnimation() starts a ValueAnimator.

The ValueAnimator has a Handler which drives the loop.

https://github.com/CyanogenMod/android_frameworks_base/blob/ics/core/java/android/view/ViewPropertyAnimator.java

https://github.com/CyanogenMod/android_frameworks_base/blob/ics/core/java/android/animation/ValueAnimator.java

like image 86
nEx.Software Avatar answered Oct 01 '22 06:10

nEx.Software