public class

LayoutTransition

extends Object
java.lang.Object
   ↳ android.animation.LayoutTransition

Class Overview

This class enables automatic animations on layout changes in ViewGroup objects. To enable transitions for a layout container, create a LayoutTransition object and set it on any ViewGroup by calling setLayoutTransition(LayoutTransition). This will cause default animations to run whenever items are added to or removed from that container. To specify custom animations, use the setAnimator() method.

One of the core concepts of these transition animations is that there are two types of changes that cause the transition and four different animations that run because of those changes. The changes that trigger the transition are items being added to a container (referred to as an "appearing" transition) or removed from a container (also known as "disappearing"). Setting the visibility of views (between GONE and VISIBLE) will trigger the same add/remove logic. The animations that run due to those events are one that animates items being added, one that animates items being removed, and two that animate the other items in the container that change due to the add/remove occurrence. Users of the transition may want different animations for the changing items depending on whether they are changing due to an appearing or disappearing event, so there is one animation for each of these variations of the changing event. Most of the API of this class is concerned with setting up the basic properties of the animations used in these four situations, or with setting up custom animations for any or all of the four.

The animations specified for the transition, both the defaults and any custom animations set on the transition object, are templates only. That is, these animations exist to hold the basic animation properties, such as the duration, start delay, and properties being animated. But the actual target object, as well as the start and end values for those properties, are set automatically in the process of setting up the transition each time it runs. Each of the animations is cloned from the original copy and the clone is then populated with the dynamic values of the target being animated (such as one of the items in a layout container that is moving as a result of the layout event) as well as the values that are changing (such as the position and size of that object). The actual values that are pushed to each animation depends on what properties are specified for the animation. For example, the default CHANGE_APPEARING animation animates left, top, right, and bottom. Values for these properties are updated with the pre- and post-layout values when the transition begins. Custom animations will be similarly populated with the target and values being animated, assuming they use ObjectAnimator objects with property names that are known on the target object.

This class, and the associated XML flag for containers, animateLayoutChanges="true", provides a simple utility meant for automating changes in straightforward situations. Using LayoutTransition at multiple levels of a nested view hierarchy may not work due to the interrelationship of the various levels of layout. Also, a container that is being scrolled at the same time as items are being added or removed is probably not a good candidate for this utility, because the before/after locations calculated by LayoutTransition may not match the actual locations when the animations finish due to the container being scrolled as the animations are running. You can work around that particular issue by disabling the 'changing' animations by setting the CHANGE_APPEARING and CHANGE_DISAPPEARING animations to null, and setting the startDelay of the other animations appropriately.

Summary

Nested Classes
interface LayoutTransition.TransitionListener This interface is used for listening to starting and ending events for transitions. 
Constants
int APPEARING A flag indicating the animation that runs on those items that are appearing in the container.
int CHANGE_APPEARING A flag indicating the animation that runs on those items that are changing due to a new item appearing in the container.
int CHANGE_DISAPPEARING A flag indicating the animation that runs on those items that are changing due to an item disappearing from the container.
int DISAPPEARING A flag indicating the animation that runs on those items that are disappearing from the container.
Public Constructors
LayoutTransition()
Constructs a LayoutTransition object.
Public Methods
void addChild(ViewGroup parent, View child)
This method is called by ViewGroup when a child view is about to be added to the container.
void addTransitionListener(LayoutTransition.TransitionListener listener)
Add a listener that will be called when the bounds of the view change due to layout processing.
Animator getAnimator(int transitionType)
Gets the animation used during one of the transition types that may run.
long getDuration(int transitionType)
Gets the duration on one of the animation objects used by this transition.
TimeInterpolator getInterpolator(int transitionType)
Gets the interpolator on one of the animation objects used by this transition.
long getStagger(int transitionType)
Tets the length of time to delay between starting each animation during one of the CHANGE animations.
long getStartDelay(int transitionType)
Gets the start delay on one of the animation objects used by this transition.
List<LayoutTransition.TransitionListener> getTransitionListeners()
Gets the current list of listeners for layout changes.
void hideChild(ViewGroup parent, View child)
This method is called by ViewGroup when a child view is about to be removed from the container.
boolean isChangingLayout()
Returns true if animations are running which animate layout-related properties.
boolean isRunning()
Returns true if any of the animations in this transition are currently running.
void removeChild(ViewGroup parent, View child)
This method is called by ViewGroup when a child view is about to be removed from the container.
void removeTransitionListener(LayoutTransition.TransitionListener listener)
Remove a listener for layout changes.
void setAnimator(int transitionType, Animator animator)
Sets the animation used during one of the transition types that may run.
void setDuration(int transitionType, long duration)
Sets the duration on one of the animation objects used by this transition.
void setDuration(long duration)
Sets the duration to be used by all animations of this transition object.
void setInterpolator(int transitionType, TimeInterpolator interpolator)
Sets the interpolator on one of the animation objects used by this transition.
void setStagger(int transitionType, long duration)
Sets the length of time to delay between starting each animation during one of the CHANGE animations.
void setStartDelay(int transitionType, long delay)
Sets the start delay on one of the animation objects used by this transition.
void showChild(ViewGroup parent, View child)
This method is called by ViewGroup when a child view is about to be added to the container.
[Expand]
Inherited Methods
From class java.lang.Object

