Skip to content

Commit

Permalink
Remove value callback animations when they get overwritten (airbnb#1470)
Browse files Browse the repository at this point in the history
  • Loading branch information
gpeal authored Jan 6, 2020
1 parent 353c834 commit 8a11553
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,18 @@ class DynamicActivity : AppCompatActivity() {
animationView.addLottieOnCompositionLoadedListener { _ ->
animationView.resolveKeyPath(KeyPath("**")).forEach {
Log.d(TAG, it.keysToString())
setupValueCallbacks()
}
}
animationView.setFailureListener { e ->
Log.e(TAG, "Failed to load composition", e)
Log.e(TAG, "Failed to load animation!", e)
}

updateButtonText()

}

private fun setupValueCallbacks() {
animationView.addValueCallback(KeyPath("LeftArmWave"),
LottieProperty.TIME_REMAP) { frameInfo ->
animationView.addValueCallback(KeyPath("LeftArmWave"), LottieProperty.TIME_REMAP) { frameInfo ->
2 * speed.toFloat() * frameInfo.overallProgress
}

Expand Down
4 changes: 4 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,7 @@ POM_DEVELOPER_EMAIL=lottie@airbnb.com
POM_INCEPTION_YEAR=2017
android.useAndroidX=true
android.enableJetifier=true
org.gradle.caching=true
org.gradle.jvmargs=-Xmx4096m
org.gradle.daemon=true
org.gradle.parallel=true
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,10 @@ public <T> void addValueCallback(T property, @Nullable LottieValueCallback<T> ca
} else if (property == LottieProperty.STROKE_WIDTH) {
widthAnimation.setValueCallback((LottieValueCallback<Float>) callback);
} else if (property == LottieProperty.COLOR_FILTER) {
if (colorFilterAnimation != null) {
layer.removeAnimation(colorFilterAnimation);
}

if (callback == null) {
colorFilterAnimation = null;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ public <T> void addValueCallback(T property, @Nullable LottieValueCallback<T> ca
} else if (property == LottieProperty.OPACITY) {
opacityAnimation.setValueCallback((LottieValueCallback<Integer>) callback);
} else if (property == LottieProperty.COLOR_FILTER) {
if (colorFilterAnimation != null) {
layer.removeAnimation(colorFilterAnimation);
}

if (callback == null) {
colorFilterAnimation = null;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,10 @@ public <T> void addValueCallback(T property, @Nullable LottieValueCallback<T> ca
if (property == LottieProperty.OPACITY) {
opacityAnimation.setValueCallback((LottieValueCallback<Integer>) callback);
} else if (property == LottieProperty.COLOR_FILTER) {
if (colorFilterAnimation != null) {
layer.removeAnimation(colorFilterAnimation);
}

if (callback == null) {
colorFilterAnimation = null;
} else {
Expand All @@ -244,12 +248,14 @@ public <T> void addValueCallback(T property, @Nullable LottieValueCallback<T> ca
layer.addAnimation(colorFilterAnimation);
}
} else if (property == LottieProperty.GRADIENT_COLOR) {
if (colorCallbackAnimation != null) {
layer.removeAnimation(colorCallbackAnimation);
}

if (callback == null) {
if (colorCallbackAnimation != null) {
layer.removeAnimation(colorCallbackAnimation);
}
colorCallbackAnimation = null;
} else {
//noinspection rawtypes
colorCallbackAnimation = new ValueCallbackKeyframeAnimation<>(callback);
colorCallbackAnimation.addUpdateListener(this);
layer.addAnimation(colorCallbackAnimation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,11 @@ private int[] applyDynamicColorsIfNeeded(int[] colors) {
public <T> void addValueCallback(T property, @Nullable LottieValueCallback<T> callback) {
super.addValueCallback(property, callback);
if (property == LottieProperty.GRADIENT_COLOR) {
if (colorCallbackAnimation != null) {
layer.removeAnimation(colorCallbackAnimation);
}

if (callback == null) {
if (colorCallbackAnimation != null) {
layer.removeAnimation(colorCallbackAnimation);
}
colorCallbackAnimation = null;
} else {
colorCallbackAnimation = new ValueCallbackKeyframeAnimation<>(callback);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,7 @@ public <T> void addValueCallback(T property, @Nullable LottieValueCallback<T> ca
innerRadiusAnimation.setValueCallback((LottieValueCallback<Float>) callback);
} else if (property == LottieProperty.POLYSTAR_OUTER_RADIUS) {
outerRadiusAnimation.setValueCallback((LottieValueCallback<Float>) callback);
} else if (property == LottieProperty.POLYSTAR_INNER_ROUNDEDNESS &&
innerRoundednessAnimation != null) {
} else if (property == LottieProperty.POLYSTAR_INNER_ROUNDEDNESS && innerRoundednessAnimation != null) {
innerRoundednessAnimation.setValueCallback((LottieValueCallback<Float>) callback);
} else if (property == LottieProperty.POLYSTAR_OUTER_ROUNDEDNESS) {
outerRoundednessAnimation.setValueCallback((LottieValueCallback<Float>) callback);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ public <T> void addValueCallback(T property, @Nullable LottieValueCallback<T> ca
if (property == STROKE_COLOR) {
colorAnimation.setValueCallback((LottieValueCallback<Integer>) callback);
} else if (property == LottieProperty.COLOR_FILTER) {
if (colorFilterAnimation != null) {
layer.removeAnimation(colorFilterAnimation);
}

if (callback == null) {
colorFilterAnimation = null;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,12 @@ public <T> void addValueCallback(T property, @Nullable LottieValueCallback<T> ca

if (property == LottieProperty.TIME_REMAP) {
if (callback == null) {
timeRemapping = null;
if (timeRemapping != null) {
timeRemapping.setValueCallback(null);
}
} else {
timeRemapping = new ValueCallbackKeyframeAnimation<>((LottieValueCallback<Float>) callback);
timeRemapping.addUpdateListener(this);
addAnimation(timeRemapping);
}
}
Expand Down
147 changes: 85 additions & 62 deletions lottie/src/main/java/com/airbnb/lottie/model/layer/TextLayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,23 @@ public class TextLayer extends BaseLayer {
@Nullable
private BaseKeyframeAnimation<Integer, Integer> colorAnimation;
@Nullable
private BaseKeyframeAnimation<Integer, Integer> colorCallbackAnimation;
@Nullable
private BaseKeyframeAnimation<Integer, Integer> strokeColorAnimation;
@Nullable
private BaseKeyframeAnimation<Integer, Integer> strokeColorCallbackAnimation;
@Nullable
private BaseKeyframeAnimation<Float, Float> strokeWidthAnimation;
@Nullable
private BaseKeyframeAnimation<Float, Float> strokeWidthCallbackAnimation;
@Nullable
private BaseKeyframeAnimation<Float, Float> trackingAnimation;
@Nullable
private BaseKeyframeAnimation<Float, Float> trackingCallbackAnimation;
@Nullable
private BaseKeyframeAnimation<Float, Float> textSizeAnimation;
@Nullable
private BaseKeyframeAnimation<Float, Float> textSizeCallbackAnimation;

TextLayer(LottieDrawable lottieDrawable, Layer layerModel) {
super(lottieDrawable, layerModel);
Expand Down Expand Up @@ -116,13 +126,17 @@ void drawLayer(Canvas canvas, Matrix parentMatrix, int parentAlpha) {
return;
}

if (colorAnimation != null) {
if (colorCallbackAnimation != null) {
fillPaint.setColor(colorCallbackAnimation.getValue());
} else if (colorAnimation != null) {
fillPaint.setColor(colorAnimation.getValue());
} else {
fillPaint.setColor(documentData.color);
}

if (strokeColorAnimation != null) {
if (strokeColorCallbackAnimation != null) {
strokePaint.setColor(strokeColorCallbackAnimation.getValue());
} else if (strokeColorAnimation != null) {
strokePaint.setColor(strokeColorAnimation.getValue());
} else {
strokePaint.setColor(documentData.strokeColor);
Expand All @@ -132,7 +146,9 @@ void drawLayer(Canvas canvas, Matrix parentMatrix, int parentAlpha) {
fillPaint.setAlpha(alpha);
strokePaint.setAlpha(alpha);

if (strokeWidthAnimation != null) {
if (strokeWidthCallbackAnimation != null) {
strokePaint.setStrokeWidth(strokeWidthCallbackAnimation.getValue());
} else if (strokeWidthAnimation != null) {
strokePaint.setStrokeWidth(strokeWidthAnimation.getValue());
} else {
float parentScale = Utils.getScale(parentMatrix);
Expand All @@ -150,7 +166,14 @@ void drawLayer(Canvas canvas, Matrix parentMatrix, int parentAlpha) {

private void drawTextGlyphs(
DocumentData documentData, Matrix parentMatrix, Font font, Canvas canvas) {
float textSize = textSizeAnimation == null ? documentData.size : textSizeAnimation.getValue();
float textSize;
if (textSizeCallbackAnimation != null) {
textSize = textSizeCallbackAnimation.getValue();
} else if (textSizeAnimation != null) {
textSize = textSizeAnimation.getValue();
} else {
textSize = documentData.size;
}
float fontScale = textSize / 100f;
float parentScale = Utils.getScale(parentMatrix);

Expand Down Expand Up @@ -199,7 +222,9 @@ private void drawGlyphTextLine(String text, DocumentData documentData, Matrix pa
float tx = (float) character.getWidth() * fontScale * Utils.dpScale() * parentScale;
// Add tracking
float tracking = documentData.tracking / 10f;
if (trackingAnimation != null) {
if (trackingCallbackAnimation != null) {
tracking += trackingCallbackAnimation.getValue();
} else if (trackingAnimation != null) {
tracking += trackingAnimation.getValue();
}
tx += tracking * parentScale;
Expand All @@ -220,7 +245,14 @@ private void drawTextWithFont(
text = textDelegate.getTextInternal(text);
}
fillPaint.setTypeface(typeface);
float textSize = textSizeAnimation == null ? documentData.size : textSizeAnimation.getValue();
float textSize;
if (textSizeCallbackAnimation != null) {
textSize = textSizeCallbackAnimation.getValue();
} else if (textSizeAnimation != null) {
textSize = textSizeAnimation.getValue();
} else {
textSize = documentData.size;
}
fillPaint.setTextSize(textSize * Utils.dpScale());
strokePaint.setTypeface(fillPaint.getTypeface());
strokePaint.setTextSize(fillPaint.getTextSize());
Expand Down Expand Up @@ -268,7 +300,9 @@ private void drawFontTextLine(String text, DocumentData documentData, Canvas can
float charWidth = fillPaint.measureText(charString, 0, 1);
// Add tracking
float tracking = documentData.tracking / 10f;
if (trackingAnimation != null) {
if (trackingCallbackAnimation != null) {
tracking += trackingCallbackAnimation.getValue();
} else if (trackingAnimation != null) {
tracking += trackingAnimation.getValue();
}
float tx = charWidth + tracking * parentScale;
Expand Down Expand Up @@ -417,75 +451,64 @@ private boolean isModifier(int codePoint) {
public <T> void addValueCallback(T property, @Nullable LottieValueCallback<T> callback) {
super.addValueCallback(property, callback);
if (property == LottieProperty.COLOR) {
if (colorAnimation != null) {
colorAnimation.setValueCallback((LottieValueCallback<Integer>) callback);
if (colorCallbackAnimation != null) {
removeAnimation(colorCallbackAnimation);
}

if (callback == null) {
colorCallbackAnimation = null;
} else {
if (callback == null) {
if (colorAnimation != null) {
removeAnimation(colorAnimation);
}
colorAnimation = null;
} else {
colorAnimation = new ValueCallbackKeyframeAnimation<>((LottieValueCallback<Integer>) callback);
colorAnimation.addUpdateListener(this);
addAnimation(colorAnimation);
}
colorCallbackAnimation = new ValueCallbackKeyframeAnimation<>((LottieValueCallback<Integer>) callback);
colorCallbackAnimation.addUpdateListener(this);
addAnimation(colorCallbackAnimation);
}
} else if (property == LottieProperty.STROKE_COLOR) {
if (strokeColorAnimation != null) {
strokeColorAnimation.setValueCallback((LottieValueCallback<Integer>) callback);
if (strokeColorCallbackAnimation != null) {
removeAnimation(strokeColorCallbackAnimation);
}

if (callback == null) {
strokeColorCallbackAnimation = null;
} else {
if (callback == null) {
if (strokeColorAnimation != null) {
removeAnimation(strokeColorAnimation);
}
strokeColorAnimation = null;
} else {
strokeColorAnimation = new ValueCallbackKeyframeAnimation<>((LottieValueCallback<Integer>) callback);
strokeColorAnimation.addUpdateListener(this);
addAnimation(strokeColorAnimation);
}
strokeColorCallbackAnimation = new ValueCallbackKeyframeAnimation<>((LottieValueCallback<Integer>) callback);
strokeColorCallbackAnimation.addUpdateListener(this);
addAnimation(strokeColorCallbackAnimation);
}
} else if (property == LottieProperty.STROKE_WIDTH) {
if (strokeWidthAnimation != null) {
strokeWidthAnimation.setValueCallback((LottieValueCallback<Float>) callback);
if (strokeWidthCallbackAnimation != null) {
removeAnimation(strokeWidthCallbackAnimation);
}

if (callback == null) {
strokeWidthCallbackAnimation = null;
} else {
if (callback == null) {
if (strokeWidthAnimation != null) {
removeAnimation(strokeWidthAnimation);
}
strokeWidthAnimation = null;
} else {
strokeWidthAnimation = new ValueCallbackKeyframeAnimation<>((LottieValueCallback<Float>) callback);
strokeWidthAnimation.addUpdateListener(this);
addAnimation(strokeWidthAnimation);
}
strokeWidthCallbackAnimation = new ValueCallbackKeyframeAnimation<>((LottieValueCallback<Float>) callback);
strokeWidthCallbackAnimation.addUpdateListener(this);
addAnimation(strokeWidthCallbackAnimation);
}
} else if (property == LottieProperty.TEXT_TRACKING) {
if (trackingAnimation != null) {
trackingAnimation.setValueCallback((LottieValueCallback<Float>) callback);
if (trackingCallbackAnimation != null) {
removeAnimation(trackingCallbackAnimation);
}

if (callback == null) {
trackingCallbackAnimation = null;
} else {
if (callback == null) {
if (trackingAnimation != null) {
removeAnimation(trackingAnimation);
}
trackingAnimation = null;
} else {
trackingAnimation = new ValueCallbackKeyframeAnimation<>((LottieValueCallback<Float>) callback);
trackingAnimation.addUpdateListener(this);
addAnimation(trackingAnimation);
}
trackingCallbackAnimation = new ValueCallbackKeyframeAnimation<>((LottieValueCallback<Float>) callback);
trackingCallbackAnimation.addUpdateListener(this);
addAnimation(trackingCallbackAnimation);
}
} else if (property == LottieProperty.TEXT_SIZE) {
if (textSizeCallbackAnimation != null) {
removeAnimation(textSizeCallbackAnimation);
}

if (callback == null) {
if (textSizeAnimation != null) {
removeAnimation(textSizeAnimation);
}
textSizeAnimation = null;
textSizeCallbackAnimation = null;
} else {
textSizeAnimation = new ValueCallbackKeyframeAnimation<>((LottieValueCallback<Float>) callback);
textSizeAnimation.addUpdateListener(this);
addAnimation(textSizeAnimation);
textSizeCallbackAnimation = new ValueCallbackKeyframeAnimation<>((LottieValueCallback<Float>) callback);
textSizeCallbackAnimation.addUpdateListener(this);
addAnimation(textSizeCallbackAnimation);
}
}
}
Expand Down

0 comments on commit 8a11553

Please sign in to comment.