FileDocCategorySizeDatePackage
StateListAnimator.javaAPI DocAndroid 5.1 API10487Thu Mar 12 22:22:08 GMT 2015android.animation

StateListAnimator

public class StateListAnimator extends Object implements Cloneable
Lets you define a number of Animators that will run on the attached View depending on the View's drawable state.

It can be defined in an XML file with the <selector> element. Each State Animator is defined in a nested <item> element.

attr
ref android.R.styleable#DrawableStates_state_focused
attr
ref android.R.styleable#DrawableStates_state_window_focused
attr
ref android.R.styleable#DrawableStates_state_enabled
attr
ref android.R.styleable#DrawableStates_state_checkable
attr
ref android.R.styleable#DrawableStates_state_checked
attr
ref android.R.styleable#DrawableStates_state_selected
attr
ref android.R.styleable#DrawableStates_state_activated
attr
ref android.R.styleable#DrawableStates_state_active
attr
ref android.R.styleable#DrawableStates_state_single
attr
ref android.R.styleable#DrawableStates_state_first
attr
ref android.R.styleable#DrawableStates_state_middle
attr
ref android.R.styleable#DrawableStates_state_last
attr
ref android.R.styleable#DrawableStates_state_pressed
attr
ref android.R.styleable#StateListAnimatorItem_animation

Fields Summary
private ArrayList
mTuples
private Tuple
mLastMatch
private Animator
mRunningAnimator
private WeakReference
mViewRef
private StateListAnimatorConstantState
mConstantState
private AnimatorListenerAdapter
mAnimatorListener
private int
mChangingConfigurations
Constructors Summary
public StateListAnimator()


      
        initAnimatorListener();
    
Methods Summary
public voidaddState(int[] specs, Animator animator)
Associates the given animator with the provided drawable state specs so that it will be run when the View's drawable state matches the specs.

param
specs The drawable state specs to match against
param
animator The animator to run when the specs match

        Tuple tuple = new Tuple(specs, animator);
        tuple.mAnimator.addListener(mAnimatorListener);
        mTuples.add(tuple);
        mChangingConfigurations |= animator.getChangingConfigurations();
    
public voidappendChangingConfigurations(int configs)
Sets the changing configurations value to the union of the current changing configurations and the provided configs. This method is called while loading the animator.

hide

        mChangingConfigurations |= configs;
    
private voidcancel()

        if (mRunningAnimator != null) {
            mRunningAnimator.cancel();
            mRunningAnimator = null;
        }
    
private voidclearTarget()

        final int size = mTuples.size();
        for (int i = 0; i < size; i++) {
            mTuples.get(i).mAnimator.setTarget(null);
        }
        mViewRef = null;
        mLastMatch = null;
        mRunningAnimator = null;
    
public android.animation.StateListAnimatorclone()

        try {
            StateListAnimator clone = (StateListAnimator) super.clone();
            clone.mTuples = new ArrayList<Tuple>(mTuples.size());
            clone.mLastMatch = null;
            clone.mRunningAnimator = null;
            clone.mViewRef = null;
            clone.mAnimatorListener = null;
            clone.initAnimatorListener();
            final int tupleSize = mTuples.size();
            for (int i = 0; i < tupleSize; i++) {
                final Tuple tuple = mTuples.get(i);
                final Animator animatorClone = tuple.mAnimator.clone();
                animatorClone.removeListener(mAnimatorListener);
                clone.addState(tuple.mSpecs, animatorClone);
            }
            clone.setChangingConfigurations(getChangingConfigurations());
            return clone;
        } catch (CloneNotSupportedException e) {
            throw new AssertionError("cannot clone state list animator", e);
        }
    
public android.content.res.ConstantStatecreateConstantState()
Return a {@link android.content.res.ConstantState} instance that holds the shared state of this Animator.

This constant state is used to create new instances of this animator when needed. Default implementation creates a new {@link StateListAnimatorConstantState}. You can override this method to provide your custom logic or return null if you don't want this animator to be cached.

return
The {@link android.content.res.ConstantState} associated to this Animator.
see
android.content.res.ConstantState
see
#clone()
hide

        return new StateListAnimatorConstantState(this);
    
public intgetChangingConfigurations()
Return a mask of the configuration parameters for which this animator may change, requiring that it be re-created. The default implementation returns whatever was provided through {@link #setChangingConfigurations(int)} or 0 by default.

return
Returns a mask of the changing configuration parameters, as defined by {@link android.content.pm.ActivityInfo}.
see
android.content.pm.ActivityInfo
hide

        return mChangingConfigurations;
    
public AnimatorgetRunningAnimator()
Returns the current {@link android.animation.Animator} which is started because of a state change.

return
The currently running Animator or null if no Animator is running
hide

        return mRunningAnimator;
    
public android.view.ViewgetTarget()

hide

        return mViewRef == null ? null : mViewRef.get();
    
public java.util.ArrayListgetTuples()

hide

        return mTuples;
    
private voidinitAnimatorListener()

        mAnimatorListener = new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                animation.setTarget(null);
                if (mRunningAnimator == animation) {
                    mRunningAnimator = null;
                }
            }
        };
    
public voidjumpToCurrentState()
If there is an animation running for a recent state change, ends it.

This causes the animation to assign the end value(s) to the View.

        if (mRunningAnimator != null) {
            mRunningAnimator.end();
        }
    
public voidsetChangingConfigurations(int configs)
Set a mask of the configuration parameters for which this animator may change, requiring that it should be recreated from resources instead of being cloned.

param
configs A mask of the changing configuration parameters, as defined by {@link android.content.pm.ActivityInfo}.
see
android.content.pm.ActivityInfo
hide

        mChangingConfigurations = configs;
    
public voidsetState(int[] state)
Called by View

hide

        Tuple match = null;
        final int count = mTuples.size();
        for (int i = 0; i < count; i++) {
            final Tuple tuple = mTuples.get(i);
            if (StateSet.stateSetMatches(tuple.mSpecs, state)) {
                match = tuple;
                break;
            }
        }
        if (match == mLastMatch) {
            return;
        }
        if (mLastMatch != null) {
            cancel();
        }
        mLastMatch = match;
        if (match != null) {
            start(match);
        }
    
public voidsetTarget(android.view.View view)
Called by View

hide

        final View current = getTarget();
        if (current == view) {
            return;
        }
        if (current != null) {
            clearTarget();
        }
        if (view != null) {
            mViewRef = new WeakReference<View>(view);
        }

    
private voidstart(android.animation.StateListAnimator$Tuple match)

        match.mAnimator.setTarget(getTarget());
        mRunningAnimator = match.mAnimator;
        mRunningAnimator.start();