Constants

public static final int APPEARING

Since: API Level 11

A flag indicating the animation that runs on those items that are appearing in the container.

Constant Value: 2 (0x00000002)

public static final int CHANGE_APPEARING

Since: API Level 11

A flag indicating the animation that runs on those items that are changing due to a new item appearing in the container.

Constant Value: 0 (0x00000000)

public static final int CHANGE_DISAPPEARING

Since: API Level 11

A flag indicating the animation that runs on those items that are changing due to an item disappearing from the container.

Constant Value: 1 (0x00000001)

public static final int DISAPPEARING

Since: API Level 11

A flag indicating the animation that runs on those items that are disappearing from the container.

Constant Value: 3 (0x00000003)

Public Constructors

public LayoutTransition ()

Since: API Level 11

Constructs a LayoutTransition object. By default, the object will listen to layout events on any ViewGroup that it is set on and will run default animations for each type of layout event.

Public Methods

public void addChild (ViewGroup parent, View child)

Since: API Level 11

This method is called by ViewGroup when a child view is about to be added to the container. This callback starts the process of a transition; we grab the starting values, listen for changes to all of the children of the container, and start appropriate animations.

Parameters
parent The ViewGroup to which the View is being added.
child The View being added to the ViewGroup.

public void addTransitionListener (LayoutTransition.TransitionListener listener)

Since: API Level 11

Add a listener that will be called when the bounds of the view change due to layout processing.

Parameters
listener The listener that will be called when layout bounds change.

public Animator getAnimator (int transitionType)

Since: API Level 11

Gets the animation used during one of the transition types that may run.

Parameters
transitionType one of CHANGE_APPEARING, CHANGE_DISAPPEARING, APPEARING, or DISAPPEARING, which determines the animation whose duration is being set.
Returns
  • Animator The animation being used for the given transition type.

public long getDuration (int transitionType)

Since: API Level 11

Gets the duration on one of the animation objects used by this transition. The transitionType parameter determines the animation whose duration is returned.

Parameters
transitionType one of CHANGE_APPEARING, CHANGE_DISAPPEARING, APPEARING, or DISAPPEARING, which determines the animation whose duration is returned.
Returns
  • long The duration of the specified animation.
See Also

public TimeInterpolator getInterpolator (int transitionType)

Since: API Level 11

Gets the interpolator on one of the animation objects used by this transition. The transitionType parameter determines the animation whose interpolator is returned.

Parameters
transitionType one of CHANGE_APPEARING, CHANGE_DISAPPEARING, APPEARING, or DISAPPEARING, which determines the animation whose duration is being set.
Returns
  • TimeInterpolator The interpolator that the specified animation uses.

public long getStagger (int transitionType)

Since: API Level 11

Tets the length of time to delay between starting each animation during one of the CHANGE animations.

Parameters
transitionType A value of CHANGE_APPEARING or @link #CHANGE_DISAPPEARING}.
Returns
  • long The length of time, in milliseconds, to delay before launching the next animation in the sequence.

public long getStartDelay (int transitionType)

Since: API Level 11

Gets the start delay on one of the animation objects used by this transition. The transitionType parameter determines the animation whose start delay is returned.

Parameters
transitionType one of CHANGE_APPEARING, CHANGE_DISAPPEARING, APPEARING, or DISAPPEARING, which determines the animation whose start delay is returned.
Returns
  • long The start delay of the specified animation.
See Also

public List<LayoutTransition.TransitionListener> getTransitionListeners ()

Since: API Level 11

Gets the current list of listeners for layout changes.

public void hideChild (ViewGroup parent, View child)

Since: API Level 11

This method is called by ViewGroup when a child view is about to be removed from the container. This callback starts the process of a transition; we grab the starting values, listen for changes to all of the children of the container, and start appropriate animations.

Parameters
parent The ViewGroup from which the View is being removed.
child The View being removed from the ViewGroup.

public boolean isChangingLayout ()

