/
LottieAnimatableColorValue.java
61 lines (51 loc) · 2.07 KB
/
LottieAnimatableColorValue.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
package com.airbnb.lottie.animation;
import android.graphics.Color;
import com.airbnb.lottie.utils.LottieColorKeyframeAnimation;
import com.airbnb.lottie.utils.LottieKeyframeAnimation;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class LottieAnimatableColorValue extends BaseLottieAnimatableValue<Integer, Integer> {
public LottieAnimatableColorValue(JSONObject json, int frameRate, long compDuration) {
super(json, frameRate, compDuration);
}
@Override
protected Integer valueFromObject(Object object) throws JSONException {
JSONArray colorArray = (JSONArray) object;
if (colorArray.length() == 4) {
boolean shouldUse255 = true;
for (int i = 0; i < colorArray.length(); i++) {
double colorChannel = colorArray.getDouble(i);
if (colorChannel > 1f) {
shouldUse255 = false;
}
}
float multiplier = shouldUse255 ? 255f : 1f;
return Color.argb(
(int) (colorArray.getDouble(3) * multiplier),
(int) (colorArray.getDouble(0) * multiplier),
(int) (colorArray.getDouble(1) * multiplier),
(int) (colorArray.getDouble(2) * multiplier));
}
return Color.BLACK;
}
@Override
public LottieKeyframeAnimation animationForKeyPath() {
if (!hasAnimation()) {
return null;
}
LottieColorKeyframeAnimation animation = new LottieColorKeyframeAnimation(duration, compDuration, keyTimes, keyValues, interpolators);
animation.setStartDelay(delay);
animation.addUpdateListener(new LottieKeyframeAnimation.AnimationListener<Integer>() {
@Override
public void onValueChanged(Integer progress) {
observable.setValue(progress);
}
});
return animation;
}
@Override
public String toString() {
return "LottieAnimatableColorValue{" + "initialValue=" + initialValue + '}';
}
}