/
LottieAnimatablePathValue.java
241 lines (208 loc) · 8.84 KB
/
LottieAnimatablePathValue.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package com.airbnb.lottie.animation;
import android.graphics.PointF;
import android.support.v4.view.animation.PathInterpolatorCompat;
import android.view.animation.Interpolator;
import android.view.animation.LinearInterpolator;
import com.airbnb.lottie.utils.LottieKeyframeAnimation;
import com.airbnb.lottie.utils.LottiePathKeyframeAnimation;
import com.airbnb.lottie.utils.Observable;
import com.airbnb.lottie.utils.SegmentedPath;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class LottieAnimatablePathValue implements LottieAnimatableValue<PointF> {
private final Observable<PointF> observable = new Observable<>();
private final List<Float> keyTimes = new ArrayList<>();
private final List<Interpolator> interpolators = new ArrayList<>();
private final long compDuration;
private final int frameRate;
private PointF initialPoint;
private final SegmentedPath animationPath = new SegmentedPath();
private long delay;
private long duration;
private long startFrame;
private long durationFrames;
@SuppressWarnings("EmptyCatchBlock")
public LottieAnimatablePathValue(JSONObject pointValues, int frameRate, long compDuration) {
this.compDuration = compDuration;
this.frameRate = frameRate;
Object value = null;
try {
value = pointValues.get("k");
} catch (JSONException e) { }
if (value == null) {
throw new IllegalArgumentException("Point values have no keyframes.");
}
if (value instanceof JSONArray) {
Object firstObject = null;
try {
firstObject = ((JSONArray) value).get(0);
} catch (JSONException e) { }
if (firstObject == null) {
throw new IllegalArgumentException("Unable to parse value.");
}
if (firstObject instanceof JSONObject && ((JSONObject) firstObject).has("t")) {
// Keyframes
buildAnimationForKeyframes((JSONArray) value);
} else {
// Single Value, no animation
initialPoint = pointFromValueArray((JSONArray) value);
observable.setValue(initialPoint);
}
}
}
private void buildAnimationForKeyframes(JSONArray keyframes) {
try {
for (int i = 0; i < keyframes.length(); i++) {
JSONObject kf = keyframes.getJSONObject(i);
if (kf.has("t")) {
startFrame = kf.getLong("t");
break;
}
}
for (int i = keyframes.length() - 1; i >= 0; i--) {
JSONObject keyframe = keyframes.getJSONObject(i);
if (keyframe.has("t")) {
long endFrame = keyframe.getLong("t");
if (endFrame <= startFrame) {
throw new IllegalStateException("Invalid frame compDuration " + startFrame + "->" + endFrame);
}
durationFrames = endFrame - startFrame;
duration = (long) (durationFrames / (float) frameRate * 1000);
delay = (long) (startFrame / (float) frameRate * 1000);
break;
}
}
boolean addStartValue = true;
boolean addTimePadding = false;
PointF outPoint = null;
for (int i = 0; i < keyframes.length(); i++) {
JSONObject keyframe = keyframes.getJSONObject(i);
long frame = keyframe.getLong("t");
float timePercentage = (float) (frame - startFrame) / (float) durationFrames;
if (outPoint != null) {
PointF vertex = outPoint;
animationPath.lineTo(vertex.x, vertex.y);
interpolators.add(new LinearInterpolator());
outPoint = null;
}
PointF startPoint = keyframe.has("s") ? pointFromValueArray(keyframe.getJSONArray("s")) : new PointF();
if (addStartValue) {
if (i == 0) {
animationPath.moveTo(startPoint.x, startPoint.y);
initialPoint = startPoint;
observable.setValue(initialPoint);
} else {
animationPath.lineTo(startPoint.x, startPoint.y);
interpolators.add(new LinearInterpolator());
}
addStartValue = false;
}
if (addTimePadding) {
float holdPercentage = timePercentage - 0.00001f;
keyTimes.add(holdPercentage);
addTimePadding = false;
}
PointF cp1;
PointF cp2;
if (keyframe.has("e")) {
cp1 = keyframe.has("to") ? pointFromValueArray(keyframe.getJSONArray("to")) : null;
cp2 = keyframe.has("ti") ? pointFromValueArray(keyframe.getJSONArray("ti")) : null;
PointF vertex = pointFromValueArray(keyframe.getJSONArray("e"));
if (cp1 != null && cp2 != null) {
animationPath.cubicTo(
startPoint.x + cp1.x, startPoint.y + cp1.y,
vertex.x + cp2.x, vertex.y + cp2.y,
vertex.x, vertex.y);
} else {
animationPath.lineTo(vertex.x, vertex.y);
}
Interpolator interpolator;
if (keyframe.has("o") && keyframe.has("i")) {
cp1 = pointFromValueObject(keyframe.getJSONObject("o"));
cp2 = pointFromValueObject(keyframe.getJSONObject("i"));
interpolator = PathInterpolatorCompat.create(cp1.x, cp1.y, cp2.x, cp2.y);
} else {
interpolator = new LinearInterpolator();
}
interpolators.add(interpolator);
}
keyTimes.add(timePercentage);
if (keyframe.has("h") && keyframe.getInt("h") == 1) {
outPoint = startPoint;
addStartValue = true;
addTimePadding = true;
}
}
} catch (JSONException e) {
throw new IllegalArgumentException("Unable to parse keyframes " + keyframes, e);
}
}
private PointF pointFromValueArray(JSONArray values) {
if (values.length() >= 2) {
try {
return new PointF((float) values.getDouble(0), (float) values.getDouble(1));
} catch (JSONException e) {
throw new IllegalArgumentException("Unable to parse point for " + values, e);
}
}
return new PointF();
}
private PointF pointFromValueObject(JSONObject value) {
try {
Object x = value.get("x");
Object y = value.get("y");
PointF point = new PointF();
if (x instanceof JSONArray) {
point.x = (float) ((JSONArray) x).getDouble(0);
} else {
if (x instanceof Integer) {
point.x = (Integer) x;
} else {
point.x = new Float((Double) x);
}
}
if (y instanceof JSONArray) {
point.y = (float) ((JSONArray) y).getDouble(0);
} else {
if (y instanceof Integer) {
point.y = (Integer) y;
} else {
point.y = new Float((Double) y);
}
}
return point;
} catch (JSONException e) {
throw new IllegalArgumentException("Unable to parse point for " + value);
}
}
@Override
public Observable<PointF> getObservable() {
return observable;
}
@Override
public LottieKeyframeAnimation animationForKeyPath() {
if (!hasAnimation()) {
return null;
}
LottieKeyframeAnimation<PointF> animation = new LottiePathKeyframeAnimation(duration, compDuration, keyTimes, animationPath, interpolators);
animation.setStartDelay(delay);
animation.addUpdateListener(new LottieKeyframeAnimation.AnimationListener<PointF>() {
@Override
public void onValueChanged(PointF progress) {
observable.setValue(progress);
}
});
return animation;
}
@Override
public boolean hasAnimation() {
return animationPath.hasSegments();
}
@Override
public String toString() {
return "LottieAnimatablePathValue{" + "initialPoint=" + initialPoint + '}';
}
}