Skip to content

Commit 0a5f347

Browse files
pekingmehunterstich
authored andcommitted
[LoadingIndicator] Fixed unstoppable animators.
Resolves #4267 PiperOrigin-RevId: 697705431
1 parent dff307b commit 0a5f347

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

lib/java/com/google/android/material/loadingindicator/LoadingIndicator.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,14 @@ protected void onSizeChanged(int w, int h, int oldw, int oldh) {
130130
@Override
131131
protected void onVisibilityChanged(@NonNull View changedView, int visibility) {
132132
super.onVisibilityChanged(changedView, visibility);
133-
drawable.setVisible(visibleToUser(), /* restart= */ true);
133+
drawable.setVisible(
134+
visibleToUser(), /* restart= */ false, /* animate= */ visibility == VISIBLE);
134135
}
135136

136137
@Override
137138
protected void onWindowVisibilityChanged(int visibility) {
138139
super.onWindowVisibilityChanged(visibility);
139-
drawable.setVisible(visibleToUser(), /* restart= */ true);
140+
drawable.setVisible(visibleToUser(), /* restart= */ false, /* animate= */ false);
140141
}
141142

142143
@Override

lib/java/com/google/android/material/loadingindicator/LoadingIndicatorDrawable.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,26 @@ public void draw(@NonNull Canvas canvas) {
113113
@CanIgnoreReturnValue
114114
@Override
115115
public boolean setVisible(boolean visible, boolean restart) {
116+
return setVisible(visible, restart, /* animate= */ visible);
117+
}
118+
119+
/**
120+
* Changes the visibility with/without triggering the animation callbacks.
121+
*
122+
* @param visible Whether to make the drawable visible.
123+
* @param restart Whether to force starting the animation from the beginning.
124+
* @param animate Whether to change the visibility with animation.
125+
* @return {@code true}, if the visibility changes or will change after the animation; {@code
126+
* false}, otherwise.
127+
* @see #setVisible(boolean, boolean, boolean)
128+
*/
129+
@CanIgnoreReturnValue
130+
public boolean setVisible(boolean visible, boolean restart, boolean animate) {
116131
boolean changed = super.setVisible(visible, restart);
117-
if (visible && !isSystemAnimatorDisabled()) {
132+
animatorDelegate.cancelAnimatorImmediately();
133+
// Restarts the main animator if it's visible and needs to be animated.
134+
if (visible && animate && !isSystemAnimatorDisabled()) {
118135
animatorDelegate.startAnimator();
119-
} else {
120-
animatorDelegate.cancelAnimatorImmediately();
121136
}
122137
return changed;
123138
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,6 @@ public boolean setVisible(boolean visible, boolean restart) {
241241
* @param animate Whether to change the visibility with animation.
242242
* @return {@code true}, if the visibility changes or will change after the animation; {@code
243243
* false}, otherwise.
244-
* @see #setVisible(boolean, boolean, boolean)
245244
*/
246245
public boolean setVisible(boolean visible, boolean restart, boolean animate) {
247246
float systemAnimatorDurationScale =

0 commit comments

Comments
 (0)