30
30
import androidx .annotation .CallSuper ;
31
31
import androidx .annotation .NonNull ;
32
32
import androidx .annotation .Nullable ;
33
+ import androidx .core .graphics .ColorUtils ;
33
34
import androidx .core .util .Preconditions ;
34
35
import com .google .android .material .animation .AnimatorSetCompat ;
35
36
import com .google .android .material .animation .MotionSpec ;
@@ -157,33 +158,13 @@ AnimatorSet createAnimator(@NonNull MotionSpec spec) {
157
158
158
159
@ Override
159
160
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 ;
167
164
}
168
165
169
166
@ Override
170
167
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
-
187
168
// Setting the text color back to the original CSL in an onAnimationEnd callback
188
169
// causes the view to blink after the animation ends. To avoid this, reset the
189
170
// text color on the last frame of this animation instead.
@@ -193,8 +174,19 @@ public void set(ExtendedFloatingActionButton object, Float value) {
193
174
// would jump in and jank the animation, but would conserve the user's updated
194
175
// color.
195
176
if (value == 1F ) { // last frame and visible
196
- object .silentlyUpdateTextColor (object .originalTextCsl );
177
+ object .silentlyUpdateTextColor (object .getOriginalTextColor () );
197
178
} 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 );
198
190
object .silentlyUpdateTextColor (csl );
199
191
}
200
192
}
0 commit comments