(一)快速开始
Animator是属性动画,通过修改对象的属性来达到实现动画的效果。
一段替换Animation全功能的Animator代码:(请使用3.0以上API来执行前面的例子)
ObjectAnimator animation = ObjectAnimator.ofFloat(view, "alpha", 0f, 1f);
// ObjectAnimator animation = ObjectAnimator.ofFloat(view, "translationX", 0, 500);
// ObjectAnimator animation = ObjectAnimator.ofFloat(view, "translationY", 0, 500);
// ObjectAnimator animation = ObjectAnimator.ofFloat(view, "scaleX", 0, 2f);
// ObjectAnimator animation = ObjectAnimator.ofFloat(view, "scaleY", 0, 2f);
// ObjectAnimator animation = ObjectAnimator.ofFloat(view, "rotation", 0, 360f);
// ObjectAnimator animation = ObjectAnimator.ofFloat(view, "rotationX", 0, 360f);
// ObjectAnimator animation = ObjectAnimator.ofFloat(view, "rotationY", 0, 360f);
animation.setDuration(5000);
animation.start();
以上代码可以实现:透明度,位移,缩放,旋转变化
Animatoin中包含的锚点,可以直接对view进行设置:
view.setPivotX(0);
view.setPivotY(0);
虽然属性动画不太适合用XML书写,但是其Java代码的属性难度非常低,很容易理解。
(二)总体脉络
首先从实现一个颜色渐变的动画开始,讲述属性动画的属性是指什么,然后讲述一下如何使用TypeEvaluator。
(三) 颜色的渐变动画
当你想改变一个View的背景颜色的时候,你想到的方法是什么?
view.setBackgroundColor(color);
而对于属性动画而言,你指定一个属性之后,动画框架会反射的去调用你的setXXX来实现对你的属性的修改。
根据这个思路,我们将属性命名为backgroundColor ,于是写下下面的代码:
ObjectAnimator animation = ObjectAni