您的位置:首页 > 移动开发 > Android开发

android动画速率Interpolator类的一些认识

2016-03-07 16:53 363 查看

android动画速率Interpolator类的一些认识

从今天开始,有时间写写博客,就算是些基础知识也可以写,加深自己学习的记忆.

Interpolator接口继承TimeInterpolator

public interface Interpolator extends TimeInterpolator {
}


没有增加任何方法,而TimeInterpolator只有一个抽象方法

public interface TimeInterpolator {
float getInterpolation(float input);
}


参数input值是一个由0变到1的值,在动画进行过程中,不断调用这个方法..取返回值决定了动画在各个时间段的值。

实现这个方法,应该保证input=0时返回0,input=1时返回1,因为动画的起始和目标就是从0到1。

例如 LinearInterpolator 参照函数y=x

public class LinearInterpolator implements Interpolator {

public LinearInterpolator() {
}

public LinearInterpolator(Context context, AttributeSet attrs) {
}

public float getInterpolation(float input) {
return input;
}
}


这么原样返回,不做改变就是匀速的。

而取y=x^2就是先慢后快的。

public float getInterpolation(float input) {
return input*input;
}


而取y=根号x,先快后慢的

public float getInterpolation(float input) {
return  Math.sqrt(input);
}


在过程中还可以超出1,甚至为负值。例如CycleInterpolator

public float getInterpolation(float input) {
return (float)(Math.sin(2 * mCycles * Math.PI * input));
}


公式y=sin(2πRx),在坐标图上表现就是波浪形的正弦曲线,根据R值还可以决定波长/频率,也就是在一定时间内抖动的次数

除了android 已有实现的Interpolator 之外,我们还可以自己实现动画的具体过程.

比如我非要走一段停一段,走五次停五次,共10段

class MyInterpolator implements Interpolator {
private final float interval;
private float curIput = 0;

public MyInterpolator() {
interval = 0.1f;
}

public MyInterpolator(int totalCount) {
this.interval = 1f / totalCount;
}

@Override
public float getInterpolation(float input) {
int curTime = (int) (input / interval);
if (curTime % 2 == 0) {
return 2 * input - curIput;
} else {
return curIput = (curTime + 1) * interval;
}
}
}


运行了下,可以运行.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: