Skip to content

Commit

Permalink
[ProgressIndicator] Fixed ESCAPE animation in linear and added ESCAPE…
Browse files Browse the repository at this point in the history
… animation in circular.

PiperOrigin-RevId: 595720674
  • Loading branch information
pekingme committed Jan 4, 2024
1 parent b84e654 commit 98284e7
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.google.android.material.progressindicator;

import static com.google.android.material.math.MathUtils.lerp;
import static com.google.android.material.progressindicator.BaseProgressIndicator.HIDE_ESCAPE;
import static java.lang.Math.min;

import android.graphics.Canvas;
Expand All @@ -39,6 +41,11 @@ final class CircularDrawingDelegate extends DrawingDelegate<CircularProgressIndi
private float displayedCornerRadius;
private float adjustedRadius;

// 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.
@FloatRange(from = 0.0f, to = 1.0f)
private float totalTrackLengthFraction;

/** Instantiates CircularDrawingDelegate with the current spec. */
public CircularDrawingDelegate(@NonNull CircularProgressIndicatorSpec spec) {
super(spec);
Expand Down Expand Up @@ -115,6 +122,12 @@ public void adjustCanvas(
// indicator.
adjustedRadius -= (1 - trackThicknessFraction) * spec.trackThickness / 2;
}
// Sets the total track length fraction if ESCAPE hide animation is used.
if (isHiding && spec.hideAnimationBehavior == HIDE_ESCAPE) {
totalTrackLengthFraction = trackThicknessFraction;
} else {
totalTrackLengthFraction = 1f;
}
}

/**
Expand Down Expand Up @@ -150,12 +163,23 @@ void fillIndicator(
paint.setColor(color);
paint.setStrokeWidth(displayedTrackThickness);

float arcFraction =
endFraction >= startFraction
? (endFraction - startFraction)
: (1 + endFraction - startFraction);
startFraction %= 1;
if (totalTrackLengthFraction < 1 && startFraction + arcFraction > 1) {
// Breaks the arc at 0 degree for ESCAPE animation.
fillIndicator(canvas, paint, startFraction, 1, color, drawableAlpha);
fillIndicator(canvas, paint, 1, startFraction + arcFraction, color, drawableAlpha);
return;
}
// Scale start and arc fraction for ESCAPE animation.
startFraction = lerp(1 - totalTrackLengthFraction, 1f, startFraction);
arcFraction = lerp(0f, totalTrackLengthFraction, arcFraction);
// Calculates the start and end in degrees.
float startDegree = startFraction * 360 * arcDirectionFactor;
float arcDegree =
endFraction >= startFraction
? (endFraction - startFraction) * 360 * arcDirectionFactor
: (1 + endFraction - startFraction) * 360 * arcDirectionFactor;
float arcDegree = arcFraction * 360 * arcDirectionFactor;

// Draws the gaps if needed.
if (spec.indicatorTrackGapSize > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.google.android.material.progressindicator;

import static com.google.android.material.math.MathUtils.lerp;
import static com.google.android.material.progressindicator.BaseProgressIndicator.HIDE_ESCAPE;
import static com.google.android.material.progressindicator.BaseProgressIndicator.HIDE_INWARD;
import static com.google.android.material.progressindicator.BaseProgressIndicator.SHOW_OUTWARD;
Expand Down Expand Up @@ -42,6 +43,11 @@ final class LinearDrawingDelegate extends DrawingDelegate<LinearProgressIndicato
private float displayedCornerRadius;
private 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.
@FloatRange(from = 0.0f, to = 1.0f)
private float totalTrackLengthFraction;

/** Instantiates LinearDrawingDelegate with the current spec. */
public LinearDrawingDelegate(@NonNull LinearProgressIndicatorSpec spec) {
super(spec);
Expand Down Expand Up @@ -96,13 +102,11 @@ public void adjustCanvas(
if (isShowing || (isHiding && spec.hideAnimationBehavior != HIDE_ESCAPE)) {
canvas.translate(0f, spec.trackThickness * (trackThicknessFraction - 1) / 2f);
}
// Scales canvas while hiding with escape animation.
// Sets the total track length fraction if ESCAPE hide animation is used.
if (isHiding && spec.hideAnimationBehavior == HIDE_ESCAPE) {
canvas.scale(
trackThicknessFraction,
trackThicknessFraction,
bounds.left + bounds.width() / 2f,
bounds.top + bounds.height() / 2f);
totalTrackLengthFraction = trackThicknessFraction;
} else {
totalTrackLengthFraction = 1f;
}

// Clips all drawing to the track area, so it doesn't draw outside of its bounds (which can
Expand Down Expand Up @@ -139,6 +143,9 @@ public void fillIndicator(
return;
}
color = MaterialColors.compositeARGBWithAlpha(color, drawableAlpha);
// Scale start and end fraction if ESCAPE animation is used.
startFraction = lerp(1 - totalTrackLengthFraction, 1f, startFraction);
endFraction = lerp(1 - totalTrackLengthFraction, 1f, endFraction);

float originX = -trackLength / 2;

Expand Down Expand Up @@ -218,10 +225,11 @@ void fillTrack(
paint.setColor(trackColor);

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

0 comments on commit 98284e7

Please sign in to comment.