如何在Android中的视图上应用缓动动画功能

时间:2014-04-06 16:16:04

标签: android animation easing easing-functions

我想使用自定义animation在Android view(按钮)上应用翻译interpolator,其中缓动功能为:

public static float easeOut(float t,float b , float c, float d) {
    if ((t/=d) < (1/2.75f)) {
       return c*(7.5625f*t*t) + b;
    } else if (t < (2/2.75f)) {
       return c*(7.5625f*(t-=(1.5f/2.75f))*t + .75f) + b;
    } else if (t < (2.5/2.75)) {
       return c*(7.5625f*(t-=(2.25f/2.75f))*t + .9375f) + b;
    } else {
       return c*(7.5625f*(t-=(2.625f/2.75f))*t + .984375f) + b;
    }
}

我有一个使用自定义插补器的示例:

插补器是:

public class HesitateInterpolator implements Interpolator {
    public HesitateInterpolator() {
    }

    public float getInterpolation(float t) {
        float x = 2.0f * t - 1.0f;
        return 0.5f * (x * x * x + 1.0f);
    }
}

并使用如下:

ScaleAnimation anim = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f);
anim.setInterpolator(new HesitateInterpolator());

我的问题是: 这些值是bcd是什么?

5 个答案:

答案 0 :(得分:12)

仅供参考:对于只想要简易插补器的人,您可以使用myAnimator.setInterpolator(new AccelerateDecelerateInterpolator());

答案 1 :(得分:9)

根据Robert Penner's Easing Functions,如here所述:

t:当前时间,b:求助值,c:改变值,d:持续时间

如果你想实现你的自定义插值器,你必须做这样的事情:

(这将是easeInOutQuint

的实现
public class MVAccelerateDecelerateInterpolator implements Interpolator {

    // easeInOutQuint
    public float getInterpolation(float t) {
        float x;
        if (t<0.5f)
        { 
            x = t*2.0f;
            return 0.5f*x*x*x*x*x;
        }
        x = (t-0.5f)*2-1;
        return 0.5f*x*x*x*x*x+1;
    }
}

编辑: 为了实现缓动函数,你需要一些数学知识,考虑到getInterpolation方法只得到t参数,从0.0到1.0。

所以基本上你需要开发一个y(t)函数,t从0到1,y值从0到1,如下所示:

enter image description here

你改变的是从0到1的曲线(例如,在图像中绿线是线性的)。你需要规范化&#39;缓动函数保留在(0,1)x(0,1)方格中,如我在easeInOutQuint实现中所见。

答案 2 :(得分:9)

我创建了一个可以解决这个问题的库。 AndroidEasingFunctions

答案 3 :(得分:6)

1,2,3 go

  1. 使用此awesome site创建自定义三次贝塞尔曲线。并获得曲线的控制点。在0,0和1,1
  2. 之间
  3. Interpolator customInterpolator = PathInterpolatorCompat.create(cpX1,cpX2,cpY1,cpY2)
  4. 将此customInterpolator添加到任何动画中。
  5. 添加了gist。 一些more here

答案 4 :(得分:0)

这是另一个解决方案,最近添加到android的支持库中: https://gist.github.com/ebabel/8ff41cad01e9ce1dd9ce

以及使用中的示例:

main