/
DeterminateDrawable.java
244 lines (207 loc) · 8.73 KB
/
DeterminateDrawable.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
/*
* 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 android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import androidx.annotation.NonNull;
import androidx.dynamicanimation.animation.DynamicAnimation;
import androidx.dynamicanimation.animation.FloatPropertyCompat;
import androidx.dynamicanimation.animation.SpringAnimation;
import androidx.dynamicanimation.animation.SpringForce;
import com.google.android.material.color.MaterialColors;
/** This class draws the graphics for determinate mode. */
public final class DeterminateDrawable<S extends BaseProgressIndicatorSpec>
extends DrawableWithAnimatedVisibilityChange {
// Constants for drawing progress.
private static final int MAX_DRAWABLE_LEVEL = 10000;
// The constant for spring force stiffness.
private static final float SPRING_FORCE_STIFFNESS = SpringForce.STIFFNESS_VERY_LOW;
// Drawing delegate object.
private DrawingDelegate<S> drawingDelegate;
// Animation.
private final SpringForce springForce;
private final SpringAnimation springAnimation;
// Fraction of displayed indicator in the total width.
private float indicatorFraction;
// Whether to skip the spring animation on level change event.
private boolean skipAnimationOnLevelChange = false;
DeterminateDrawable(
@NonNull Context context,
@NonNull BaseProgressIndicatorSpec baseSpec,
@NonNull DrawingDelegate<S> drawingDelegate) {
super(context, baseSpec);
setDrawingDelegate(drawingDelegate);
springForce = new SpringForce();
springForce.setDampingRatio(SpringForce.DAMPING_RATIO_NO_BOUNCY);
springForce.setStiffness(SPRING_FORCE_STIFFNESS);
springAnimation = new SpringAnimation(this, INDICATOR_LENGTH_IN_LEVEL);
springAnimation.setSpring(springForce);
setGrowFraction(1f);
}
/**
* Creates an instance of {@link DeterminateDrawable} for {@link LinearProgressIndicator} with
* {@link LinearProgressIndicatorSpec}.
*
* @param context The current context.
* @param spec The spec for the linear indicator.
*/
@NonNull
public static DeterminateDrawable<LinearProgressIndicatorSpec> createLinearDrawable(
@NonNull Context context, @NonNull LinearProgressIndicatorSpec spec) {
return new DeterminateDrawable<>(context, /*baseSpec=*/ spec, new LinearDrawingDelegate(spec));
}
/**
* Creates an instance of {@link DeterminateDrawable} for {@link CircularProgressIndicator} with
* {@link CircularProgressIndicatorSpec}.
*
* @param context The current context.
* @param spec The spec for the circular indicator.
*/
@NonNull
public static DeterminateDrawable<CircularProgressIndicatorSpec> createCircularDrawable(
@NonNull Context context, @NonNull CircularProgressIndicatorSpec spec) {
return new DeterminateDrawable<>(
context, /*baseSpec=*/ spec, new CircularDrawingDelegate(spec));
}
public void addSpringAnimationEndListener(
@NonNull DynamicAnimation.OnAnimationEndListener listener) {
springAnimation.addEndListener(listener);
}
public void removeSpringAnimationEndListener(
@NonNull DynamicAnimation.OnAnimationEndListener listener) {
springAnimation.removeEndListener(listener);
}
// ******************* Overridden methods *******************
/**
* Sets the visibility of this drawable. It calls the {@link
* DrawableWithAnimatedVisibilityChange#setVisible(boolean, boolean)} to start the show/hide
* animation properly. The spring animation will be skipped when the level changes, if animation
* is not requested.
*
* @param visible Whether to make the drawable visible.
* @param restart Whether to force starting the animation from the beginning. Doesn't apply to the
* spring animation for changing progress.
* @param animate Whether to change the visibility with animation. The spring animation for
* changing progress only depends on system animator duration scale. Use {@link
* BaseProgressIndicator#setProgressCompat(int, boolean)} to change the progress without
* animation.
* @return {@code true}, if the visibility changes or will change after the animation; {@code
* false}, otherwise.
*/
@Override
boolean setVisibleInternal(boolean visible, boolean restart, boolean animate) {
boolean changed = super.setVisibleInternal(visible, restart, animate);
float systemAnimatorDurationScale =
animatorDurationScaleProvider.getSystemAnimatorDurationScale(context.getContentResolver());
if (systemAnimatorDurationScale == 0) {
skipAnimationOnLevelChange = true;
} else {
skipAnimationOnLevelChange = false;
springForce.setStiffness(SPRING_FORCE_STIFFNESS / systemAnimatorDurationScale);
}
return changed;
}
/** Skips the animation of changing indicator length, directly displays the target progress. */
@Override
public void jumpToCurrentState() {
// Set spring target value to the current level (may not be same as the desired level, depends
// on the completion of progress animation) and end the animation immediately.
springAnimation.skipToEnd();
setIndicatorFraction((float) getLevel() / MAX_DRAWABLE_LEVEL);
}
/**
* When progress is updated, it changes the level of drawable's level and calls this method
* afterward. It sets the new progress value to animation and starts the animation.
*
* @param level New progress level.
*/
@Override
protected boolean onLevelChange(int level) {
if (skipAnimationOnLevelChange) {
springAnimation.skipToEnd();
setIndicatorFraction((float) level / MAX_DRAWABLE_LEVEL);
} else {
springAnimation.setStartValue(getIndicatorFraction() * MAX_DRAWABLE_LEVEL);
springAnimation.animateToFinalPosition(level);
}
return true;
}
@Override
public int getIntrinsicWidth() {
return drawingDelegate.getPreferredWidth();
}
@Override
public int getIntrinsicHeight() {
return drawingDelegate.getPreferredHeight();
}
/**
* Sets the drawable level with a fraction [0,1] of the progress. Note: this function is not used
* to force updating the level in opposite to the automatic level updates by framework {@link
* ProgressBar}.
*
* @param fraction Progress in fraction, in [0, 1].
*/
void setLevelByFraction(float fraction) {
setLevel((int) (MAX_DRAWABLE_LEVEL * fraction));
}
// ******************* Drawing methods *******************
@Override
public void draw(@NonNull Canvas canvas) {
Rect clipBounds = new Rect();
if (getBounds().isEmpty() || !isVisible() || !canvas.getClipBounds(clipBounds)) {
// Escape if bounds are empty, clip bounds are empty, or currently hidden.
return;
}
canvas.save();
drawingDelegate.validateSpecAndAdjustCanvas(canvas, getBounds(), getGrowFraction());
// Draws the track.
drawingDelegate.fillTrack(canvas, paint);
// Draws the indicator.
int indicatorColor =
MaterialColors.compositeARGBWithAlpha(baseSpec.indicatorColors[0], getAlpha());
drawingDelegate.fillIndicator(canvas, paint, 0f, getIndicatorFraction(), indicatorColor);
canvas.restore();
}
// ******************* Getters and setters *******************
private float getIndicatorFraction() {
return indicatorFraction;
}
private void setIndicatorFraction(float indicatorFraction) {
this.indicatorFraction = indicatorFraction;
invalidateSelf();
}
@NonNull
DrawingDelegate<S> getDrawingDelegate() {
return drawingDelegate;
}
void setDrawingDelegate(@NonNull DrawingDelegate<S> drawingDelegate) {
this.drawingDelegate = drawingDelegate;
drawingDelegate.registerDrawable(this);
}
// ******************* Properties *******************
private static final FloatPropertyCompat<DeterminateDrawable> INDICATOR_LENGTH_IN_LEVEL =
new FloatPropertyCompat<DeterminateDrawable>("indicatorLevel") {
@Override
public float getValue(DeterminateDrawable drawable) {
return drawable.getIndicatorFraction() * MAX_DRAWABLE_LEVEL;
}
@Override
public void setValue(DeterminateDrawable drawable, float value) {
drawable.setIndicatorFraction(value / MAX_DRAWABLE_LEVEL);
}
};
}