Skip to content

Commit 659e3bc

Browse files
pekingmedrchen
authored andcommitted
[ProgressIndicator] Updated SpringAnimation's minimum visible change and added APIs to customize spring force in DeterminateDrawable.
PiperOrigin-RevId: 798225060
1 parent fe1c0d6 commit 659e3bc

File tree

1 file changed

+42
-8
lines changed

1 file changed

+42
-8
lines changed

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

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import com.google.android.material.R;
2020

21+
import static java.lang.Math.min;
22+
2123
import android.animation.TimeInterpolator;
2224
import android.animation.ValueAnimator;
2325
import android.content.Context;
@@ -57,7 +59,6 @@ public final class DeterminateDrawable<S extends BaseProgressIndicatorSpec>
5759
private DrawingDelegate<S> drawingDelegate;
5860

5961
// Animation.
60-
private final SpringForce springForce;
6162
private final SpringAnimation springAnimation;
6263
// Active indicator for the progress.
6364
private final ActiveIndicator activeIndicator;
@@ -83,13 +84,11 @@ public final class DeterminateDrawable<S extends BaseProgressIndicatorSpec>
8384
activeIndicator.isDeterminate = true;
8485

8586
// Initializes a spring animator for progress animation.
86-
springForce = new SpringForce();
87-
88-
springForce.setDampingRatio(SpringForce.DAMPING_RATIO_NO_BOUNCY);
89-
springForce.setStiffness(SPRING_FORCE_STIFFNESS);
90-
9187
springAnimation = new SpringAnimation(this, INDICATOR_LENGTH_IN_LEVEL);
92-
springAnimation.setSpring(springForce);
88+
springAnimation.setSpring(
89+
new SpringForce()
90+
.setDampingRatio(SpringForce.DAMPING_RATIO_NO_BOUNCY)
91+
.setStiffness(SPRING_FORCE_STIFFNESS));
9392

9493
// Initializes a linear animator to enforce phase animation when progress is unchanged.
9594
phaseAnimator = new ValueAnimator();
@@ -231,7 +230,9 @@ boolean setVisibleInternal(boolean visible, boolean restart, boolean animate) {
231230
skipAnimationOnLevelChange = true;
232231
} else {
233232
skipAnimationOnLevelChange = false;
234-
springForce.setStiffness(SPRING_FORCE_STIFFNESS / systemAnimatorDurationScale);
233+
springAnimation
234+
.getSpring()
235+
.setStiffness(SPRING_FORCE_STIFFNESS / systemAnimatorDurationScale);
235236
}
236237

237238
return changed;
@@ -260,12 +261,34 @@ protected boolean onLevelChange(int level) {
260261
setIndicatorFraction((float) level / MAX_DRAWABLE_LEVEL);
261262
setAmplitudeFraction(nextAmplitudeFraction);
262263
} else {
264+
// Update min visible change to the recommended value.
265+
updateSpringMinVisibleChange();
263266
springAnimation.setStartValue(getIndicatorFraction() * MAX_DRAWABLE_LEVEL);
264267
springAnimation.animateToFinalPosition(level);
265268
}
266269
return true;
267270
}
268271

272+
/**
273+
* Updates the minimum visible change of the spring animation controlling the indicator length
274+
* (progress) to the recommended value based on the track's length.
275+
*/
276+
private void updateSpringMinVisibleChange() {
277+
int width = getBounds().width();
278+
int height = getBounds().height();
279+
if (width <= 0 || height <= 0) {
280+
return;
281+
}
282+
if (drawingDelegate instanceof LinearDrawingDelegate) {
283+
// Track length is the width of the drawable.
284+
springAnimation.setMinimumVisibleChange((float) MAX_DRAWABLE_LEVEL / width);
285+
} else {
286+
// Track length is the perimeter of the circle fit in the drawable.
287+
springAnimation.setMinimumVisibleChange(
288+
(float) (MAX_DRAWABLE_LEVEL / (min(height, width) * Math.PI)));
289+
}
290+
}
291+
269292
@Override
270293
public int getIntrinsicWidth() {
271294
return drawingDelegate.getPreferredWidth();
@@ -276,6 +299,17 @@ public int getIntrinsicHeight() {
276299
return drawingDelegate.getPreferredHeight();
277300
}
278301

302+
/** Returns the spring force of the spring animation for the progress. */
303+
@NonNull
304+
public SpringForce getSpringForce() {
305+
return springAnimation.getSpring();
306+
}
307+
308+
/** Sets the spring force of the spring animation for the progress. */
309+
public void setSpringForce(@NonNull SpringForce spring) {
310+
springAnimation.setSpring(spring);
311+
}
312+
279313
/**
280314
* Sets the drawable level with a fraction [0,1] of the progress. Note: this function is not used
281315
* to force updating the level in opposite to the automatic level updates by framework {@link

0 commit comments

Comments
 (0)