Skip to content

Commit cb5afbc

Browse files
pekingmeleticiarossi
authored andcommitted
[ProgressIndicator] Updated to draw the track in Circular indeterminate mode with an option to opt out this behavior.
PiperOrigin-RevId: 629828525
1 parent 3880efe commit cb5afbc

File tree

4 files changed

+76
-42
lines changed

4 files changed

+76
-42
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ public final class CircularProgressIndicatorSpec extends BaseProgressIndicatorSp
5151
/** The direction in which the indicator will rotate and grow to. */
5252
@IndicatorDirection public int indicatorDirection;
5353

54+
/** Whether to show the track in the indeterminate mode. */
55+
public boolean indeterminateTrackVisible;
56+
5457
/**
5558
* Instantiates the spec for {@link CircularProgressIndicator}.
5659
*
@@ -107,6 +110,8 @@ public CircularProgressIndicatorSpec(
107110
a.getInt(
108111
R.styleable.CircularProgressIndicator_indicatorDirectionCircular,
109112
CircularProgressIndicator.INDICATOR_DIRECTION_CLOCKWISE);
113+
indeterminateTrackVisible =
114+
a.getBoolean(R.styleable.CircularProgressIndicator_indeterminateTrackVisible, true);
110115
a.recycle();
111116

112117
validateSpec();

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,11 @@ public void draw(@NonNull Canvas canvas) {
203203

204204
int gapSize = baseSpec.indicatorTrackGapSize;
205205
int trackAlpha = getAlpha();
206-
boolean drawFullTrack = gapSize == 0 && !baseSpec.hasWavyEffect();
206+
boolean drawTrack =
207+
baseSpec instanceof LinearProgressIndicatorSpec
208+
|| (baseSpec instanceof CircularProgressIndicatorSpec
209+
&& ((CircularProgressIndicatorSpec) baseSpec).indeterminateTrackVisible);
210+
boolean drawFullTrack = drawTrack && gapSize == 0 && !baseSpec.hasWavyEffect();
207211

208212
if (drawFullTrack) {
209213
drawingDelegate.fillTrack(
@@ -214,7 +218,8 @@ public void draw(@NonNull Canvas canvas) {
214218
baseSpec.trackColor,
215219
trackAlpha,
216220
/* gapSize= */ 0);
217-
} else {
221+
} else if (drawTrack) {
222+
// Draws the track with partial length.
218223
ActiveIndicator firstIndicator = animatorDelegate.activeIndicators.get(0);
219224
ActiveIndicator lastIndicator =
220225
animatorDelegate.activeIndicators.get(animatorDelegate.activeIndicators.size() - 1);
@@ -235,8 +240,19 @@ public void draw(@NonNull Canvas canvas) {
235240
baseSpec.trackColor,
236241
trackAlpha,
237242
gapSize);
243+
} else {
244+
canvas.save();
245+
canvas.rotate(lastIndicator.rotationDegree);
246+
drawingDelegate.fillTrack(
247+
canvas,
248+
paint,
249+
lastIndicator.endFraction,
250+
firstIndicator.startFraction + 1f,
251+
baseSpec.trackColor,
252+
trackAlpha,
253+
gapSize);
254+
canvas.restore();
238255
}
239-
// No inactive track is drawn in circular indeterminate mode.
240256
}
241257

242258
// Draws indicators and tracks in between.
@@ -249,7 +265,7 @@ public void draw(@NonNull Canvas canvas) {
249265
drawingDelegate.fillIndicator(canvas, paint, curIndicator, getAlpha());
250266

251267
// Draws tracks between indicators.
252-
if (indicatorIndex > 0 && !drawFullTrack) {
268+
if (indicatorIndex > 0 && !drawFullTrack && drawTrack) {
253269
ActiveIndicator prevIndicator = animatorDelegate.activeIndicators.get(indicatorIndex - 1);
254270
drawingDelegate.fillTrack(
255271
canvas,

lib/java/com/google/android/material/progressindicator/res/values/attrs.xml

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -18,90 +18,90 @@
1818

1919
<declare-styleable name="BaseProgressIndicator">
2020
<!-- Whether the progress indicator should be indeterminate mode. -->
21-
<attr name="android:indeterminate"/>
21+
<attr name="android:indeterminate" />
2222
<!-- The thickness of the progress track and indicator. -->
23-
<attr name="trackThickness" format="dimension"/>
23+
<attr name="trackThickness" format="dimension" />
2424
<!--
2525
The radius of each corner of both the indicator and the track. A radius
2626
larger than half of the track width will throw exceptions during
2727
initialization.
2828
-->
29-
<attr name="trackCornerRadius" format="dimension"/>
29+
<attr name="trackCornerRadius" format="dimension" />
3030
<!--
3131
The indicator color (or colors in an array). By default, it uses theme
3232
primary color.
3333
-->
34-
<attr name="indicatorColor" format="color|reference"/>
34+
<attr name="indicatorColor" format="color|reference" />
3535
<!--
3636
The color used for the progress track. If not defined, it will be set to
3737
the indicatorColor and apply the android:disabledAlpha from the theme.
3838
-->
39-
<attr name="trackColor"/>
39+
<attr name="trackColor" />
4040
<!-- The animation behavior to show the indicator and track. -->
4141
<attr name="showAnimationBehavior">
4242
<!-- No animation used; appears immediately. -->
43-
<enum name="none" value="0"/>
43+
<enum name="none" value="0" />
4444
<!--
4545
Expands from the bottom edge to the top edge for the linear type;
4646
expands from the inner edge to the outer edge for the circular type.
4747
-->
48-
<enum name="outward" value="1"/>
48+
<enum name="outward" value="1" />
4949
<!--
5050
Expands from the top edge to the bottom edge for the linear type;
5151
expands from the outer edge to the inner edge for the circular type.
5252
-->
53-
<enum name="inward" value="2"/>
53+
<enum name="inward" value="2" />
5454
</attr>
5555
<!-- The animation behavior to hide the indicator and track. -->
5656
<attr name="hideAnimationBehavior">
5757
<!-- No animation used; disappears immediately. -->
58-
<enum name="none" value="0"/>
58+
<enum name="none" value="0" />
5959
<!--
6060
Collapses from the bottom edge to the top edge for the linear type;
6161
collapses from the inner edge to the outer edge for the circular type.
6262
-->
63-
<enum name="outward" value="1"/>
63+
<enum name="outward" value="1" />
6464
<!--
6565
Collapses from the top edge to the bottom edge for the linear type;
6666
collapses from the outer edge to the inner edge for the circular type.
6767
-->
68-
<enum name="inward" value="2"/>
68+
<enum name="inward" value="2" />
6969
<!--
7070
Escapes in the progression direction for the linear type;
7171
no effect for the circular type.
7272
-->
73-
<enum name="escape" value="3"/>
73+
<enum name="escape" value="3" />
7474
</attr>
7575
<!--
7676
The time, in milliseconds, that the progress indicator will wait to show
7777
once show() is called. If set to zero or negative values (-1 as default),
7878
the show action will start immediately.
7979
-->
80-
<attr name="showDelay" format="integer"/>
80+
<attr name="showDelay" format="integer" />
8181
<!--
8282
The minimum time, in milliseconds, that the requested hide action will
8383
wait to start once show action is started. If set to zero or negative
8484
values (-1 as default), the requested hide action will start immediately.
8585
This value is capped to a limit defined in ProgressIndicator class.
8686
-->
87-
<attr name="minHideDelay" format="integer"/>
87+
<attr name="minHideDelay" format="integer" />
8888
<!--
8989
The size of the gap between the progress indicator and track.
9090
-->
91-
<attr name="indicatorTrackGapSize" format="dimension"/>
91+
<attr name="indicatorTrackGapSize" format="dimension" />
9292
<!--
9393
Defines the wavelength (in dp) of the wave effect.
9494
-->
95-
<attr name="wavelength" format="dimension"/>
95+
<attr name="wavelength" format="dimension" />
9696
<!--
9797
Defines the amplitude (in dp) of the wave effect.
9898
-->
99-
<attr name="amplitude" format="dimension"/>
99+
<attr name="amplitude" format="dimension" />
100100
<!--
101101
Defines the wave speed (in dp/s) of the wavy effect. If positive, wave moves towards 100%; if
102102
negative, wave moves towards 0%.
103103
-->
104-
<attr name="speed" format="dimension"/>
104+
<attr name="speed" format="dimension" />
105105
</declare-styleable>
106106

107107
<declare-styleable name="LinearProgressIndicator">
@@ -112,55 +112,57 @@
112112
This type is only available when there are three or more indicator
113113
colors.
114114
-->
115-
<enum name="contiguous" value="0"/>
115+
<enum name="contiguous" value="0" />
116116
<!--
117117
There will be two disjoint segments in the same color per cycle. The color iterates between cycles.
118118
-->
119-
<enum name="disjoint" value="1"/>
119+
<enum name="disjoint" value="1" />
120120
</attr>
121121
<!--
122122
The direction in which the linear indicator progresses, in the determinate
123123
mode, and is animated, in the indeterminate mode.
124124
-->
125125
<attr name="indicatorDirectionLinear">
126126
<!-- Animated from the left end to the right end of the track. -->
127-
<enum name="leftToRight" value="0"/>
127+
<enum name="leftToRight" value="0" />
128128
<!-- Animated from the right end to the left end of the track. -->
129-
<enum name="rightToLeft" value="1"/>
129+
<enum name="rightToLeft" value="1" />
130130
<!--
131131
Animated from the start position to the end position of the track.
132132
This will be same as the leftToRight for API before 17.
133133
-->
134-
<enum name="startToEnd" value="2"/>
134+
<enum name="startToEnd" value="2" />
135135
<!--
136136
Animated from the end position to the start position of the track.
137137
This will be same as the rightToLeft for API before 17.
138138
-->
139-
<enum name="endToStart" value="3"/>
139+
<enum name="endToStart" value="3" />
140140
</attr>
141141
<!--
142142
The size of the stop indicator at the end of the progress track.
143143
-->
144-
<attr name="trackStopIndicatorSize"/>
144+
<attr name="trackStopIndicatorSize" />
145145
</declare-styleable>
146146

147147
<declare-styleable name="CircularProgressIndicator">
148+
<!-- Whether to show the track in the indeterminate mode. -->
149+
<attr name="indeterminateTrackVisible" format="boolean" />
148150
<!-- The animation style of the indeterminate mode. -->
149151
<attr name="indeterminateAnimationTypeCircular">
150-
<!-- The active track will grow on the end point and shrink on the start point. -->
151-
<enum name="advance" value="0"/>
152-
<!-- The active track will grow and shrink on the end point. -->
153-
<enum name="retreat" value="1"/>
152+
<!-- The indicator will grow on the end point and shrink on the start point. -->
153+
<enum name="advance" value="0" />
154+
<!-- The indicator track will grow and shrink on the end point. -->
155+
<enum name="retreat" value="1" />
154156
</attr>
155157
<!--
156158
Defines the size (outer diameter) of the circular progress indicator.
157159
-->
158-
<attr name="indicatorSize" format="dimension"/>
160+
<attr name="indicatorSize" format="dimension" />
159161
<!--
160162
The extra space from the outer edge of the indicator to the edge of the
161163
canvas.
162164
-->
163-
<attr name="indicatorInset" format="dimension"/>
165+
<attr name="indicatorInset" format="dimension" />
164166
<!--
165167
The direction in which the circular indicator progresses, in the
166168
determinate mode, and is animated, in the indeterminate mode.
@@ -171,18 +173,18 @@
171173
determinate mode, the indicator will progress from the top (12 o'clock)
172174
clockwise.
173175
-->
174-
<enum name="clockwise" value="0"/>
176+
<enum name="clockwise" value="0" />
175177
<!--
176178
In the indeterminate mode, the spinner will spin counter-clockwise; in
177179
the determinate mode, the indicator will progress from the top (12
178180
o'clock) counter-clockwise.
179181
-->
180-
<enum name="counterclockwise" value="1"/>
182+
<enum name="counterclockwise" value="1" />
181183
</attr>
182184
</declare-styleable>
183185

184186
<!-- Style to use for LinearProgressIndicator in this theme. -->
185-
<attr name="linearProgressIndicatorStyle" format="reference"/>
187+
<attr name="linearProgressIndicatorStyle" format="reference" />
186188
<!-- Style to use for CircularProgressIndicator in this theme. -->
187-
<attr name="circularProgressIndicatorStyle" format="reference"/>
189+
<attr name="circularProgressIndicatorStyle" format="reference" />
188190
</resources>

lib/java/com/google/android/material/progressindicator/res/values/styles.xml

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
<item name="indicatorInset">@dimen/mtrl_progress_circular_inset_medium</item>
3535
<item name="trackColor">@android:color/transparent</item>
3636
<item name="indicatorDirectionCircular">clockwise</item>
37+
<item name="indeterminateTrackVisible">true</item>
3738
</style>
3839
<style name="Widget.MaterialComponents.CircularProgressIndicator.Medium"/>
3940
<style name="Widget.MaterialComponents.CircularProgressIndicator.Small">
@@ -65,9 +66,18 @@
6566
<item name="trackCornerRadius">0dp</item>
6667
<item name="indicatorTrackGapSize">0dp</item>
6768
</style>
68-
<style name="Widget.Material3.CircularProgressIndicator.Legacy.Medium" parent="Widget.MaterialComponents.CircularProgressIndicator.Medium"/>
69-
<style name="Widget.Material3.CircularProgressIndicator.Legacy.Small" parent="Widget.MaterialComponents.CircularProgressIndicator.Small"/>
70-
<style name="Widget.Material3.CircularProgressIndicator.Legacy.ExtraSmall" parent="Widget.MaterialComponents.CircularProgressIndicator.ExtraSmall"/>
69+
<style name="Widget.Material3.CircularProgressIndicator.Legacy.Medium" parent="Widget.MaterialComponents.CircularProgressIndicator.Medium">
70+
<item name="trackCornerRadius">0dp</item>
71+
<item name="indicatorTrackGapSize">0dp</item>
72+
</style>
73+
<style name="Widget.Material3.CircularProgressIndicator.Legacy.Small" parent="Widget.MaterialComponents.CircularProgressIndicator.Small">
74+
<item name="trackCornerRadius">0dp</item>
75+
<item name="indicatorTrackGapSize">0dp</item>
76+
</style>
77+
<style name="Widget.Material3.CircularProgressIndicator.Legacy.ExtraSmall" parent="Widget.MaterialComponents.CircularProgressIndicator.ExtraSmall">
78+
<item name="trackCornerRadius">0dp</item>
79+
<item name="indicatorTrackGapSize">0dp</item>
80+
</style>
7181

7282
<!-- M3 LinearProgressIndicator style -->
7383
<style name="Widget.Material3.LinearProgressIndicator" parent="Widget.MaterialComponents.LinearProgressIndicator">
@@ -86,6 +96,7 @@
8696
<item name="trackColor">@macro/m3_comp_progress_indicator_track_color</item>
8797
<item name="trackCornerRadius">2dp</item>
8898
<item name="indicatorTrackGapSize">@dimen/m3_comp_progress_indicator_active_indicator_track_space</item>
99+
<item name="indeterminateTrackVisible">false</item>
89100
</style>
90101
<style name="Widget.Material3.CircularProgressIndicator.Medium"/>
91102
<style name="Widget.Material3.CircularProgressIndicator.Small">

0 commit comments

Comments
 (0)