简体   繁体   中英

Android Animation Interpolator Change During Animation

What I'm trying to do is load one animation on a view with a linear interpolator for example. Then, when a certain event happens in my application, change the interpolator to something like a deceleration interpolator. So the effect would be a constant animation until an event happened, then the animation would slow down to a stop. Is this possible to achieve? Im wondering how I would achieve this and would love some direction. Thanks!

Yes. You would create a custom Interpolator for the animation. It contains the other interpolators that you want. When the event happens, just trigger it in the interpolator and have the interpolator swap one interpolator for the other.

public class CompositeInterpolator implements TimeInterpolator {
    LinearInterpolator mLinear;
    DecelerateInterpolator mDecelerate;

    boolean mUseLinear = false;

    public CompositeInterpolator() {
       mLinear = new LinearInterpolator();
       mDecelerate = new DecelerateInterpolator();
    }

    public void useLinear(boolean use) {
        mUseLinear = use;
    }

    @Override
    public float getInterpolation(float input) {
        if (mUseLinear) {
           return mLinear.getInterpolation(input);
        } else {
           return mDecelerate.getInterpolation(input);
        }
    }
}

Then just call useLinear(false) when you want to swap.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM