Skip to content

Commit 98284e7

Browse files
committed
[ProgressIndicator] Fixed ESCAPE animation in linear and added ESCAPE animation in circular.
PiperOrigin-RevId: 595720674
1 parent b84e654 commit 98284e7

File tree

2 files changed

+43
-11
lines changed

2 files changed

+43
-11
lines changed

lib/java/com/google/android/material/progressindicator/CircularDrawingDelegate.java

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package com.google.android.material.progressindicator;
1717

18+
import static com.google.android.material.math.MathUtils.lerp;
19+
import static com.google.android.material.progressindicator.BaseProgressIndicator.HIDE_ESCAPE;
1820
import static java.lang.Math.min;
1921

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

44+
// This will be used in the ESCAPE hide animation. The start and end fraction in track will be
45+
// scaled by this fraction with a pivot of 1.0f.
46+
@FloatRange(from = 0.0f, to = 1.0f)
47+
private float totalTrackLengthFraction;
48+
4249
/** Instantiates CircularDrawingDelegate with the current spec. */
4350
public CircularDrawingDelegate(@NonNull CircularProgressIndicatorSpec spec) {
4451
super(spec);
@@ -115,6 +122,12 @@ public void adjustCanvas(
115122
// indicator.
116123
adjustedRadius -= (1 - trackThicknessFraction) * spec.trackThickness / 2;
117124
}
125+
// Sets the total track length fraction if ESCAPE hide animation is used.
126+
if (isHiding && spec.hideAnimationBehavior == HIDE_ESCAPE) {
127+
totalTrackLengthFraction = trackThicknessFraction;
128+
} else {
129+
totalTrackLengthFraction = 1f;
130+
}
118131
}
119132

120133
/**
@@ -150,12 +163,23 @@ void fillIndicator(
150163
paint.setColor(color);
151164
paint.setStrokeWidth(displayedTrackThickness);
152165

166+
float arcFraction =
167+
endFraction >= startFraction
168+
? (endFraction - startFraction)
169+
: (1 + endFraction - startFraction);
170+
startFraction %= 1;
171+
if (totalTrackLengthFraction < 1 && startFraction + arcFraction > 1) {
172+
// Breaks the arc at 0 degree for ESCAPE animation.
173+
fillIndicator(canvas, paint, startFraction, 1, color, drawableAlpha);
174+
fillIndicator(canvas, paint, 1, startFraction + arcFraction, color, drawableAlpha);
175+
return;
176+
}
177+
// Scale start and arc fraction for ESCAPE animation.
178+
startFraction = lerp(1 - totalTrackLengthFraction, 1f, startFraction);
179+
arcFraction = lerp(0f, totalTrackLengthFraction, arcFraction);
153180
// Calculates the start and end in degrees.
154181
float startDegree = startFraction * 360 * arcDirectionFactor;
155-
float arcDegree =
156-
endFraction >= startFraction
157-
? (endFraction - startFraction) * 360 * arcDirectionFactor
158-
: (1 + endFraction - startFraction) * 360 * arcDirectionFactor;
182+
float arcDegree = arcFraction * 360 * arcDirectionFactor;
159183

160184
// Draws the gaps if needed.
161185
if (spec.indicatorTrackGapSize > 0) {

lib/java/com/google/android/material/progressindicator/LinearDrawingDelegate.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.google.android.material.progressindicator;
1717

18+
import static com.google.android.material.math.MathUtils.lerp;
1819
import static com.google.android.material.progressindicator.BaseProgressIndicator.HIDE_ESCAPE;
1920
import static com.google.android.material.progressindicator.BaseProgressIndicator.HIDE_INWARD;
2021
import static com.google.android.material.progressindicator.BaseProgressIndicator.SHOW_OUTWARD;
@@ -42,6 +43,11 @@ final class LinearDrawingDelegate extends DrawingDelegate<LinearProgressIndicato
4243
private float displayedCornerRadius;
4344
private Path displayedTrackPath;
4445

46+
// This will be used in the ESCAPE hide animation. The start and end fraction in track will be
47+
// scaled by this fraction with a pivot of 1.0f.
48+
@FloatRange(from = 0.0f, to = 1.0f)
49+
private float totalTrackLengthFraction;
50+
4551
/** Instantiates LinearDrawingDelegate with the current spec. */
4652
public LinearDrawingDelegate(@NonNull LinearProgressIndicatorSpec spec) {
4753
super(spec);
@@ -96,13 +102,11 @@ public void adjustCanvas(
96102
if (isShowing || (isHiding && spec.hideAnimationBehavior != HIDE_ESCAPE)) {
97103
canvas.translate(0f, spec.trackThickness * (trackThicknessFraction - 1) / 2f);
98104
}
99-
// Scales canvas while hiding with escape animation.
105+
// Sets the total track length fraction if ESCAPE hide animation is used.
100106
if (isHiding && spec.hideAnimationBehavior == HIDE_ESCAPE) {
101-
canvas.scale(
102-
trackThicknessFraction,
103-
trackThicknessFraction,
104-
bounds.left + bounds.width() / 2f,
105-
bounds.top + bounds.height() / 2f);
107+
totalTrackLengthFraction = trackThicknessFraction;
108+
} else {
109+
totalTrackLengthFraction = 1f;
106110
}
107111

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

143150
float originX = -trackLength / 2;
144151

@@ -218,10 +225,11 @@ void fillTrack(
218225
paint.setColor(trackColor);
219226

220227
float right = trackLength / 2;
228+
float left = right - trackLength * totalTrackLengthFraction;
221229
float bottom = displayedTrackThickness / 2;
222230
displayedTrackPath = new Path();
223231
displayedTrackPath.addRoundRect(
224-
new RectF(-right, -bottom, right, bottom),
232+
new RectF(left, -bottom, right, bottom),
225233
displayedCornerRadius,
226234
displayedCornerRadius,
227235
Path.Direction.CCW);

0 commit comments

Comments
 (0)