Skip to content

Commit 1886cf0

Browse files
pekingmedsn5ft
authored andcommitted
[LoadingIndicator] Fixed the animation no constant rotation.
PiperOrigin-RevId: 780327353 (cherry picked from commit f300ba0)
1 parent 61c5a6d commit 1886cf0

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,21 @@ public void onAnimationRepeat(Animator animation) {
121121

122122
/** Updates the indicator's rotation based on current playtime. */
123123
private void updateIndicatorRotation(int playtime) {
124+
float morphFactorBase = morphFactorTarget - 1;
125+
float morphFactorPerShape = morphFactor - morphFactorBase;
126+
float timeFactorPerShape = (float) playtime / DURATION_PER_SHAPE_IN_MS;
127+
if (timeFactorPerShape == 1f) {
128+
// The animation on repeat is called before the playtime restart. So if playtime reaches the
129+
// end, we take it as restarted as 0.
130+
timeFactorPerShape = 0f;
131+
}
124132
// Initial rotation.
125-
indicatorState.rotationDegree = CONSTANT_ROTATION_PER_SHAPE_DEGREES * (morphFactorTarget - 1);
126-
// Constant rotation for the current shape.
127-
indicatorState.rotationDegree +=
128-
CONSTANT_ROTATION_PER_SHAPE_DEGREES * ((float) playtime / DURATION_PER_SHAPE_IN_MS);
129-
// Rotation driven by spring animation.
130133
indicatorState.rotationDegree =
131-
(CONSTANT_ROTATION_PER_SHAPE_DEGREES + EXTRA_ROTATION_PER_SHAPE_DEGREES) * morphFactor;
134+
(CONSTANT_ROTATION_PER_SHAPE_DEGREES + EXTRA_ROTATION_PER_SHAPE_DEGREES) * morphFactorBase;
135+
// Constant rotation.
136+
indicatorState.rotationDegree += CONSTANT_ROTATION_PER_SHAPE_DEGREES * timeFactorPerShape;
137+
// Rotation driven by spring animation.
138+
indicatorState.rotationDegree += EXTRA_ROTATION_PER_SHAPE_DEGREES * morphFactorPerShape;
132139

133140
indicatorState.rotationDegree %= 360;
134141
}

0 commit comments

Comments
 (0)