Skip to content

Commit

Permalink
[ProgressIndicator] Fixed the rounded ends overlapping bug with semi-…
Browse files Browse the repository at this point in the history
…transparent track/indicator color in Circular default style.

Workaround is applied by using the Cap.ROUND when corner radius is half of the track thickness (the case for M3 default Circular style). It doesn't fix the overlap for smaller corner radius with semi-transparent color.

PiperOrigin-RevId: 599942498
(cherry picked from commit 97f18a3)
  • Loading branch information
pekingme authored and hunterstich committed Mar 21, 2024
1 parent 3c8342c commit 8167c11
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,7 @@ public int getTrackThickness() {
public void setTrackThickness(@Px int trackThickness) {
if (spec.trackThickness != trackThickness) {
spec.trackThickness = trackThickness;
spec.trackCornerRadius = min(spec.trackCornerRadius, spec.trackThickness / 2);
requestLayout();
}
}
Expand Down Expand Up @@ -634,7 +635,6 @@ public int getTrackCornerRadius() {
public void setTrackCornerRadius(@Px int trackCornerRadius) {
if (spec.trackCornerRadius != trackCornerRadius) {
spec.trackCornerRadius = min(trackCornerRadius, spec.trackThickness / 2);
spec.validateSpec();
invalidate();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ final class CircularDrawingDelegate extends DrawingDelegate<CircularProgressIndi
private float displayedTrackThickness;
private float displayedCornerRadius;
private float adjustedRadius;
// For full round ends, the stroke ROUND cap is used to prevent artifacts like (b/319309456).
private boolean useStrokeCap;

// This will be used in the ESCAPE hide animation. The start and end fraction in track will be
// scaled by this fraction with a pivot of 1.0f.
Expand Down Expand Up @@ -105,6 +107,7 @@ void adjustCanvas(
-outerRadiusWithInset, -outerRadiusWithInset, outerRadiusWithInset, outerRadiusWithInset);

// These are used when drawing the indicator and track.
useStrokeCap = spec.trackThickness / 2 <= spec.trackCornerRadius;
displayedTrackThickness = spec.trackThickness * trackThicknessFraction;
displayedCornerRadius = spec.trackCornerRadius * trackThicknessFraction;
adjustedRadius = (spec.indicatorSize - spec.trackThickness) / 2f;
Expand Down Expand Up @@ -145,7 +148,6 @@ void fillIndicator(

// Sets up the paint.
paint.setStyle(Style.STROKE);
paint.setStrokeCap(Cap.BUTT);
paint.setAntiAlias(true);
paint.setColor(color);
paint.setStrokeWidth(displayedTrackThickness);
Expand Down Expand Up @@ -189,10 +191,14 @@ void fillIndicator(

// Draws the indicator arc without rounded corners.
RectF arcBound = new RectF(-adjustedRadius, -adjustedRadius, adjustedRadius, adjustedRadius);
paint.setStrokeCap(useStrokeCap ? Cap.ROUND : Cap.BUTT);
canvas.drawArc(arcBound, startDegree, arcDegree, false, paint);

// Draws rounded corners if needed.
if (displayedCornerRadius > 0 && Math.abs(arcDegree) > 0 && Math.abs(arcDegree) < 360) {
if (!useStrokeCap
&& displayedCornerRadius > 0
&& Math.abs(arcDegree) > 0
&& Math.abs(arcDegree) < 360) {
paint.setStyle(Style.FILL);
drawRoundedEnd(canvas, paint, displayedTrackThickness, displayedCornerRadius, startDegree);
drawRoundedEnd(
Expand Down

0 comments on commit 8167c11

Please sign in to comment.