In this document
Key classes
See also
Physics-based animation relies on the laws of physics to manifest a high degree of realism in animation. In our day-to-day life, when a change occurs, it comes with a physical transition that is natural for us to recognize. Similarly, animations that are more natural-looking, uninterrupted, and the ones that maintain momentum, are easily perceived by us.
How does physics-based animation work?
Physics-based animation uses the fundamentals of physics to build animations. An animation is driven by force. The animation comes to rest when the force reaches equilibrium. These animation APIs embeds physics to render a more natural-looking and flexible animation and you can expect a high degree of course correction and a reduction in jank.
Benefits
Other animations available in Android are driven by fixed durations and with the changes in animation values. To change an animation during its run without introducing any visual disruption is highly challenging.
With physics-based animation, the animation can track velocity, flawlessly correct the course, and move naturally towards the new position. Animations keep momentum by keeping the velocity continuous, which renders a smooth transition from one value to another.
- Natural-looking
- Course Correction
- Reduced visual janks
Animations are more flexible and mimic real time movements. Drawing influence from physics creates motion that is easy to understand and works holistically.
Animations keep momentum when their target changes, and end with a smoother motion.
Animations appear more responsive and smooth, and reduce overall visual disruptions.
Difference between Animator-based animation and physics-based animation
Consider a scenario where the target value needs to change during the animation.
Animations that are built by using Animator are
fairly static and have a fixed duration. To accommodate the change in the
target value, you need to cancel the animation at the time of target value
change, re-configure the animation with current value as the new start value,
and add the new target value. Visually, this process creates an abrupt stop in
the animation, and a disjointed movement afterwards.
Animations that are built by using the physics-based animation APIs are driven by force. The change in the target value results in a change in force. The new force applies on the existing velocity, which makes a continuous transition to the new target. This process results in a more natural-looking animation.
The following figures illustrate the difference between an Animator-based
animation and a physics-based animation. The animation in figure 1 is
built by using ObjectAnimator. The animation in
figure 2 is built by using physics-based APIs.
Figure 1: Animation built by using ObjectAnimator
Figure 2: Animation built by using physics-based APIs
As shown in the velocity graphs below, the Animator-based animation cannot track its velocity. After reconfiguration, the animation re-started at a near-0 velocity in the next frame. The sudden change in the velocity resulted in a visual disconnection before and after the change in the target value.
Whereas, the physics-based animation can track its velocity. The change in the target value triggers the change in force. The new force is then set on the existing velocity. Due to this, the animation can move naturally towards the new position.
Figure 3: Velocity graph of Animator-based animation
Figure 4: Velocity graph of physics-based animation
The basics
The Android framework supports physics-based animations through the
android.support.animation API. The following are the relevant
classes:
android.support.animation- This package is the primary API for animating a view. It contains classes that are used for creating and managing a physics-based animation.
DynamicAnimation- A base class of the physics-based animation package. This class manages the animation lifecycle as well as common setup among all animations.
SpringAnimation-
A direct subclass of the
DynamicAnimationclass. This class creates a spring animation that animates a property of a given view. SpringForce- This class defines properties of a spring such as damping ratio and stiffness that are used in animation.
FlingAnimation-
A direct subclass of the
DynamicAnimationclass. This class creates a fling animation that has an initial momentum and gradually slows down.
Adding support library
In order to use the physics-based support library, you must modify your app project's classpath dependencies within your development environment.
To add a support library to your application project:
- Open the
build.gradlefile of your application. - Add the support library to the
dependenciessection.dependencies { ... compile "com.android.support:support-dynamic-animation:26.1.0" }
Spring Animation
Physics-based animations are driven by force. Spring force, is one such force that guides interactivity and motion. The properties of a spring, the value, and the velocity are used in creating a spring-based animation. For more information about spring-based animations, go to Spring Animation.
Fling Animation
Fling animation, a type of physics-based animation, has an initial momentum and gradually slows down. For more information about fling-based animations, go to Fling Animation.

