Skip to content

Commit

Permalink
Add clearValueCallback and COLOR value callback to solid layer (#2378)
Browse files Browse the repository at this point in the history
  • Loading branch information
gpeal authored Sep 2, 2023
1 parent c76b9f0 commit a808c3f
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,13 @@ public List<KeyPath> resolveKeyPath(KeyPath keyPath) {
return lottieDrawable.resolveKeyPath(keyPath);
}

/**
* Clear the value callback for all nodes that match the given {@link KeyPath} and property.
*/
public <T> void clearValueCallback(KeyPath keyPath, T property) {
lottieDrawable.addValueCallback(keyPath, property, (LottieValueCallback<T>) null);
}

/**
* Add a property callback for the specified {@link KeyPath}. This {@link KeyPath} can resolve
* to multiple contents. In that case, the callback's value will apply to all of them.
Expand Down
2 changes: 2 additions & 0 deletions lottie/src/main/java/com/airbnb/lottie/LottieDrawable.java
Original file line number Diff line number Diff line change
Expand Up @@ -1288,6 +1288,8 @@ public List<KeyPath> resolveKeyPath(KeyPath keyPath) {
* <p>
* Internally, this will check if the {@link KeyPath} has already been resolved with
* {@link #resolveKeyPath(KeyPath)} and will resolve it if it hasn't.
*
* Set the callback to null to clear it.
*/
public <T> void addValueCallback(
final KeyPath keyPath, final T property, @Nullable final LottieValueCallback<T> callback) {
Expand Down
11 changes: 11 additions & 0 deletions lottie/src/main/java/com/airbnb/lottie/model/layer/SolidLayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class SolidLayer extends BaseLayer {
private final Path path = new Path();
private final Layer layerModel;
@Nullable private BaseKeyframeAnimation<ColorFilter, ColorFilter> colorFilterAnimation;
@Nullable private BaseKeyframeAnimation<Integer, Integer> colorAnimation;

SolidLayer(LottieDrawable lottieDrawable, Layer layerModel) {
super(lottieDrawable, layerModel);
Expand All @@ -43,6 +44,9 @@ public class SolidLayer extends BaseLayer {
int opacity = transform.getOpacity() == null ? 100 : transform.getOpacity().getValue();
int alpha = (int) (parentAlpha / 255f * (backgroundAlpha / 255f * opacity / 100f) * 255);
paint.setAlpha(alpha);
if (colorAnimation != null) {
paint.setColor(colorAnimation.getValue());
}
if (colorFilterAnimation != null) {
paint.setColorFilter(colorFilterAnimation.getValue());
}
Expand Down Expand Up @@ -88,6 +92,13 @@ public <T> void addValueCallback(T property, @Nullable LottieValueCallback<T> ca
colorFilterAnimation =
new ValueCallbackKeyframeAnimation<>((LottieValueCallback<ColorFilter>) callback);
}
} else if (property == LottieProperty.COLOR) {
if (callback == null) {
colorAnimation = null;
paint.setColor(layerModel.getSolidColor());
} else {
colorAnimation = new ValueCallbackKeyframeAnimation<>((LottieValueCallback<Integer>) callback);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,14 @@ class DynamicPropertiesTestCase : SnapshotTestCase {
assetName = "Tests/AnimatedShadow.json"
)

testDynamicProperty(
"Solid Color",
KeyPath("Cyan Solid 1", "**"),
LottieProperty.COLOR,
LottieValueCallback(Color.YELLOW),
assetName = "Tests/SolidLayerTransform.json"
)

withDrawable("Tests/DynamicGradient.json", "Gradient Colors", "Linear Gradient Fill") { drawable ->
val value = object : LottieValueCallback<Array<Int>>() {
override fun getValue(frameInfo: LottieFrameInfo<Array<Int>>?): Array<Int> {
Expand Down Expand Up @@ -483,4 +491,4 @@ class DynamicPropertiesTestCase : SnapshotTestCase {
drawable.progress = progress
}
}
}
}

0 comments on commit a808c3f

Please sign in to comment.