Android Animator之ObjectAnimator

(一)ObjectAnimation 概述

ObjectAnimator:可以直接对任意对象的任意属性进行动画操作。其中控件的属性(必须有对应的get/set方法)可以通过控件本身或者父View的源码查询。

(二)ObjectAnimation 用法

1、一般动画 ObjectAnimation

1
2
3
4
5
6
// 获取当前控件Y轴缩放比
float currentScaleX = textView.getScaleX();
// 控件Y轴放大3倍。属性名scaleY对应 getScaleY()和setScaleY(float scaleY)方法。
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(textView, "scaleY", 1F, 3F);
objectAnimator.setDuration(1000L);
objectAnimator.start();

2、组合动画 AnimatorSet

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
AnimatorSet animatorSet = new AnimatorSet();

ObjectAnimator alphaAnimator = ObjectAnimator.ofFloat(textView, "alpha", 1F, 0.1F);
ObjectAnimator rotationAnimator = ObjectAnimator.ofFloat(textView, "rotation", 0F, 360F);
ObjectAnimator scaleXAnimator = ObjectAnimator.ofFloat(textView, "scaleX", 1F, 3F);
ObjectAnimator translationXAnimator = ObjectAnimator.ofFloat(textView, "translationX", 0F, -300F);

// with:alphaAnimator与rotationAnimator同时执行
animatorSet.play(alphaAnimator).with(rotationAnimator);
// before:alphaAnimator在translationXAnimator之前执行
animatorSet.play(alphaAnimator).before(translationXAnimator);
// after:alphaAnimator在scaleXAnimator之后执行
animatorSet.play(alphaAnimator).after(scaleXAnimator);
// after:alphaAnimator在1s之后执行
animatorSet.play(alphaAnimator).after(1000L);

// playTogether:同时执行多个动画
animatorSet.playTogether(alphaAnimator, rotationAnimator, scaleXAnimator, translationXAnimator);
// playSequentially:多个动画按顺序执行
animatorSet.playSequentially(alphaAnimator, rotationAnimator, scaleXAnimator, translationXAnimator);


// 动画加速
animatorSet.setInterpolator(new AccelerateInterpolator());
// 动画监听
animatorSet.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationStart(Animator animation) {
DebugLog.e("动画开始");
}

@Override
public void onAnimationCancel(Animator animation) {
DebugLog.e("动画取消");
}

@Override
public void onAnimationRepeat(Animator animation) {
DebugLog.e("动画重复执行");
}

@Override
public void onAnimationPause(Animator animation) {
DebugLog.e("动画暂停");
}

@Override
public void onAnimationResume(Animator animation) {
DebugLog.e("动画继续");
}

@Override
public void onAnimationEnd(Animator animation) {
DebugLog.e("动画结束");
}
});
// 执行时长。若同时执行则为 1s,反之累加。
animatorSet.setDuration(1000L);
// 延时 1s 执行
animatorSet.setStartDelay(1000L);
animatorSet.start();

(三)Interpolator 用法

Interpolator插值器 定义动画的变化率。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
* 自定义一个先减速后加速的Interpolator
*
* @author [*昨日重现*] lhy_ycu@163.com
* @since version 1.0
*/
public class DecelerateAccelerateInterpolator implements TimeInterpolator {

/**
* @param input 系统根据设定的动画时长匀速增加,变化范围是0到1。
* @return fraction 斜率决定快慢 - +
*/
@Override
public float getInterpolation(float input) {
if (input < 0.5f) {// x ∈[0, 1/2)
return (float) (Math.sin(input * Math.PI) / 2.0f);// y∈[0, 1/2] 【-】
} else {// x ∈[1/2, 1]
return 1 - (float) (Math.sin(input * Math.PI) / 2.0f);// y∈ 1 - [1/2, 0] 【+】
}
}

}

(四)ObjectAnimation 实例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/**
* ObjectAnimator 小球加速下落过程中颜色过渡动画
*
* @author [*昨日重现*] lhy_ycu@163.com
* @since version 1.0
*/
public class ObjectAnimatorView extends View {
private static final int CIRCLE_RADIUS = 100;// 圆的半径

private Paint mPaint;
private int mWidth, mHeight;// 控件大小
private Point mCurrentPoint;// 当前中心点
private int mColor;// 颜色值

public ObjectAnimatorView(Context context) {
this(context, null);
}

public ObjectAnimatorView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}

private void init() {
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setColor(Color.RED);

mCurrentPoint = new Point(0, 0);
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
this.mWidth = w;
this.mHeight = h;
mCurrentPoint.set(mWidth / 2, CIRCLE_RADIUS);
}

public int getColor() {
return mColor;
}

public void setColor(int color) {
this.mColor = color;
mPaint.setColor(color);
invalidate();
}

/**
* 开始动画
*/
public void startAnimation() {
// ObjectAnimator:本对象必须要有关于color属性的set/get方法。 Red -> Green -> Blue颜色变化
ObjectAnimator objectAnimator = ObjectAnimator.ofObject(this, "color", new ArgbEvaluator(), Color.RED, Color.GREEN, Color.BLUE);

// ValueAnimator:中心点的坐标位置过渡
Point startPoint = new Point(mWidth / 2, CIRCLE_RADIUS);
Point endPoint = new Point(mWidth / 2, mHeight / 2);
ValueAnimator valueAnimator = ValueAnimator.ofObject(new PointEvaluator(), startPoint, endPoint);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
mCurrentPoint = (Point) animation.getAnimatedValue();
invalidate();
}
});
valueAnimator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
DebugLog.e("动画结束");
}
});

// 组合成一个动画
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.play(objectAnimator).with(valueAnimator);
animatorSet.setDuration(3000L);
// 设置插值器:加速变化(默认是AccelerateDecelerateInterpolator)
animatorSet.setInterpolator(new AccelerateInterpolator(2F));
animatorSet.start();
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mPaint.setStyle(Paint.Style.FILL);
canvas.drawCircle(mCurrentPoint.x, mCurrentPoint.y, CIRCLE_RADIUS, mPaint);

mPaint.reset();
canvas.drawLine(0, mHeight / 2 + CIRCLE_RADIUS, mWidth, mHeight / 2 + CIRCLE_RADIUS, mPaint);
mPaint.setColor(Color.RED);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(15F);
final int LIMIT_COUNT = 19;
int pointDist = mWidth / LIMIT_COUNT;
for (int i = 1; i < LIMIT_COUNT; i++) {
canvas.drawPoint(pointDist * i, mHeight / 2 + CIRCLE_RADIUS, mPaint);
}
}

}
Hawky wechat
欢迎订阅我的微信公众号
坚持原创技术分享,您的支持将鼓励我继续创作!

分享