Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add clearValueCallback and COLOR value callback to solid layer #2378

Merged
merged 1 commit into from
Sep 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -1282,6 +1282,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
}
}
}
}