| package com.airbnb.lottie.animation.content; |
| |
| import android.graphics.ColorFilter; |
| import android.graphics.Matrix; |
| import androidx.annotation.Nullable; |
| |
| import com.airbnb.lottie.LottieDrawable; |
| import com.airbnb.lottie.LottieProperty; |
| import com.airbnb.lottie.animation.canvas.ICanvas; |
| import com.airbnb.lottie.animation.keyframe.BaseKeyframeAnimation; |
| import com.airbnb.lottie.animation.keyframe.MaskKeyframeAnimation; |
| import com.airbnb.lottie.animation.keyframe.ValueCallbackKeyframeAnimation; |
| import com.airbnb.lottie.model.content.ShapeStroke; |
| import com.airbnb.lottie.model.layer.BaseLayer; |
| import com.airbnb.lottie.value.LottieValueCallback; |
| |
| import static com.airbnb.lottie.LottieProperty.STROKE_COLOR; |
| |
| public class StrokeContent extends BaseStrokeContent { |
| |
| private final BaseLayer layer; |
| private final String name; |
| private final BaseKeyframeAnimation<Integer, Integer> colorAnimation; |
| @Nullable private BaseKeyframeAnimation<ColorFilter, ColorFilter> colorFilterAnimation; |
| |
| public StrokeContent(final LottieDrawable lottieDrawable, BaseLayer layer, ShapeStroke stroke) { |
| super(lottieDrawable, layer, stroke.getCapType().toPaintCap(), |
| stroke.getJoinType().toPaintJoin(), stroke.getMiterLimit(), stroke.getOpacity(), |
| stroke.getWidth(), stroke.getLineDashPattern(), stroke.getDashOffset()); |
| this.layer = layer; |
| name = stroke.getName(); |
| colorAnimation = stroke.getColor().createAnimation(); |
| colorAnimation.addUpdateListener(this); |
| layer.addAnimation(colorAnimation); |
| } |
| |
| @Override public void draw(ICanvas canvas, Matrix parentMatrix, int parentAlpha, @Nullable MaskKeyframeAnimation mask, Matrix maskMatrix) { |
| paint.setColor(colorAnimation.getValue()); |
| if (colorFilterAnimation != null) { |
| paint.setColorFilter(colorFilterAnimation.getValue()); |
| } |
| super.draw(canvas, parentMatrix, parentAlpha, mask, maskMatrix); |
| } |
| |
| @Override public String getName() { |
| return name; |
| } |
| |
| @SuppressWarnings("unchecked") |
| @Override |
| public <T> void addValueCallback(T property, @Nullable LottieValueCallback<T> callback) { |
| super.addValueCallback(property, callback); |
| if (property == STROKE_COLOR) { |
| colorAnimation.setValueCallback((LottieValueCallback<Integer>) callback); |
| } else if (property == LottieProperty.COLOR_FILTER) { |
| if (callback == null) { |
| colorFilterAnimation = null; |
| } else { |
| colorFilterAnimation = |
| new ValueCallbackKeyframeAnimation<>((LottieValueCallback<ColorFilter>) callback); |
| colorFilterAnimation.addUpdateListener(this); |
| layer.addAnimation(colorAnimation); |
| } |
| } |
| } |
| } |