Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FloatingActionButton] Fix labelOpacity property #4594

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import androidx.annotation.CallSuper;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.graphics.ColorUtils;
import androidx.core.util.Preconditions;
import com.google.android.material.animation.AnimatorSetCompat;
import com.google.android.material.animation.MotionSpec;
Expand Down Expand Up @@ -157,33 +158,13 @@ AnimatorSet createAnimator(@NonNull MotionSpec spec) {

@Override
public Float get(ExtendedFloatingActionButton object) {
// The alpha of the currently drawn text
int originalOpacity =
Color.alpha(
object.originalTextCsl.getColorForState(
object.getDrawableState(), fab.originalTextCsl.getDefaultColor()));
final float currentOpacity = Color.alpha(object.getCurrentTextColor()) / 255F;
return lerp(0F, 1F, currentOpacity / originalOpacity);
final int originalAlpha = Color.alpha(object.getCurrentOriginalTextColor());
final int currentAlpha = Color.alpha(object.getCurrentTextColor());
return originalAlpha != 0 ? (float) currentAlpha / originalAlpha : 0f;
}

@Override
public void set(ExtendedFloatingActionButton object, Float value) {
// Since `value` is always between 0 (gone) and 1 (visible), interpolate between
// 0 (gone) and the color's original alpha to avoid overshooting the text alpha.
int originalColor =
object.originalTextCsl.getColorForState(
object.getDrawableState(), fab.originalTextCsl.getDefaultColor());

final float interpolatedValue =
lerp(0F, Color.alpha(originalColor) / 255F, value);
int alphaColor =
Color.argb(
(int) (interpolatedValue * 255),
Color.red(originalColor),
Color.green(originalColor),
Color.blue(originalColor));
ColorStateList csl = ColorStateList.valueOf(alphaColor);

// Setting the text color back to the original CSL in an onAnimationEnd callback
// causes the view to blink after the animation ends. To avoid this, reset the
// text color on the last frame of this animation instead.
Expand All @@ -193,8 +174,19 @@ public void set(ExtendedFloatingActionButton object, Float value) {
// would jump in and jank the animation, but would conserve the user's updated
// color.
if (value == 1F) { // last frame and visible
object.silentlyUpdateTextColor(object.originalTextCsl);
object.silentlyUpdateTextColor(object.getOriginalTextColor());
} else {
final int originalColor = object.getCurrentOriginalTextColor();

// Since `value` is always between 0 (gone) and 1 (visible), interpolate
// between 0 (gone) and the color's original alpha to avoid overshooting
// the text alpha.
final int targetAlpha =
Math.round(lerp(0f, Color.alpha(originalColor), value));
final int targetColor =
ColorUtils.setAlphaComponent(originalColor, targetAlpha);

final ColorStateList csl = ColorStateList.valueOf(targetColor);
object.silentlyUpdateTextColor(csl);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import android.view.ViewGroup.LayoutParams;
import android.view.ViewGroup.MarginLayoutParams;
import androidx.annotation.AnimatorRes;
import androidx.annotation.ColorInt;
import androidx.annotation.IntDef;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand Down Expand Up @@ -460,6 +461,15 @@ private void saveOriginalTextCsl() {
originalTextCsl = getTextColors();
}

ColorStateList getOriginalTextColor() {
return originalTextCsl;
}

@ColorInt
int getCurrentOriginalTextColor() {
return originalTextCsl.getColorForState(getDrawableState(), 0);
}

/**
* Update the text color without affecting the original, client-set color.
*/
Expand Down