极客学院Animation教程讲解的很详细,点击进入哦
这里为学习的整理和补充O(∩_∩)O
一、秒懂ofObject()概念
还记得这行代码么?
ValueAnimator valueAnimator = ValueAnimator.ofInt(0, 400);
ofInt()和 ofFloat()可以定义动画,但是这时候我想要操作其他类型肿么办???( ⊙ o ⊙ )这个时候,ofObject()就出场了,可以传进去任何类型的变量。
下边,一起揭开真面目~源码敬上
/**
@param evaluator A TypeEvaluator that will be called on each animation frame to
* provide the ncessry interpolation between the Object values to derive the animated
* value.
* @param values A set of values that the animation will animate between over time.
* @return A ValueAnimator object that is set up to animate between the given values.
*/
public static ValueAnimator ofObject(TypeEvaluator evaluator, Object... values) {
ValueAnimator anim = new ValueAnimator();
anim.setObjectValues(values);
anim.setEvaluator(evaluator);
return anim;
}
稍微解释一下:
-
evaluator: TypeEvaluator会被每个动画帧调用,根据Object... values的值和所选择的插值器,计算当前进度下所对应的值。
其实,和Evaluator的作用是一样的~~~只是TypeEvaluator是一个自定义的 Evaluator。 ofInt要用IntEvaluator,自己设定ofObject()设置类型,自然是要自定义Evaluator的 -
Object... values:指定动画变化区间,类比参考ofInt()、ofFloat()~
二、使用ofObject()
创建一个字母从 A 变化到 Z的动画
1. 自定义Evaluator
public class CharEvaluator implements TypeEvaluator<Character> {
@Override
public Character evaluate(float fraction, Character startValue, Character endValue) {
int start = startValue;
int end = endValue;
//A(ASCII65) Z(ASCII90)
int curValue = (int) (startValue + (endValue - startValue) * fraction);
return (char) curValue;
}}
2. 开始使用
ValueAnimator valueAnimator = ValueAnimator.ofObject(new CharEvaluator(), 'A', 'X','Z');
valueAnimator.setDuration(6000);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
char animatedValue = (char) animation.getAnimatedValue();
view.setText(String.valueOf(animatedValue));
}});
valueAnimator.start();
完工啦,这样就可以看到A->Z的变化哦,会发现A->X变化速度很快,X->Z变化慢哦
三、ofObject()之自定义对象示例
引用自http://wiki.jikexueyuan.com/project/android-animation/6.html还是这个gif,咱们换种思路实现O(∩_∩)O~
- 建立实体类
/**
* 可以添加多种圆的属性,可以设置半径、颜色等,这里仅设置半径
*/
public class Circle {
//圆的半径
private int radius;
public Circle(int radius) {
this.radius = radius;
}
public int getRadius() {
return radius;
}
public void setRadius(int radius) {
this.radius = radius;
}
}
2.自定义View,绘制圆形
public class MyCircleView extends View {
private Circle curCircle;
Paint paint = new Paint();
public MyCircleView(Context context) {
super(context);
}
public MyCircleView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyCircleView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//画圆,关于绘图的知识,会在以后讲解
if (curCircle != null) {
paint.setColor(Color.RED);
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.FILL);
canvas.drawCircle(500, 600, curCircle.getRadius(), paint);
}
}
//动画,这个函数是在自定义CircleEvaluator之后添加的哦
public void doAnimate() {
ValueAnimator valueAnimator = ValueAnimator.ofObject(new CircleEvaluator(), new Circle(20), new Circle(400));
valueAnimator.setDuration(6000);
valueAnimator.setInterpolator(new BounceInterpolator());
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
curCircle = (Circle) animation.getAnimatedValue();
//刷新该View
invalidate();
}
});
valueAnimator.start();
}
}
3.xml布局中引用
<com.demo.animator.MyCircleView
android:id="@+id/view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
4.自定义Evaluator
public class CircleEvaluator implements TypeEvaluator<Circle> {
@Override
public Circle evaluate(float fraction, Circle startValue, Circle endValue) {
int startValueRadius = startValue.getRadius();
int endValueRadius = endValue.getRadius();
int curRadius = (int) (startValueRadius + (endValueRadius - startValueRadius) * fraction);
return new Circle(curRadius);
}
}
5.Activity中调用一下,开始执行动画吧,至此搞定O(∩_∩)O
view = (MyCircleView) findViewById(R.id.view);
view.doAnimate();
网友评论