Animations can add visual cues that notify users about what's going on in your app. They are especially useful when the UI changes state, such as when new content loads or new actions become available. Animations also add a polished look to your app, which gives it a higher quality look and feel.
Android includes different animation APIs depending on what type of animation you want. This documentation provides an overview of the different ways you can add motion to your UI.
To better understand when you should use animations, also see the Material Design guide about motion.
Animate bitmaps
To animate a bitmap graphic such as an icon or illustration, use the drawable animation APIs. Usually, these animations are defined statically with a drawable resource, but you can also define the animation behavior at runtime.
For example, a nice way to communicate to the user that two actions are related is to animate a play button that transforms into a pause button when it's tapped.
For more information, read Animate drawable graphics.
Animate UI visibility and motion
When you need to change the visibility or position of views in your layout, it's best to include subtle animations to help the user understand how the UI is changing.
To move, reveal, or hide views within the current layout, you can use the
property animation system provided by the android.animation
package, available in Android 3.0
(API level 11) and higher. These APIs update the properties of your View
objects over a period of time, continuously
redrawing the view as the properties change. For example, when you change the
position properties, the view moves across the screen. When you change the alpha
property, the view fades in or out.
For the simplest way to create these animations, enable animations on your layout so that when you change the visibility of a view, an animation applies automatically. For more information, see Auto animate layout updates.
To learn how to build animations using the property animation system, read the Property animation overview. You can also see the following pages to create common animations:
Physics-based motion
Whenever possible, apply real-world physics to your animations so that they are natural-looking. For example, they should maintain momentum when their target changes and make smooth transitions during any changes.
To provide these behaviors, the Android Support library includes physics-based animation APIs that rely on the laws of physics to control how your animations occur.
Two common physics-based animations are the following:
Animations not based on physics—such as those built with ObjectAnimator
APIs—are fairly static and have a
fixed duration. If the target value changes, you must cancel the animation at
the time of target value change, re-configure the animation with a new 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,
as shown in figure 3.
Animations built by with physics-based animation APIs, such as
DynamicAnimation
, 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, as
shown in figure 4.
Animate layout changes
On Android 4.4 (API level 19) and higher, you can use the transition framework to create animations when you swap the layout within the current activity or fragment. All you need to do is specify the starting and ending layout and what type of animation you want to use. Then the system figures out and executes an animation between the two layouts. You can use this to swap out the entire UI or to move or replace just some views.
For example, when the user taps an item to see more information, you can replace the layout with the item details, applying a transition like the one shown in figure 5.
The starting and ending layout are each stored in a
Scene
, though the starting scene is
usually determined automatically from the current layout. You create a
Transition
to tell the system what
type of animation you want, then call
TransitionManager.go()
and the system runs the animation to swap the layouts.
For more information, read Animate layout changes using a transition. For sample code, check out BasicTransition .
Animate between activities
On Android 5.0 (API level 21) and higher, you can also create animations that transition between your activities. This is based on the same transition framework described in the previous section, but it lets you create animations between layouts in separate activities.
You can apply simple animations such as sliding the new activity in from the side or fading it in, but you can also create animations that transition between shared views in each activity. For example, when the user taps an item to see more information, you can transition into a new activity with an animation that seamlessly grows that item to fill the screen, like the animation shown in figure 5.
As usual, you call
startActivity()
,
but pass it a bundle of options provided by
ActivityOptions.makeSceneTransitionAnimation()
.
This bundle of options might include which views are shared between the
activities so the transition framework can connect them during the animation.
For additional resources, see: