FileDocCategorySizeDatePackage
LayoutAnimationController.javaAPI DocAndroid 1.5 API14827Wed May 06 22:41:56 BST 2009android.view.animation

LayoutAnimationController

public class LayoutAnimationController extends Object
A layout animation controller is used to animated a layout's, or a view group's, children. Each child uses the same animation but for every one of them, the animation starts at a different time. A layout animation controller is used by {@link android.view.ViewGroup} to compute the delay by which each child's animation start must be offset. The delay is computed by using characteristics of each child, like its index in the view group. This standard implementation computes the delay by multiplying a fixed amount of miliseconds by the index of the child in its parent view group. Subclasses are supposed to override {@link #getDelayForView(android.view.View)} to implement a different way of computing the delay. For instance, a {@link android.view.animation.GridLayoutAnimationController} will compute the delay based on the column and row indices of the child in its parent view group. Information used to compute the animation delay of each child are stored in an instance of {@link android.view.animation.LayoutAnimationController.AnimationParameters}, itself stored in the {@link android.view.ViewGroup.LayoutParams} of the view.
attr
ref android.R.styleable#LayoutAnimation_delay
attr
ref android.R.styleable#LayoutAnimation_animationOrder
attr
ref android.R.styleable#LayoutAnimation_interpolator
attr
ref android.R.styleable#LayoutAnimation_animation

Fields Summary
public static final int
ORDER_NORMAL
Distributes the animation delays in the order in which view were added to their view group.
public static final int
ORDER_REVERSE
Distributes the animation delays in the reverse order in which view were added to their view group.
public static final int
ORDER_RANDOM
Randomly distributes the animation delays.
protected Animation
mAnimation
The animation applied on each child of the view group on which this layout animation controller is set.
protected Random
mRandomizer
The randomizer used when the order is set to random. Subclasses should use this object to avoid creating their own.
protected Interpolator
mInterpolator
The interpolator used to interpolate the delays.
private float
mDelay
private int
mOrder
private long
mDuration
private long
mMaxDelay
Constructors Summary
public LayoutAnimationController(android.content.Context context, android.util.AttributeSet attrs)
Creates a new layout animation controller from external resources.

param
context the Context the view group is running in, through which it can access the resources
param
attrs the attributes of the XML tag that is inflating the layout animation controller

    

                                                                 
         
        TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.LayoutAnimation);

        Animation.Description d = Animation.Description.parseValue(
                a.peekValue(com.android.internal.R.styleable.LayoutAnimation_delay));
        mDelay = d.value;

        mOrder = a.getInt(com.android.internal.R.styleable.LayoutAnimation_animationOrder, ORDER_NORMAL);

        int resource = a.getResourceId(com.android.internal.R.styleable.LayoutAnimation_animation, 0);
        if (resource > 0) {
            setAnimation(context, resource);
        }

        resource = a.getResourceId(com.android.internal.R.styleable.LayoutAnimation_interpolator, 0);
        if (resource > 0) {
            setInterpolator(context, resource);
        }

        a.recycle();
    
public LayoutAnimationController(Animation animation)
Creates a new layout animation controller with a delay of 50% and the specified animation.

param
animation the animation to use on each child of the view group

        this(animation, 0.5f);
    
public LayoutAnimationController(Animation animation, float delay)
Creates a new layout animation controller with the specified delay and the specified animation.

param
animation the animation to use on each child of the view group
param
delay the delay by which each child's animation must be offset

        mDelay = delay;
        setAnimation(animation);
    
Methods Summary
public AnimationgetAnimation()
Returns the animation applied to each child of the view group on which this controller is set.

return
an {@link android.view.animation.Animation} instance
see
#setAnimation(android.content.Context, int)
see
#setAnimation(Animation)

        return mAnimation;
    
public final AnimationgetAnimationForView(android.view.View view)
Returns the animation to be applied to the specified view. The returned animation is delayed by an offset computed according to the information provided by {@link android.view.animation.LayoutAnimationController.AnimationParameters}. This method is called by view groups to obtain the animation to set on a specific child.

