ValueAnimator和自定义Interpolator的配合使用

来源:互联网 发布:淘宝天猫店 编辑:程序博客网 时间:2024/05/22 05:33

本次要实现屏幕炫彩之闪瞎狗眼特效
代码先行:

//0-50其实我也试过0-100之类的,感觉对于本次效果没啥大影响ValueAnimator animator = ValueAnimator.ofInt(0,50);        //时间设的短,才能闪狗眼        animator.setDuration(300);        //每一帧都调用        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {            @Override            public void onAnimationUpdate(ValueAnimator animation) {                //currentValue = fraction*(50-0)                float currentValue = (Integer) animation.getAnimatedValue();                //该值是自定义MyInterpolator的返回值                 float fraction = animation.getAnimatedFraction();                //颜色估值器,根据开始颜色和结束颜色以及给定的fraction去计算这一帧的颜色                int color = (int) new ArgbEvaluator().evaluate(fraction,0xFFFF8080,0xFF8080FF);            //给屏幕根ViewGroup设置上颜色,闪爆狗眼                mActivityMain.setBackgroundColor(color);            }        });        //自定义插值器        animator.setInterpolator(new MyInterpolator());        //无限循环        animator.setRepeatCount(ValueAnimator.INFINITE);        //反转效果        animator.setRepeatMode(ValueAnimator.REVERSE);        animator.start();

自定义插值器如下,极其简单- -我就是为了试试自定义插值器好不好使 … …

public class MyInterpolator implements TimeInterpolator {    @Override    public float getInterpolation(float input) {    //传入的input是0~1之间随给定动画时间变化的数    //返回值是animation.getAnimatedValue();得到的值    //返回值可以取小于0和大于1的数        return (float) Math.sin(input);    }}

自定义插值器可以实现很多酷炫的效果,但是要求数学功力哦,我这样的就基本再见了

0 0
原创粉丝点击