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

[Test] Rounded corners with a path effect #2599

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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 @@ -5,6 +5,7 @@
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.CornerPathEffect;
import android.graphics.DashPathEffect;
import android.graphics.Matrix;
import android.graphics.Paint;
Expand Down Expand Up @@ -192,6 +193,7 @@ public abstract class BaseStrokeContent

canvas.save();
canvas.concat(parentMatrix);
float cornerRadius = 0f;
for (int i = 0; i < pathGroups.size(); i++) {
PathGroup pathGroup = pathGroups.get(i);

Expand All @@ -204,7 +206,16 @@ public abstract class BaseStrokeContent
}
path.reset();
for (int j = pathGroup.paths.size() - 1; j >= 0; j--) {
path.addPath(pathGroup.paths.get(j).getPath());
PathContent pathContent = pathGroup.paths.get(j);
if (pathContent instanceof RoundedCornersContent) {
float newCornerRadius = ((RoundedCornersContent) pathContent).getRoundedCorners().getValue();
if (newCornerRadius != cornerRadius) {
canvas.drawPath(path, paint);
path.reset();
paint.setPathEffect(new CornerPathEffect(newCornerRadius / Utils.dpScale()));
}
}
path.addPath(pathContent.getPath());
}
if (L.isTraceEnabled()) {
L.endSection("StrokeContent#buildPath");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.CornerPathEffect;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.graphics.RectF;

import androidx.annotation.Nullable;
Expand All @@ -24,6 +26,7 @@
import com.airbnb.lottie.model.layer.BaseLayer;
import com.airbnb.lottie.utils.DropShadow;
import com.airbnb.lottie.utils.MiscUtils;
import com.airbnb.lottie.utils.Utils;
import com.airbnb.lottie.value.LottieValueCallback;

import java.util.ArrayList;
Expand Down Expand Up @@ -123,7 +126,16 @@ public FillContent(final LottieDrawable lottieDrawable, BaseLayer layer, ShapeFi
}

path.reset();
float cornerRadius = 0f;
for (int i = 0; i < paths.size(); i++) {
if (paths.get(i) instanceof RoundedCornersContent) {
float newCornerRadius = ((RoundedCornersContent) paths.get(i)).getRoundedCorners().getValue();
if (i > 0 || cornerRadius != newCornerRadius) {
canvas.drawPath(path, paint);
path.reset();
}
paint.setPathEffect(new CornerPathEffect(newCornerRadius / Utils.dpScale()));
}
path.addPath(paths.get(i).getPath(), parentMatrix);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ public void setContents(List<Content> contentsBefore, List<Content> contentsAfte
TrimPathContent trimPath = (TrimPathContent) content;
trimPaths.addTrimPath(trimPath);
trimPath.addListener(this);
} else if (content instanceof RoundedCornersContent) {
roundedCornersAnimation = ((RoundedCornersContent) content).getRoundedCorners();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.airbnb.lottie.animation.content;

import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Path;
import android.graphics.PointF;
import android.graphics.RectF;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand All @@ -11,11 +15,13 @@
import com.airbnb.lottie.model.content.RoundedCorners;
import com.airbnb.lottie.model.content.ShapeData;
import com.airbnb.lottie.model.layer.BaseLayer;
import com.airbnb.lottie.utils.DropShadow;

import java.util.ArrayList;
import java.util.List;

public class RoundedCornersContent implements ShapeModifierContent, BaseKeyframeAnimation.AnimationListener {
public class RoundedCornersContent implements PathContent, BaseKeyframeAnimation.AnimationListener {

/**
* Copied from:
* https://github.com/airbnb/lottie-web/blob/bb71072a26e03f1ca993da60915860f39aae890b/player/js/utils/common.js#L47
Expand All @@ -26,6 +32,7 @@ public class RoundedCornersContent implements ShapeModifierContent, BaseKeyframe
private final String name;
private final BaseKeyframeAnimation<Float, Float> roundedCorners;
@Nullable private ShapeData shapeData;
private final Path path = new Path();

public RoundedCornersContent(LottieDrawable lottieDrawable, BaseLayer layer, RoundedCorners roundedCorners) {
this.lottieDrawable = lottieDrawable;
Expand All @@ -51,7 +58,7 @@ public BaseKeyframeAnimation<Float, Float> getRoundedCorners() {
return roundedCorners;
}

@Override public void addUpdateListener(BaseKeyframeAnimation.AnimationListener listener) {
public void addUpdateListener(BaseKeyframeAnimation.AnimationListener listener) {
roundedCorners.addUpdateListener(listener);
}

Expand All @@ -75,7 +82,7 @@ public BaseKeyframeAnimation<Float, Float> getRoundedCorners() {
* The distance that the vertices and control points are moved are relative to the
* shape's vertex distances and the roundedness set in the animation.
*/
@Override public ShapeData modifyShape(ShapeData startingShapeData) {
public ShapeData modifyShape(ShapeData startingShapeData) {
List<CubicCurveData> startingCurves = startingShapeData.getCurves();
if (startingCurves.size() <= 2) {
return startingShapeData;
Expand Down Expand Up @@ -226,4 +233,8 @@ private static int floorDiv(int x, int y) {
}
return r;
}

@Override public Path getPath() {
return path;
}
}
Loading