Last active
August 29, 2015 14:21
-
-
Save easycheese/1994e4e833846c67b95a to your computer and use it in GitHub Desktop.
FloatingActionButton Animator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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