3030import androidx .annotation .CallSuper ;
3131import androidx .annotation .NonNull ;
3232import androidx .annotation .Nullable ;
33+ import androidx .core .graphics .ColorUtils ;
3334import androidx .core .util .Preconditions ;
3435import com .google .android .material .animation .AnimatorSetCompat ;
3536import com .google .android .material .animation .MotionSpec ;
@@ -157,33 +158,13 @@ AnimatorSet createAnimator(@NonNull MotionSpec spec) {
157158
158159 @ Override
159160 public Float get (ExtendedFloatingActionButton object ) {
160- // The alpha of the currently drawn text
161- int originalOpacity =
162- Color .alpha (
163- object .originalTextCsl .getColorForState (
164- object .getDrawableState (), fab .originalTextCsl .getDefaultColor ()));
165- final float currentOpacity = Color .alpha (object .getCurrentTextColor ()) / 255F ;
166- return lerp (0F , 1F , currentOpacity / originalOpacity );
161+ final int originalAlpha = Color .alpha (object .getCurrentOriginalTextColor ());
162+ final int currentAlpha = Color .alpha (object .getCurrentTextColor ());
163+ return originalAlpha != 0 ? (float ) currentAlpha / originalAlpha : 0f ;
167164 }
168165
169166 @ Override
170167 public void set (ExtendedFloatingActionButton object , Float value ) {
171- // Since `value` is always between 0 (gone) and 1 (visible), interpolate between
172- // 0 (gone) and the color's original alpha to avoid overshooting the text alpha.
173- int originalColor =
174- object .originalTextCsl .getColorForState (
175- object .getDrawableState (), fab .originalTextCsl .getDefaultColor ());
176-
177- final float interpolatedValue =
178- lerp (0F , Color .alpha (originalColor ) / 255F , value );
179- int alphaColor =
180- Color .argb (
181- (int ) (interpolatedValue * 255 ),
182- Color .red (originalColor ),
183- Color .green (originalColor ),
184- Color .blue (originalColor ));
185- ColorStateList csl = ColorStateList .valueOf (alphaColor );
186-
187168 // Setting the text color back to the original CSL in an onAnimationEnd callback
188169 // causes the view to blink after the animation ends. To avoid this, reset the
189170 // text color on the last frame of this animation instead.
@@ -193,8 +174,19 @@ public void set(ExtendedFloatingActionButton object, Float value) {
193174 // would jump in and jank the animation, but would conserve the user's updated
194175 // color.
195176 if (value == 1F ) { // last frame and visible
196- object .silentlyUpdateTextColor (object .originalTextCsl );
177+ object .silentlyUpdateTextColor (object .getOriginalTextColor () );
197178 } else {
179+ final int originalColor = object .getCurrentOriginalTextColor ();
180+
181+ // Since `value` is always between 0 (gone) and 1 (visible), interpolate
182+ // between 0 (gone) and the color's original alpha to avoid overshooting
183+ // the text alpha.
184+ final int targetAlpha =
185+ Math .round (lerp (0f , Color .alpha (originalColor ), value ));
186+ final int targetColor =
187+ ColorUtils .setAlphaComponent (originalColor , targetAlpha );
188+
189+ final ColorStateList csl = ColorStateList .valueOf (targetColor );
198190 object .silentlyUpdateTextColor (csl );
199191 }
200192 }
0 commit comments