Skip to content

Commit fb6a97c

Browse files
paulfthomashunterstich
authored andcommitted
[NTC][ProgressIndicator] Internal changes
PiperOrigin-RevId: 588435272
1 parent 59eaee3 commit fb6a97c

File tree

3 files changed

+37
-25
lines changed

3 files changed

+37
-25
lines changed

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import android.util.Log;
3131
import androidx.annotation.NonNull;
3232
import androidx.annotation.Nullable;
33+
import androidx.annotation.Px;
3334
import androidx.annotation.RestrictTo;
3435
import androidx.annotation.RestrictTo.Scope;
3536
import androidx.annotation.VisibleForTesting;
@@ -47,6 +48,7 @@ public final class IndeterminateDrawable<S extends BaseProgressIndicatorSpec>
4748
private IndeterminateAnimatorDelegate<ObjectAnimator> animatorDelegate;
4849

4950
private Drawable staticDummyDrawable;
51+
@Px private int tempIndicatorTrackGapSize;
5052

5153
IndeterminateDrawable(
5254
@NonNull Context context,
@@ -55,10 +57,17 @@ public final class IndeterminateDrawable<S extends BaseProgressIndicatorSpec>
5557
@NonNull IndeterminateAnimatorDelegate<ObjectAnimator> animatorDelegate) {
5658
super(context, baseSpec);
5759

60+
tempIndicatorTrackGapSize = baseSpec.indicatorTrackGapSize;
5861
setDrawingDelegate(drawingDelegate);
5962
setAnimatorDelegate(animatorDelegate);
6063
}
6164

65+
@Override
66+
public void invalidateSelf() {
67+
tempIndicatorTrackGapSize = baseSpec.indicatorTrackGapSize;
68+
super.invalidateSelf();
69+
}
70+
6271
/**
6372
* Creates an instance of {@link IndeterminateDrawable} for {@link LinearProgressIndicator} with
6473
* {@link LinearProgressIndicatorSpec}.
@@ -168,13 +177,11 @@ public void draw(@NonNull Canvas canvas) {
168177
canvas.save();
169178
drawingDelegate.validateSpecAndAdjustCanvas(canvas, getBounds(), getGrowFraction());
170179

171-
if (baseSpec.indicatorTrackGapSize > 0) {
180+
if (tempIndicatorTrackGapSize > 0) {
172181
if (drawingDelegate instanceof LinearDrawingDelegate) {
173182
((LinearProgressIndicatorSpec) drawingDelegate.spec).trackStopIndicatorSize = 0;
174183
} else if (drawingDelegate instanceof CircularDrawingDelegate) {
175-
// TODO: workaround preventing exiting the indicatorTrackGapSize > 0 logic while keeping
176-
// the animation smooth.
177-
baseSpec.indicatorTrackGapSize = 1;
184+
baseSpec.indicatorTrackGapSize = 0;
178185
}
179186

180187
// Draws the transparent track.

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

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,10 @@ public void adjustCanvas(
9494
// Scales canvas while hiding with escape animation.
9595
if (drawable.isHiding() && spec.hideAnimationBehavior == HIDE_ESCAPE) {
9696
canvas.scale(
97-
trackThicknessFraction, trackThicknessFraction, bounds.left + bounds.width() / 2f, 0);
97+
trackThicknessFraction,
98+
trackThicknessFraction,
99+
bounds.left + bounds.width() / 2f,
100+
bounds.top + bounds.height() / 2f);
98101
}
99102

100103
// Clips all drawing to the track area, so it doesn't draw outside of its bounds (which can
@@ -136,11 +139,6 @@ public void fillIndicator(
136139
float endPx = endFraction * trackLength;
137140
float gapSize = min(spec.indicatorTrackGapSize, startPx);
138141

139-
// No need to draw if the indicator starts at or after the stop indicator.
140-
if (startPx + gapSize >= trackLength - spec.trackStopIndicatorSize) {
141-
return;
142-
}
143-
144142
float adjustedStartX = originX + startPx + gapSize;
145143
// TODO: workaround to maintain pixel-perfect compatibility with drawing logic
146144
// not using indicatorTrackGapSize.
@@ -149,10 +147,6 @@ public void fillIndicator(
149147
adjustedStartX -= displayedCornerRadius * 2;
150148
}
151149
float adjustedEndX = originX + endPx;
152-
// Prevents drawing over the stop indicator.
153-
if (endPx == trackLength) {
154-
adjustedEndX -= spec.trackStopIndicatorSize;
155-
}
156150

157151
// Sets up the paint.
158152
paint.setStyle(Style.FILL);
@@ -169,9 +163,28 @@ public void fillIndicator(
169163
adjustedEndX,
170164
displayedTrackThickness / 2);
171165
canvas.drawRoundRect(indicatorBounds, displayedCornerRadius, displayedCornerRadius, paint);
166+
167+
// Draw stop indicator
168+
if (spec.trackStopIndicatorSize > 0) {
169+
drawStopIndicator(canvas, paint);
170+
}
172171
canvas.restore();
173172
}
174173

174+
private void drawStopIndicator(@NonNull Canvas canvas, @NonNull Paint paint) {
175+
int indicatorColor =
176+
MaterialColors.compositeARGBWithAlpha(spec.indicatorColors[0], drawable.getAlpha());
177+
paint.setColor(indicatorColor);
178+
Rect trackBounds = canvas.getClipBounds();
179+
RectF stopBounds =
180+
new RectF(
181+
trackBounds.right - spec.trackStopIndicatorSize,
182+
-displayedTrackThickness / 2,
183+
trackBounds.right,
184+
displayedTrackThickness / 2);
185+
canvas.drawRoundRect(stopBounds, displayedCornerRadius, displayedCornerRadius, paint);
186+
}
187+
175188
/**
176189
* Fills the whole track with track color.
177190
*
@@ -196,13 +209,5 @@ void fillTrack(@NonNull Canvas canvas, @NonNull Paint paint) {
196209
displayedCornerRadius,
197210
Path.Direction.CCW);
198211
canvas.drawPath(displayedTrackPath, paint);
199-
200-
if (spec.trackStopIndicatorSize > 0) {
201-
int indicatorColor =
202-
MaterialColors.compositeARGBWithAlpha(spec.indicatorColors[0], drawable.getAlpha());
203-
paint.setColor(indicatorColor);
204-
RectF stopBounds = new RectF(right - spec.trackStopIndicatorSize, -bottom, right, bottom);
205-
canvas.drawRoundRect(stopBounds, displayedCornerRadius, displayedCornerRadius, paint);
206-
}
207212
}
208213
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,11 @@ void validateSpec() {
109109
}
110110
if (indeterminateAnimationType
111111
== LinearProgressIndicator.INDETERMINATE_ANIMATION_TYPE_CONTIGUOUS) {
112-
if (trackCornerRadius > 0) {
112+
if (trackCornerRadius > 0 && indicatorTrackGapSize == 0) {
113113
// Throws an exception if trying to use the cornered indicator/track with contiguous
114-
// indeterminate animation type.
114+
// indeterminate animation type without gap.
115115
throw new IllegalArgumentException(
116-
"Rounded corners are not supported in contiguous indeterminate animation.");
116+
"Rounded corners without gap are not supported in contiguous indeterminate animation.");
117117
}
118118
if (indicatorColors.length < 3) {
119119
// Throws an exception if trying to set contiguous indeterminate animation with less than 3

0 commit comments

Comments
 (0)