-
Notifications
You must be signed in to change notification settings - Fork 78
/
AnimatorController.java
145 lines (126 loc) · 3.79 KB
/
AnimatorController.java
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package com.kaichunlin.transition.animation;
import android.animation.ValueAnimator;
import android.annotation.TargetApi;
import android.os.Build;
import android.view.animation.LinearInterpolator;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Kai on 2015/8/7.
*/
class AnimatorController extends ValueAnimator implements StateController, ValueAnimator.AnimatorUpdateListener, android.animation.Animator.AnimatorListener {
private final List<AbstractAnimation> mAnimationList = new ArrayList<>();
private boolean mReset;
private boolean mCancel;
AnimatorController(boolean reverse) {
setInterpolator(new LinearInterpolator());
if (reverse) {
setFloatValues(1, 0);
} else {
setFloatValues(0, 1);
}
addUpdateListener(this);
addListener(this);
}
@Override
public void addAnimation(AbstractAnimation animatorStateListener) {
mAnimationList.add(animatorStateListener);
}
@Override
public void setAnimationDuration(long duration) {
setDuration(duration);
}
@Override
public void startController() {
start();
mReset = false;
mCancel = false;
}
@TargetApi(19)
@Override
public void pauseController() {
if (Build.VERSION.SDK_INT < 19) {
return;
}
pause();
}
@TargetApi(19)
@Override
public void resumeController() {
if (Build.VERSION.SDK_INT < 19) {
return;
}
resume();
}
@Override
public void endController() {
end();
}
@Override
public void resetController() {
mReset = true;
AbstractAnimation ani;
for (int i = 0; i < mAnimationList.size(); i++) {
ani = mAnimationList.get(i);
ani.notifyAnimationReset();
}
cancel();
//TODO optimize
for (int i = 0; i < mAnimationList.size(); i++) {
ani = mAnimationList.get(i);
ani.getTransition().startTransition();
ani.getTransition().updateProgress(ani.isReverseAnimation() ? 1 : 0);
ani.getTransition().stopTransition();
}
}
@Override
public void cancelController() {
cancel();
}
@Override
public void onAnimationUpdate(ValueAnimator animation) {
AbstractAnimation ani;
float progress = (Float) animation.getAnimatedValue();
for (int i = 0; i < mAnimationList.size(); i++) {
ani = mAnimationList.get(i);
ani.getTransition().updateProgress(progress);
}
}
@Override
public void onAnimationStart(android.animation.Animator animation) {
for (int i = 0; i < mAnimationList.size(); i++) {
mAnimationList.get(i).notifyAnimationStart();
}
}
@Override
public void onAnimationEnd(android.animation.Animator animation) {
if (mReset || mCancel) {
return;
}
AbstractAnimation ani;
for (int i = 0; i < mAnimationList.size(); i++) {
ani = mAnimationList.get(i);
ani.setAnimating(false);
ani.notifyAnimationEnd();
ani.getTransition().stopTransition();
}
}
@Override
public void onAnimationCancel(android.animation.Animator animation) {
AbstractAnimation ani;
for (int i = 0; i < mAnimationList.size(); i++) {
ani = mAnimationList.get(i);
if (mReset) {
ani.notifyAnimationReset();
} else {
ani.notifyAnimationCancel();
}
ani.setAnimating(false);
ani.getTransition().stopTransition();
}
mCancel = true;
}
@Override
public void onAnimationRepeat(android.animation.Animator animation) {
}
}