Skip to content

Commit 4c74b80

Browse files
pekingmedrchen
authored andcommitted
[ProgressIndicator] Added an API to automatically hide the indicator after it reaches max progress.
PiperOrigin-RevId: 800169008
1 parent 86eddaa commit 4c74b80

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

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

21+
import static com.google.android.material.progressindicator.DeterminateDrawable.MAX_DRAWABLE_LEVEL;
2122
import static com.google.android.material.theme.overlay.MaterialThemeOverlay.wrap;
2223
import static java.lang.Math.abs;
2324
import static java.lang.Math.min;
@@ -43,6 +44,7 @@
4344
import androidx.annotation.RestrictTo.Scope;
4445
import androidx.annotation.StyleRes;
4546
import androidx.annotation.VisibleForTesting;
47+
import androidx.dynamicanimation.animation.DynamicAnimation.OnAnimationEndListener;
4648
import androidx.vectordrawable.graphics.drawable.Animatable2Compat.AnimationCallback;
4749
import com.google.android.material.color.MaterialColors;
4850
import com.google.android.material.internal.ThemeEnforcement;
@@ -960,6 +962,25 @@ public void setVisibilityAfterHide(int visibility) {
960962
visibilityAfterHide = visibility;
961963
}
962964

965+
private final OnAnimationEndListener hideAfterMaxProgressListener =
966+
(animation, canceled, value, velocity) -> {
967+
if (getProgressDrawable() != null
968+
&& getProgressDrawable().getLevel() == MAX_DRAWABLE_LEVEL) {
969+
hide();
970+
}
971+
};
972+
973+
public void setHideAfterMaxProgress(boolean hideAfterMaxProgress) {
974+
if (getProgressDrawable() == null) {
975+
return;
976+
}
977+
if (hideAfterMaxProgress) {
978+
getProgressDrawable().addSpringAnimationEndListener(hideAfterMaxProgressListener);
979+
} else {
980+
getProgressDrawable().removeSpringAnimationEndListener(hideAfterMaxProgressListener);
981+
}
982+
}
983+
963984
/**
964985
* Sets the scale of the animation duration in indeterminate mode.
965986
*

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
public final class DeterminateDrawable<S extends BaseProgressIndicatorSpec>
4141
extends DrawableWithAnimatedVisibilityChange {
4242
// Constants for drawing progress.
43-
private static final int MAX_DRAWABLE_LEVEL = 10000;
43+
static final int MAX_DRAWABLE_LEVEL = 10000;
4444
// Constants for amplitude animation.
4545
static final float FULL_AMPLITUDE_PROGRESS_MIN = 0.1f;
4646
static final float FULL_AMPLITUDE_PROGRESS_MAX = 0.9f;

0 commit comments

Comments
 (0)