Skip to content

Instantly share code, notes, and snippets.

@easycheese
Last active August 29, 2015 14:21
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save easycheese/1994e4e833846c67b95a to your computer and use it in GitHub Desktop.
Save easycheese/1994e4e833846c67b95a to your computer and use it in GitHub Desktop.
FloatingActionButton Animator
package com.companionfree.tracker.bjj.util;
import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import com.getbase.floatingactionbutton.FloatingActionButton;
/**
* Simple Animator for Futuresimple's FloatingActionButton listed here: https://github.com/futuresimple/android-floating-action-button
*/
public class FABAnimator {
private static final int TRANSLATE_DURATION_MILLIS = 400;
private boolean fabTransitioning;
private boolean fabVisible;
private FloatingActionButton fab;
private RecyclerView recyclerView;
public void attachToRecyclerView(final FloatingActionButton fab, RecyclerView recyclerView) {
this.fab = fab;
fabVisible = true;
fabTransitioning = false;
this.recyclerView = recyclerView;
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (!fabTransitioning) {
if (dy > 20) {
fabTransitioning = true;
hide(true);
} else if (dy < -20) {
fabTransitioning = true;
show(true);
}
}
}
});
}
public void removeAllListeners() {
recyclerView.clearOnScrollListeners();
}
public void hide(boolean animate) {
toggle(false, animate, false);
}
public void show(boolean animate) {
toggle(true, animate, false);
}
private void toggle(final boolean visible, final boolean animate, boolean force) {
if (fabVisible != visible || force) {
fabVisible = visible;
int height = fab.getHeight();
int translationY = visible ? 0 : height + getMarginBottom();
ObjectAnimator fabAnim = ObjectAnimator.ofFloat(fab, "translationY", translationY);
fabAnim.setDuration(TRANSLATE_DURATION_MILLIS);
if (animate) {
fabAnim.setInterpolator(new DecelerateInterpolator());
} else {
fabAnim.setInterpolator(new AccelerateInterpolator());
}
fabAnim.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
fabTransitioning = false;
}
@Override
public void onAnimationCancel(Animator animation) {
fabTransitioning = false;
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
fabAnim.start();
}
}
private int getMarginBottom() {
int marginBottom = 0;
final ViewGroup.LayoutParams layoutParams = fab.getLayoutParams();
if (layoutParams instanceof ViewGroup.MarginLayoutParams) {
marginBottom = ((ViewGroup.MarginLayoutParams) layoutParams).bottomMargin;
}
return marginBottom;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment