-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathLinearProgressIndicator.java
393 lines (355 loc) · 14.9 KB
/
LinearProgressIndicator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
* Copyright (C) 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.material.progressindicator;
import com.google.android.material.R;
import static java.lang.Math.min;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import androidx.annotation.AttrRes;
import androidx.annotation.IntDef;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.Px;
import androidx.annotation.RestrictTo;
import androidx.annotation.RestrictTo.Scope;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Objects;
/**
* This class implements the linear type progress indicators.
*
* <p>With the default style {@code Widget.MaterialComponents.LinearProgressIndicator}, 4dp
* indicator/track thickness is used without animation is used for visibility change. Without
* customization, primaryColor will be used as the indicator color; the track is the (first)
* indicator color applying the disabledAlpha. The following attributes can be used to customize the
* component's appearance:
*
* <ul>
* <li>{@code trackThickness}: the thickness of the indicator and track.
* <li>{@code indicatorColor}: the color(s) of the indicator.
* <li>{@code trackColor}: the color of the track.
* <li>{@code trackCornerRadius}: the radius of the rounded corner of the indicator and track.
* <li>{@code indeterminateAnimationType}: the type of indeterminate animation.
* <li>{@code indicatorDirectionLinear}: the sweeping direction of the indicator.
* </ul>
*
* <p>For more information, see the <a
* href="https://github.com/material-components/material-components-android/blob/master/docs/components/ProgressIndicator.md">component
* developer guidance</a> and <a
* href="https://material.io/components/progress-indicators/overview">design guidelines</a>.
*/
public class LinearProgressIndicator
extends BaseProgressIndicator<LinearProgressIndicatorSpec> {
public static final int DEF_STYLE_RES = R.style.Widget_MaterialComponents_LinearProgressIndicator;
/**
* Used in the indeterminate animation type setter for contiguous animation, which animates the
* connecting active segment(s) occupying the full track.
*/
public static final int INDETERMINATE_ANIMATION_TYPE_CONTIGUOUS = 0;
/**
* Used in the indeterminate animation type setter for disjoint animation, which animates the
* active segment(s) with noticeable gaps.
*/
public static final int INDETERMINATE_ANIMATION_TYPE_DISJOINT = 1;
public static final int INDICATOR_DIRECTION_LEFT_TO_RIGHT = 0;
public static final int INDICATOR_DIRECTION_RIGHT_TO_LEFT = 1;
public static final int INDICATOR_DIRECTION_START_TO_END = 2;
public static final int INDICATOR_DIRECTION_END_TO_START = 3;
// **************** Constructors ****************
public LinearProgressIndicator(@NonNull Context context) {
this(context, null);
}
public LinearProgressIndicator(@NonNull Context context, @Nullable AttributeSet attrs) {
this(context, attrs, R.attr.linearProgressIndicatorStyle);
}
public LinearProgressIndicator(
@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes final int defStyleAttr) {
super(context, attrs, defStyleAttr, DEF_STYLE_RES);
initializeDrawables();
}
// **************** Inherited functions ****************
@Override
LinearProgressIndicatorSpec createSpec(@NonNull Context context, @NonNull AttributeSet attrs) {
return new LinearProgressIndicatorSpec(context, attrs);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
// In case that layout direction is changed, update the spec.
spec.drawHorizontallyInverse =
spec.indicatorDirection == INDICATOR_DIRECTION_RIGHT_TO_LEFT
|| (getLayoutDirection() == View.LAYOUT_DIRECTION_RTL
&& spec.indicatorDirection == INDICATOR_DIRECTION_START_TO_END)
|| (getLayoutDirection() == View.LAYOUT_DIRECTION_LTR
&& spec.indicatorDirection == INDICATOR_DIRECTION_END_TO_START);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
int contentWidth = w - (getPaddingLeft() + getPaddingRight());
int contentHeight = h - (getPaddingTop() + getPaddingBottom());
Drawable drawable = getIndeterminateDrawable();
if (drawable != null) {
drawable.setBounds(/*left=*/ 0, /*top=*/ 0, contentWidth, contentHeight);
}
drawable = getProgressDrawable();
if (drawable != null) {
drawable.setBounds(/*left=*/ 0, /*top=*/ 0, contentWidth, contentHeight);
}
}
// ******************** Initialization **********************
private void initializeDrawables() {
LinearDrawingDelegate drawingDelegate = new LinearDrawingDelegate(spec);
setIndeterminateDrawable(
IndeterminateDrawable.createLinearDrawable(getContext(), spec, drawingDelegate));
setProgressDrawable(
DeterminateDrawable.createLinearDrawable(getContext(), spec, drawingDelegate));
}
// **************** Getters and setters ****************
/**
* Sets the colors used in the indicator of this progress indicator.
*
* @param indicatorColors The new colors used in indicator.
* @throws IllegalArgumentException if there are less than 3 indicator colors when
* indeterminateAnimationType is {@link #INDETERMINATE_ANIMATION_TYPE_CONTIGUOUS}.
*/
@Override
public void setIndicatorColor(@NonNull int... indicatorColors) {
super.setIndicatorColor(indicatorColors);
spec.validateSpec();
}
/**
* Sets the radius of the rounded corner for the indicator and track in pixels.
*
* @param trackCornerRadius The new corner radius in pixels.
* @throws IllegalArgumentException if trackCornerRadius is not zero, when
* indeterminateAnimationType is {@link #INDETERMINATE_ANIMATION_TYPE_CONTIGUOUS}.
*/
@Override
public void setTrackCornerRadius(int trackCornerRadius) {
super.setTrackCornerRadius(trackCornerRadius);
spec.validateSpec();
invalidate();
}
/**
* Returns the radius of the rounded inner corner for the indicator and track in pixels.
*
* @see #setTrackInnerCornerRadius(int)
* @see #setTrackInnerCornerRadiusFraction(int)
* @attr ref
* com.google.android.material.progressindicator.R.styleable#LinearProgressIndicator_trackInnerCornerRadius
*/
@Px
public int getTrackInnerCornerRadius() {
return spec.trackInnerCornerRadius;
}
/**
* Sets the radius of the rounded inner corner for the indicator and track in pixels.
*
* @param trackInnerCornerRadius The new corner radius in pixels.
* @see #setTrackInnerCornerRadiusFraction(float)
* @see #getTrackInnerCornerRadius()
* @attr ref
* com.google.android.material.progressindicator.R.styleable#LinearProgressIndicator_trackInnerCornerRadius
*/
public void setTrackInnerCornerRadius(@Px int trackInnerCornerRadius) {
if (spec.trackInnerCornerRadius != trackInnerCornerRadius) {
spec.trackInnerCornerRadius =
Math.round(min(trackInnerCornerRadius, spec.trackThickness / 2f));
spec.useRelativeTrackInnerCornerRadius = false;
spec.hasInnerCornerRadius = true;
spec.validateSpec();
invalidate();
}
}
/**
* Sets the radius of the rounded inner corner for the indicator and track in fraction of the
* track thickness.
*
* @param trackInnerCornerRadiusFraction The new corner radius in fraction of the track thickness.
* @see #setTrackInnerCornerRadius(int)
* @see #getTrackInnerCornerRadius()
* @attr ref
* com.google.android.material.progressindicator.R.styleable#LinearProgressIndicator_trackInnerCornerRadius
*/
public void setTrackInnerCornerRadiusFraction(float trackInnerCornerRadiusFraction) {
if (spec.trackInnerCornerRadiusFraction != trackInnerCornerRadiusFraction) {
spec.trackInnerCornerRadiusFraction = min(trackInnerCornerRadiusFraction, 0.5f);
spec.useRelativeTrackInnerCornerRadius = true;
spec.hasInnerCornerRadius = true;
spec.validateSpec();
invalidate();
}
}
/**
* Returns the size of the stop indicator at the end of the track in pixels.
*
* @see #setTrackStopIndicatorSize(int)
* @attr ref
* com.google.android.material.progressindicator.R.styleable#LinearProgressIndicator_trackStopIndicatorSize
*/
@Px
public int getTrackStopIndicatorSize() {
return spec.trackStopIndicatorSize;
}
/**
* Sets the size of the stop indicator at the end of the track in pixels.
*
* @param trackStopIndicatorSize The new stop indicator size in pixels.
* @see #getTrackStopIndicatorSize()
* @attr ref
* com.google.android.material.progressindicator.R.styleable#LinearProgressIndicator_trackStopIndicatorSize
*/
public void setTrackStopIndicatorSize(@Px int trackStopIndicatorSize) {
if (spec.trackStopIndicatorSize != trackStopIndicatorSize) {
spec.trackStopIndicatorSize = min(trackStopIndicatorSize, spec.trackThickness);
spec.validateSpec();
invalidate();
}
}
/**
* Returns the padding of the stop indicator at the end of the track in pixels.
*
* @see #setTrackStopIndicatorPadding(int)
* @attr ref
* com.google.android.material.progressindicator.R.styleable#LinearProgressIndicator_trackStopIndicatorPadding
*/
@Nullable
public Integer getTrackStopIndicatorPadding() {
return spec.trackStopIndicatorPadding;
}
/**
* Sets the padding of the stop indicator at the end of the track in pixels.
*
* @param trackStopIndicatorPadding The new stop indicator padding in pixels.
* @see #getTrackStopIndicatorPadding()
* @attr ref
* com.google.android.material.progressindicator.R.styleable#LinearProgressIndicator_trackStopIndicatorPadding
*/
public void setTrackStopIndicatorPadding(@Nullable Integer trackStopIndicatorPadding) {
if (!Objects.equals(spec.trackStopIndicatorPadding, trackStopIndicatorPadding)) {
spec.trackStopIndicatorPadding = trackStopIndicatorPadding;
invalidate();
}
}
/**
* Returns the type of indeterminate animation of this progress indicator.
*
* @see #setIndeterminateAnimationType(int)
* @attr ref
* com.google.android.material.progressindicator.R.styleable#LinearProgressIndicator_indeterminateAnimationType
*/
@IndeterminateAnimationType
public int getIndeterminateAnimationType() {
return spec.indeterminateAnimationType;
}
/**
* Sets the type of indeterminate animation.
*
* @param indeterminateAnimationType The new type of indeterminate animation.
* @see #getIndeterminateAnimationType()
* @attr ref
* com.google.android.material.progressindicator.R.styleable#LinearProgressIndicator_indeterminateAnimationType
*/
public void setIndeterminateAnimationType(
@IndeterminateAnimationType int indeterminateAnimationType) {
if (spec.indeterminateAnimationType == indeterminateAnimationType) {
return;
}
if (visibleToUser() && isIndeterminate()) {
throw new IllegalStateException(
"Cannot change indeterminate animation type while the progress indicator is show in"
+ " indeterminate mode.");
}
spec.indeterminateAnimationType = indeterminateAnimationType;
spec.validateSpec();
if (indeterminateAnimationType == INDETERMINATE_ANIMATION_TYPE_CONTIGUOUS) {
getIndeterminateDrawable()
.setAnimatorDelegate(new LinearIndeterminateContiguousAnimatorDelegate(spec));
} else {
getIndeterminateDrawable()
.setAnimatorDelegate(new LinearIndeterminateDisjointAnimatorDelegate(getContext(), spec));
}
registerSwitchIndeterminateModeCallback();
invalidate();
}
/**
* Returns the indicator animating direction used in this progress indicator.
*
* @see #setIndicatorDirection(int)
* @attr ref
* com.google.android.material.progressindicator.R.styleable#LinearProgressIndicator_indicatorDirectionLinear
*/
@IndicatorDirection
public int getIndicatorDirection() {
return spec.indicatorDirection;
}
/**
* Sets the indicator animating direction used in this progress indicator.
*
* @param indicatorDirection The new indicator direction.
* @see #getIndicatorDirection()
* @attr ref
* com.google.android.material.progressindicator.R.styleable#LinearProgressIndicator_indicatorDirectionLinear
*/
public void setIndicatorDirection(@IndicatorDirection int indicatorDirection) {
spec.indicatorDirection = indicatorDirection;
spec.drawHorizontallyInverse =
indicatorDirection == INDICATOR_DIRECTION_RIGHT_TO_LEFT
|| (getLayoutDirection() == View.LAYOUT_DIRECTION_RTL
&& spec.indicatorDirection == INDICATOR_DIRECTION_START_TO_END)
|| (getLayoutDirection() == View.LAYOUT_DIRECTION_LTR
&& indicatorDirection == INDICATOR_DIRECTION_END_TO_START);
invalidate();
}
/**
* Sets the current progress to the specified value with/without animation based on the input.
*
* <p>If it's in the indeterminate mode and using disjoint animation, it will smoothly transition
* to determinate mode by finishing the current indeterminate animation cycle.
*
* @param progress The new progress value.
* @param animated Whether to update the progress with the animation.
* @see BaseProgressIndicator#setProgress(int)
*/
@Override
public void setProgressCompat(int progress, boolean animated) {
// Doesn't support to switching into determinate mode while contiguous animation is used.
if (spec != null
&& spec.indeterminateAnimationType == INDETERMINATE_ANIMATION_TYPE_CONTIGUOUS
&& isIndeterminate()) {
return;
}
super.setProgressCompat(progress, animated);
}
// **************** Interface ****************
/** @hide */
@RestrictTo(Scope.LIBRARY_GROUP)
@IntDef({INDETERMINATE_ANIMATION_TYPE_CONTIGUOUS, INDETERMINATE_ANIMATION_TYPE_DISJOINT})
@Retention(RetentionPolicy.SOURCE)
public @interface IndeterminateAnimationType {}
/** @hide */
@RestrictTo(Scope.LIBRARY_GROUP)
@IntDef({
INDICATOR_DIRECTION_LEFT_TO_RIGHT,
INDICATOR_DIRECTION_RIGHT_TO_LEFT,
INDICATOR_DIRECTION_START_TO_END,
INDICATOR_DIRECTION_END_TO_START
})
@Retention(RetentionPolicy.SOURCE)
public @interface IndicatorDirection {}
}