自定义动画渲染器 Interceptor

来源:互联网 发布:美股最近行情走势知乎 编辑:程序博客网 时间:2024/04/29 15:03

android 有 系统的动画渲染器,  当然也可以实现自定义的动画渲染器。
要实现动画渲染器, 需要实现 android.vidw.animation.Interpolator 接口,这里以实现一个可以来回弹跳的动画渲染器
public class CustomInterceptor implements Interpolator {

@Override
public float getInterpolation(float input) {

if (input <= 0.5)
return input * input;
else
return (1 - input) * (1 - input);

}

}

在Interpolator 接口中只有一个getInterpolation 方法。该方法有一个float 类型的参数,取值范围在0.0-1.0 之间,表示动画的进度,如果参数值为0.0, 表示动画刚开始。如果参数值为1.0, 表示动画已结束。

如果方法的返回值小于1.0, 表示动画对象还没有到达目标点,越接近1.0,动画对象离目标点越近,当等于1.0时,正好到达目标点. 如果返回值大于1.0, 表示动画对象超过了目标点.

加载和开始动画的代码如下:

ImageView imageView = (ImageView) findViewById(R.id.imageview);
Animation animation = AnimationUtils.loadAnimation(this,
R.anim.translate);
animation.setInterpolator(new MyInterceptor());
animation.setRepeatCount(Animation.INFINITE);
imageView.startAnimation(animation);

程序运行后示意图如下, 具体代码请参见 ch11_Interceptor 工程