Skip to content

Commit 6b41a60

Browse files
pubiqqhunterstich
authored andcommitted
[FloatingActionButton] Fix labelOpacity property
Resolves #4594 - f3ae9c5 by pubiqq <serj.ese@gmail.com> PiperOrigin-RevId: 772501116
1 parent bbd942c commit 6b41a60

File tree

2 files changed

+26
-24
lines changed

2 files changed

+26
-24
lines changed

lib/java/com/google/android/material/floatingactionbutton/BaseMotionStrategy.java

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import androidx.annotation.CallSuper;
3131
import androidx.annotation.NonNull;
3232
import androidx.annotation.Nullable;
33+
import androidx.core.graphics.ColorUtils;
3334
import androidx.core.util.Preconditions;
3435
import com.google.android.material.animation.AnimatorSetCompat;
3536
import 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
}

lib/java/com/google/android/material/floatingactionbutton/ExtendedFloatingActionButton.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import android.view.ViewGroup.LayoutParams;
4444
import android.view.ViewGroup.MarginLayoutParams;
4545
import androidx.annotation.AnimatorRes;
46+
import androidx.annotation.ColorInt;
4647
import androidx.annotation.IntDef;
4748
import androidx.annotation.NonNull;
4849
import androidx.annotation.Nullable;
@@ -461,6 +462,15 @@ private void saveOriginalTextCsl() {
461462
originalTextCsl = getTextColors();
462463
}
463464

465+
ColorStateList getOriginalTextColor() {
466+
return originalTextCsl;
467+
}
468+
469+
@ColorInt
470+
int getCurrentOriginalTextColor() {
471+
return originalTextCsl.getColorForState(getDrawableState(), 0);
472+
}
473+
464474
/**
465475
* Update the text color without affecting the original, client-set color.
466476
*/

0 commit comments

Comments
 (0)