简体   繁体   English

动画期间Android动画插值器的更改

[英]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. 然后,当您要交换时,只需调用useLinear(false)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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