Skip to content

Commit

Permalink
[BottomAppBar] Updated FAB alignment mode slide animation to use new …
Browse files Browse the repository at this point in the history
…motion attributes.

PiperOrigin-RevId: 449287560
  • Loading branch information
hunterstich authored and afohrman committed May 18, 2022
1 parent 72bac61 commit 722756e
Showing 1 changed file with 34 additions and 17 deletions.
51 changes: 34 additions & 17 deletions lib/java/com/google/android/material/bottomappbar/BottomAppBar.java
Expand Up @@ -55,6 +55,7 @@
import androidx.core.view.ViewCompat.ScrollAxis;
import androidx.core.view.WindowInsetsCompat;
import androidx.customview.view.AbsSavedState;
import com.google.android.material.animation.AnimationUtils;
import com.google.android.material.animation.TransformationCallback;
import com.google.android.material.behavior.HideBottomViewOnScrollBehavior;
import com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton;
Expand All @@ -63,6 +64,7 @@
import com.google.android.material.internal.ThemeEnforcement;
import com.google.android.material.internal.ViewUtils;
import com.google.android.material.internal.ViewUtils.RelativePadding;
import com.google.android.material.motion.MotionUtils;
import com.google.android.material.resources.MaterialResources;
import com.google.android.material.shape.EdgeTreatment;
import com.google.android.material.shape.MaterialShapeDrawable;
Expand Down Expand Up @@ -112,7 +114,11 @@ public class BottomAppBar extends Toolbar implements AttachedBehavior {

private static final int DEF_STYLE_RES = R.style.Widget_MaterialComponents_BottomAppBar;

private static final long ANIMATION_DURATION = 300;
private static final int FAB_ALIGNMENT_ANIM_DURATION_DEFAULT = 300;
private static final int FAB_ALIGNMENT_ANIM_DURATION_ATTR = R.attr.motionDurationLong2;
private static final int FAB_ALIGNMENT_ANIM_EASING_ATTR =
R.attr.motionEasingEmphasizedInterpolator;
private static final float FAB_ALIGNMENT_ANIM_EASING_MIDPOINT = .2F;

public static final int FAB_ALIGNMENT_MODE_CENTER = 0;
public static final int FAB_ALIGNMENT_MODE_END = 1;
Expand Down Expand Up @@ -503,10 +509,9 @@ public void performHide() {
}

/**
* Hides the {@link BottomAppBar}.
* Hides the {@link BottomAppBar}.
*
* @param animate {@code false} to hide the {@link BottomAppBar} immediately
* without animation.
* @param animate {@code false} to hide the {@link BottomAppBar} immediately without animation.
*/
public void performHide(boolean animate) {
getBehavior().slideDown(this, animate);
Expand All @@ -520,8 +525,7 @@ public void performShow() {
/**
* Shows the {@link BottomAppBar}.
*
* @param animate {@code false} to show the {@link BottomAppBar} immediately without
* animation.
* @param animate {@code false} to show the {@link BottomAppBar} immediately without animation.
*/
public void performShow(boolean animate) {
getBehavior().slideUp(this, animate);
Expand Down Expand Up @@ -633,6 +637,9 @@ private void maybeAnimateModeChange(@FabAlignmentMode int targetMode) {

AnimatorSet set = new AnimatorSet();
set.playTogether(animators);
set.setInterpolator(
MotionUtils.resolveThemeInterpolator(
getContext(), FAB_ALIGNMENT_ANIM_EASING_ATTR, AnimationUtils.LINEAR_INTERPOLATOR));
modeAnimator = set;
modeAnimator.addListener(
new AnimatorListenerAdapter() {
Expand Down Expand Up @@ -715,10 +722,15 @@ private void createFabTranslationXAnimation(
@FabAlignmentMode int targetMode, @NonNull List<Animator> animators) {
ObjectAnimator animator =
ObjectAnimator.ofFloat(findDependentFab(), "translationX", getFabTranslationX(targetMode));
animator.setDuration(ANIMATION_DURATION);
animator.setDuration(getFabAlignmentAnimationDuration());
animators.add(animator);
}

private int getFabAlignmentAnimationDuration() {
return MotionUtils.resolveThemeDuration(
getContext(), FAB_ALIGNMENT_ANIM_DURATION_ATTR, FAB_ALIGNMENT_ANIM_DURATION_DEFAULT);
}

@Nullable
private Drawable maybeTintNavigationIcon(@Nullable Drawable navigationIcon) {
if (navigationIcon != null && navigationIconTint != null) {
Expand Down Expand Up @@ -785,7 +797,9 @@ private void createMenuViewTranslationAnimation(
return;
}

final float animationDuration = getFabAlignmentAnimationDuration();
Animator fadeIn = ObjectAnimator.ofFloat(actionMenuView, "alpha", 1);
fadeIn.setDuration((long) (animationDuration * (1F - FAB_ALIGNMENT_ANIM_EASING_MIDPOINT)));

float translationXDifference =
actionMenuView.getTranslationX()
Expand All @@ -795,6 +809,7 @@ private void createMenuViewTranslationAnimation(
if (Math.abs(translationXDifference) > 1) {
// We need to fade the MenuView out and in because it's position is changing
Animator fadeOut = ObjectAnimator.ofFloat(actionMenuView, "alpha", 0);
fadeOut.setDuration((long) (animationDuration * FAB_ALIGNMENT_ANIM_EASING_MIDPOINT));

fadeOut.addListener(
new AnimatorListenerAdapter() {
Expand All @@ -816,7 +831,6 @@ public void onAnimationEnd(Animator animation) {
});

AnimatorSet set = new AnimatorSet();
set.setDuration(ANIMATION_DURATION / 2);
set.playSequentially(fadeOut, fadeIn);
animators.add(set);
} else if (actionMenuView.getAlpha() < 1) {
Expand Down Expand Up @@ -879,13 +893,14 @@ private void translateActionMenuView(
@FabAlignmentMode final int fabAlignmentMode,
final boolean fabAttached,
boolean shouldWaitForMenuReplacement) {
Runnable runnable = new Runnable() {
@Override
public void run() {
actionMenuView.setTranslationX(
getActionMenuViewTranslationX(actionMenuView, fabAlignmentMode, fabAttached));
}
};
Runnable runnable =
new Runnable() {
@Override
public void run() {
actionMenuView.setTranslationX(
getActionMenuViewTranslationX(actionMenuView, fabAlignmentMode, fabAttached));
}
};
if (shouldWaitForMenuReplacement) {
// Wait to ensure the actionMenuView has had it's menu inflated and is able to correctly
// measure it's width before calculating and translating X.
Expand Down Expand Up @@ -1103,8 +1118,10 @@ public void onLayoutChange(
child.setFabDiameter(height);

// Assume symmetrical corners
float cornerSize = fab.getShapeAppearanceModel().getTopLeftCornerSize()
.getCornerSize(new RectF(fabContentRect));
float cornerSize =
fab.getShapeAppearanceModel()
.getTopLeftCornerSize()
.getCornerSize(new RectF(fabContentRect));

child.setFabCornerSize(cornerSize);

Expand Down

0 comments on commit 722756e

Please sign in to comment.