/
LottieAnimatablePointValue.java
92 lines (78 loc) · 2.97 KB
/
LottieAnimatablePointValue.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
package com.airbnb.lottie.animation;
import android.graphics.PointF;
import com.airbnb.lottie.utils.LottieKeyframeAnimation;
import com.airbnb.lottie.utils.LottiePointKeyframeAnimation;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class LottieAnimatablePointValue extends BaseLottieAnimatableValue<PointF, PointF> {
public LottieAnimatablePointValue(JSONObject pointValues, int frameRate, long compDuration) {
super(pointValues, frameRate, compDuration);
}
@Override
protected PointF valueFromObject(Object object) throws JSONException {
if (object instanceof JSONArray) {
return pointFromJsonArray((JSONArray) object);
} else if (object instanceof JSONObject) {
return pointFromJsonObject((JSONObject) object);
}
throw new IllegalArgumentException("Unable to parse point from " + object);
}
private PointF pointFromJsonArray(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 pointFromJsonObject(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 LottieKeyframeAnimation animationForKeyPath() {
if (!hasAnimation()) {
return null;
}
LottieKeyframeAnimation<PointF> animation = new LottiePointKeyframeAnimation(duration, compDuration, keyTimes, keyValues, 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 !keyValues.isEmpty();
}
}