Since: API Level 11

Returns true if animations are running which animate layout-related properties. This essentially means that either CHANGE_APPEARING or CHANGE_DISAPPEARING animations are running, since these animations operate on layout-related properties.

Returns
  • true if CHANGE_APPEARING or CHANGE_DISAPPEARING animations are currently running.

public boolean isRunning ()

Since: API Level 11

Returns true if any of the animations in this transition are currently running.

Returns
  • true if any animations in the transition are running.

public void removeChild (ViewGroup parent, View child)

Since: API Level 11

This method is called by ViewGroup when a child view is about to be removed from the container. This callback starts the process of a transition; we grab the starting values, listen for changes to all of the children of the container, and start appropriate animations.

Parameters
parent The ViewGroup from which the View is being removed.
child The View being removed from the ViewGroup.

public void removeTransitionListener (LayoutTransition.TransitionListener listener)

Since: API Level 11

Remove a listener for layout changes.

Parameters
listener The listener for layout bounds change.

public void setAnimator (int transitionType, Animator animator)

Since: API Level 11

Sets the animation used during one of the transition types that may run. Any Animator object can be used, but to be most useful in the context of layout transitions, the animation should either be a ObjectAnimator or a AnimatorSet of animations including PropertyAnimators. Also, these ObjectAnimator objects should be able to get and set values on their target objects automatically. For example, a ObjectAnimator that animates the property "left" is able to set and get the left property from the View objects being animated by the layout transition. The transition works by setting target objects and properties dynamically, according to the pre- and post-layoout values of those objects, so having animations that can handle those properties appropriately will work best for custom animation. The dynamic setting of values is only the case for the CHANGE animations; the APPEARING and DISAPPEARING animations are simply run with the values they have.

It is also worth noting that any and all animations (and their underlying PropertyValuesHolder objects) will have their start and end values set according to the pre- and post-layout values. So, for example, a custom animation on "alpha" as the CHANGE_APPEARING animation will inherit the real value of alpha on the target object (presumably 1) as its starting and ending value when the animation begins. Animations which need to use values at the beginning and end that may not match the values queried when the transition begins may need to use a different mechanism than a standard ObjectAnimator object.

Parameters
transitionType one of CHANGE_APPEARING, CHANGE_DISAPPEARING, APPEARING, or DISAPPEARING, which determines the animation whose duration is being set.
animator The animation being assigned. A value of null means that no animation will be run for the specified transitionType.

public void setDuration (int transitionType, long duration)

Since: API Level 11

Sets the duration on one of the animation objects used by this transition. The transitionType parameter determines the animation whose duration is being set.

Parameters
transitionType one of CHANGE_APPEARING, CHANGE_DISAPPEARING, APPEARING, or DISAPPEARING, which determines the animation whose duration is being set.
duration The length of time, in milliseconds, that the specified animation should run.

public void setDuration (long duration)

Since: API Level 11

Sets the duration to be used by all animations of this transition object. If you want to set the duration of just one of the animations in particular, use the setDuration(int, long) method.

Parameters
duration The length of time, in milliseconds, that the transition animations should last.

public void setInterpolator (int transitionType, TimeInterpolator interpolator)

Since: API Level 11

Sets the interpolator on one of the animation objects used by this transition. The transitionType parameter determines the animation whose interpolator is being set.

Parameters
transitionType one of CHANGE_APPEARING, CHANGE_DISAPPEARING, APPEARING, or DISAPPEARING, which determines the animation whose duration is being set.
interpolator The interpolator that the specified animation should use.

public void setStagger (int transitionType, long duration)

Since: API Level 11

Sets the length of time to delay between starting each animation during one of the CHANGE animations.

Parameters
transitionType A value of CHANGE_APPEARING or @link #CHANGE_DISAPPEARING}.
duration The length of time, in milliseconds, to delay before launching the next animation in the sequence.

public void setStartDelay (int transitionType, long delay)

Since: API Level 11

Sets the start delay on one of the animation objects used by this transition. The transitionType parameter determines the animation whose start delay is being set.

Parameters
transitionType one of CHANGE_APPEARING, CHANGE_DISAPPEARING, APPEARING, or DISAPPEARING, which determines the animation whose start delay is being set.
delay The length of time, in milliseconds, to delay before starting the animation.

public void showChild (ViewGroup parent, View child)

Since: API Level 11

This method is called by ViewGroup when a child view is about to be added to the container. This callback starts the process of a transition; we grab the starting values, listen for changes to all of the children of the container, and start appropriate animations.

Parameters
parent The ViewGroup to which the View is being added.
child The View being added to the ViewGroup.