Skip to content

Commit

Permalink
[ProgressIndicator] Removed the call to draw a transparent full track.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 597992011
(cherry picked from commit b597c1e)
  • Loading branch information
pekingme authored and hunterstich committed Mar 21, 2024
1 parent bcc27a3 commit 3f80fdb
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,6 @@ public void draw(@NonNull Canvas canvas) {
activeIndicator.color = baseSpec.indicatorColors[0];
if (baseSpec.indicatorTrackGapSize > 0) {
partialTrack.color = baseSpec.trackColor;
// Draws the full transparent track.
drawingDelegate.fillTrack(canvas, paint, /* drawableAlpha= */ 0);
// Draws the indicator and track.
int gapSize = baseSpec.indicatorTrackGapSize;
// TODO: workaround to maintain pixel-perfect compatibility with drawing logic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Build.VERSION;
Expand Down Expand Up @@ -209,20 +208,12 @@ public void draw(@NonNull Canvas canvas) {
drawingDelegate.validateSpecAndAdjustCanvas(
canvas, getBounds(), getGrowFraction(), isShowing(), isHiding());

if (initialIndicatorTrackGapSize > 0) {
if (drawingDelegate instanceof LinearDrawingDelegate) {
((LinearProgressIndicatorSpec) drawingDelegate.spec).trackStopIndicatorSize = 0;
} else if (drawingDelegate instanceof CircularDrawingDelegate) {
baseSpec.indicatorTrackGapSize = 0;
}

// Draws the transparent track.
int trackColor = baseSpec.trackColor;
baseSpec.trackColor = Color.TRANSPARENT;
drawingDelegate.fillTrack(canvas, paint, getAlpha());
baseSpec.trackColor = trackColor;
} else {
if (initialIndicatorTrackGapSize == 0) {
drawingDelegate.fillTrack(canvas, paint, getAlpha());
} else if (drawingDelegate instanceof LinearDrawingDelegate) {
((LinearProgressIndicatorSpec) drawingDelegate.spec).trackStopIndicatorSize = 0;
} else if (drawingDelegate instanceof CircularDrawingDelegate) {
baseSpec.indicatorTrackGapSize = 0;
}

for (int indicatorIndex = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ final class LinearDrawingDelegate extends DrawingDelegate<LinearProgressIndicato
private float trackLength = 300f;
private float displayedTrackThickness;
private float displayedCornerRadius;
private Path displayedTrackPath;
private final Path displayedTrackPath;

// 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 All @@ -52,6 +52,8 @@ final class LinearDrawingDelegate extends DrawingDelegate<LinearProgressIndicato
/** Instantiates LinearDrawingDelegate with the current spec. */
LinearDrawingDelegate(@NonNull LinearProgressIndicatorSpec spec) {
super(spec);

displayedTrackPath = new Path();
}

@Override
Expand Down Expand Up @@ -83,7 +85,6 @@ void adjustCanvas(
boolean isShowing,
boolean isHiding) {
trackLength = bounds.width();
float trackSize = spec.trackThickness;

// Positions canvas to center of the clip bounds.
canvas.translate(
Expand All @@ -110,13 +111,22 @@ void adjustCanvas(
totalTrackLengthFraction = 1f;
}

// Clips all drawing to the track area, so it doesn't draw outside of its bounds (which can
// happen in certain configurations of clipToPadding and clipChildren)
canvas.clipRect(-trackLength / 2, -trackSize / 2, trackLength / 2, trackSize / 2);

// These are set for the drawing the indicator and track.
displayedTrackThickness = spec.trackThickness * trackThicknessFraction;
displayedCornerRadius = spec.trackCornerRadius * trackThicknessFraction;

// Clips all drawing to the track area, so it doesn't draw outside of its bounds (which can
// happen in certain configurations of clipToPadding and clipChildren)
float right = trackLength / 2;
float left = right - trackLength * totalTrackLengthFraction;
float bottom = displayedTrackThickness / 2;
displayedTrackPath.rewind();
displayedTrackPath.addRoundRect(
new RectF(left, -bottom, right, bottom),
displayedCornerRadius,
displayedCornerRadius,
Path.Direction.CCW);
canvas.clipPath(displayedTrackPath);
}

@Override
Expand Down Expand Up @@ -159,7 +169,6 @@ void fillIndicator(

canvas.save();
// Avoid the indicator being drawn out of the track.
canvas.clipPath(displayedTrackPath);
RectF indicatorBounds =
new RectF(
adjustedStartX,
Expand All @@ -182,15 +191,6 @@ void fillTrack(
paint.setAntiAlias(true);
paint.setColor(trackColor);

float right = trackLength / 2;
float left = right - trackLength * totalTrackLengthFraction;
float bottom = displayedTrackThickness / 2;
displayedTrackPath = new Path();
displayedTrackPath.addRoundRect(
new RectF(left, -bottom, right, bottom),
displayedCornerRadius,
displayedCornerRadius,
Path.Direction.CCW);
canvas.drawPath(displayedTrackPath, paint);
}

Expand All @@ -208,7 +208,6 @@ void drawStopIndicator(
paint.setColor(paintColor);
canvas.save();
// Avoid the indicator being drawn out of the track.
canvas.clipPath(displayedTrackPath);
Rect trackBounds = canvas.getClipBounds();
float offset = max(0, displayedTrackThickness - spec.trackStopIndicatorSize);
RectF stopBounds =
Expand Down

0 comments on commit 3f80fdb

Please sign in to comment.