FileDocCategorySizeDatePackage
RampAnimator.javaAPI DocAndroid 5.1 API6181Thu Mar 12 22:22:42 GMT 2015com.android.server.display

RampAnimator

public final class RampAnimator extends Object
A custom animator that progressively updates a property value at a given variable rate until it reaches a particular target value.

Fields Summary
private final T
mObject
private final android.util.IntProperty
mProperty
private final android.view.Choreographer
mChoreographer
private int
mCurrentValue
private int
mTargetValue
private int
mRate
private boolean
mAnimating
private float
mAnimatedValue
private long
mLastFrameTimeNanos
private boolean
mFirstTime
private Listener
mListener
private final Runnable
mAnimationCallback
Constructors Summary
public RampAnimator(T object, android.util.IntProperty property)


         
        mObject = object;
        mProperty = property;
        mChoreographer = Choreographer.getInstance();
    
Methods Summary
public booleananimateTo(int target, int rate)
Starts animating towards the specified value. If this is the first time the property is being set or if the rate is 0, the value jumps directly to the target.

param
target The target value.
param
rate The convergence rate in units per second, or 0 to set the value immediately.
return
True if the target differs from the previous target.

        // Immediately jump to the target the first time.
        if (mFirstTime || rate <= 0) {
            if (mFirstTime || target != mCurrentValue) {
                mFirstTime = false;
                mRate = 0;
                mTargetValue = target;
                mCurrentValue = target;
                mProperty.setValue(mObject, target);
                if (mAnimating) {
                    mAnimating = false;
                    cancelAnimationCallback();
                }
                if (mListener != null) {
                    mListener.onAnimationEnd();
                }
                return true;
            }
            return false;
        }

        // Adjust the rate based on the closest target.
        // If a faster rate is specified, then use the new rate so that we converge
        // more rapidly based on the new request.
        // If a slower rate is specified, then use the new rate only if the current
        // value is somewhere in between the new and the old target meaning that
        // we will be ramping in a different direction to get there.
        // Otherwise, continue at the previous rate.
        if (!mAnimating
                || rate > mRate
                || (target <= mCurrentValue && mCurrentValue <= mTargetValue)
                || (mTargetValue <= mCurrentValue && mCurrentValue <= target)) {
            mRate = rate;
        }

        final boolean changed = (mTargetValue != target);
        mTargetValue = target;

        // Start animating.
        if (!mAnimating && target != mCurrentValue) {
            mAnimating = true;
            mAnimatedValue = mCurrentValue;
            mLastFrameTimeNanos = System.nanoTime();
            postAnimationCallback();
        }

        return changed;
    
private voidcancelAnimationCallback()

        mChoreographer.removeCallbacks(Choreographer.CALLBACK_ANIMATION, mAnimationCallback, null);
    
public booleanisAnimating()
Returns true if the animation is running.

        return mAnimating;
    
private voidpostAnimationCallback()

        mChoreographer.postCallback(Choreographer.CALLBACK_ANIMATION, mAnimationCallback, null);
    
public voidsetListener(com.android.server.display.RampAnimator$Listener listener)
Sets a listener to watch for animation events.

        mListener = listener;