param
view the view to animate
return
an animation delayed by the number of milliseconds returned by {@link #getDelayForView(android.view.View)}
see
#getDelay()
see
#setDelay(float)
see
#getDelayForView(android.view.View)

        final long delay = getDelayForView(view) + mAnimation.getStartOffset();
        mMaxDelay = Math.max(mMaxDelay, delay);

        try {
            final Animation animation = mAnimation.clone();
            animation.setStartOffset(delay);
            return animation;
        } catch (CloneNotSupportedException e) {
            return null;
        }
    
public floatgetDelay()
Returns the delay by which the children's animation are offset. The delay is expressed as a fraction of the animation duration.

return
a fraction of the animation duration
see
#setDelay(float)

        return mDelay;
    
protected longgetDelayForView(android.view.View view)
Returns the amount of milliseconds by which the specified view's animation must be delayed or offset. Subclasses should override this method to return a suitable value. This implementation returns child animation delay milliseconds where:
child animation delay = child index * delay
The index is retrieved from the {@link android.view.animation.LayoutAnimationController.AnimationParameters} found in the view's {@link android.view.ViewGroup.LayoutParams}.

param
view the view for which to obtain the animation's delay
return
a delay in milliseconds
see
#getAnimationForView(android.view.View)
see
#getDelay()
see
#getTransformedIndex(android.view.animation.LayoutAnimationController.AnimationParameters)
see
android.view.ViewGroup.LayoutParams

        ViewGroup.LayoutParams lp = view.getLayoutParams();
        AnimationParameters params = lp.layoutAnimationParameters;

        if (params == null) {
            return 0;
        }

        final float delay = mDelay * mAnimation.getDuration();
        final long viewDelay = (long) (getTransformedIndex(params) * delay);
        final float totalDelay = delay * params.count;

        if (mInterpolator == null) {
            mInterpolator = new LinearInterpolator();
        }

        float normalizedDelay = viewDelay / totalDelay;
        normalizedDelay = mInterpolator.getInterpolation(normalizedDelay);

        return (long) (normalizedDelay * totalDelay);
    
public InterpolatorgetInterpolator()
Returns the interpolator used to interpolate the delays between the children.

return
an {@link android.view.animation.Interpolator}

        return mInterpolator;
    
public intgetOrder()
Returns the order used to compute the delay of each child's animation.

return
one of {@link #ORDER_NORMAL}, {@link #ORDER_REVERSE} or {@link #ORDER_RANDOM)
attr
ref android.R.styleable#LayoutAnimation_animationOrder

        return mOrder;
    
protected intgetTransformedIndex(android.view.animation.LayoutAnimationController$AnimationParameters params)
Transforms the index stored in {@link android.view.animation.LayoutAnimationController.AnimationParameters} by the order returned by {@link #getOrder()}. Subclasses should override this method to provide additional support for other types of ordering. This method should be invoked by {@link #getDelayForView(android.view.View)} prior to any computation.

param
params the animation parameters containing the index
return
a transformed index

        switch (getOrder()) {
            case ORDER_REVERSE:
                return params.count - 1 - params.index;
            case ORDER_RANDOM:
                if (mRandomizer == null) {
                    mRandomizer = new Random();
                }
                return (int) (params.count * mRandomizer.nextFloat());
            case ORDER_NORMAL:
            default:
                return params.index;
        }
    
public booleanisDone()
Indicates whether the layout animation is over or not. A layout animation is considered done when the animation with the longest delay is done.

return
true if all of the children's animations are over, false otherwise

        return AnimationUtils.currentAnimationTimeMillis() >
                mAnimation.getStartTime() + mMaxDelay + mDuration;
    
public voidsetAnimation(android.content.Context context, int resourceID)
Sets the animation to be run on each child of the view group on which this layout animation controller is .

param
context the context from which the animation must be inflated
param
resourceID the resource identifier of the animation
see
#setAnimation(Animation)
see
#getAnimation()
attr
ref android.R.styleable#LayoutAnimation_animation

        setAnimation(AnimationUtils.loadAnimation(context, resourceID));
    
public voidsetAnimation(Animation animation)
Sets the animation to be run on each child of the view group on which this layout animation controller is .

param
animation the animation to run on each child of the view group
see
#setAnimation(android.content.Context, int)
see
#getAnimation()
attr
ref android.R.styleable#LayoutAnimation_animation

        mAnimation = animation;
        mAnimation.setFillBefore(true);
    
public voidsetDelay(float delay)
Sets the delay, as a fraction of the animation duration, by which the children's animations are offset. The general formula is:
child animation delay = child index * delay * animation duration

param
delay a fraction of the animation duration
see
#getDelay()

        mDelay = delay;
    
public voidsetInterpolator(Interpolator interpolator)
Sets the interpolator used to interpolate the delays between the children.

param
interpolator the interpolator
see
#getInterpolator()
see
#setInterpolator(Interpolator)
attr
ref android.R.styleable#LayoutAnimation_interpolator

        mInterpolator = interpolator;
    
public voidsetInterpolator(android.content.Context context, int resourceID)
Sets the interpolator used to interpolate the delays between the children.

param
context the context from which the interpolator must be inflated
param
resourceID the resource identifier of the interpolator
see
#getInterpolator()
see
#setInterpolator(Interpolator)
attr
ref android.R.styleable#LayoutAnimation_interpolator

        setInterpolator(AnimationUtils.loadInterpolator(context, resourceID));
    
public voidsetOrder(int order)
Sets the order used to compute the delay of each child's animation.

param
order one of {@link #ORDER_NORMAL}, {@link #ORDER_REVERSE} or {@link #ORDER_RANDOM}
attr
ref android.R.styleable#LayoutAnimation_animationOrder

        mOrder = order;
    
public voidstart()
Starts the animation.

        mDuration = mAnimation.getDuration();
        mMaxDelay = Long.MIN_VALUE;
        mAnimation.setStartTime(-1);
    
public booleanwillOverlap()
Indicates whether two children's animations will overlap. Animations overlap when the delay is lower than 100% (or 1.0).

return
true if animations will overlap, false otherwise

        return mDelay < 1.0f;