Instantly share code, notes, and snippets.
Created
June 26, 2016 10:37
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save UmarBhutta/a6d18660077fb0fe1a0cdc44d3d31a80 to your computer and use it in GitHub Desktop.
Behaviour for Floating Action Menu
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
/** | |
* Created by malikumarbhutta on 2/10/16. | |
*/ | |
public class FloatingActionMenuBehaviour extends CoordinatorLayout.Behavior<FloatingActionsMenu> { | |
private static final Interpolator INTERPOLATOR = new FastOutSlowInInterpolator(); | |
private boolean mIsAnimatingOut = false; | |
public FloatingActionMenuBehaviour(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
@Override | |
public boolean layoutDependsOn(CoordinatorLayout parent, FloatingActionsMenu child, View dependency) { | |
return dependency instanceof FloatingActionsMenu; | |
} | |
@Override | |
public boolean onDependentViewChanged(CoordinatorLayout parent, FloatingActionsMenu child, View dependency) { | |
if(dependency instanceof FloatingActionsMenu){ | |
return super.onDependentViewChanged(parent,child,dependency); | |
}else if(dependency instanceof AppBarLayout){ | |
this.updateFabVisibility(parent,(AppBarLayout)dependency,child); | |
} | |
return false; | |
} | |
private boolean updateFabVisibility(CoordinatorLayout parent, AppBarLayout appBarLayout, FloatingActionsMenu child) { | |
CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams)child.getLayoutParams(); | |
if(lp.getAnchorId() != appBarLayout.getId()) { | |
return false; | |
} else { | |
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) child.getLayoutParams(); | |
int point = child.getTop() - params.topMargin; | |
try { | |
Method method = AppBarLayout.class.getDeclaredMethod("getMinimumHeightForVisibleOverlappingContent"); | |
method.setAccessible(true); | |
if(point <= (int) method.invoke(appBarLayout)){ | |
child.setAlpha(0.0F); | |
//child.setVisibility(View.GONE); | |
}else { | |
child.setAlpha(1.0F); | |
// child.setVisibility(View.VISIBLE); | |
} | |
// if(point <= (int) method.invoke(appBarLayout) && !this.mIsAnimatingOut && child.getVisibility() == View.VISIBLE) { | |
// animateOut(child); | |
// } else if (!this.mIsAnimatingOut && child.getVisibility() != View.VISIBLE){ | |
// animateIn(child); | |
// } | |
return true; | |
} catch (Exception e) { | |
return true; | |
} | |
} | |
} | |
private void animateOut(final FloatingActionsMenu button) { | |
if (Build.VERSION.SDK_INT >= 14) { | |
ViewCompat.animate(button).alpha(0.0F).setInterpolator(INTERPOLATOR).withLayer() | |
.setListener(new ViewPropertyAnimatorListener() { | |
public void onAnimationStart(View view) { | |
MenuBehaviour.this.mIsAnimatingOut = true; | |
} | |
public void onAnimationCancel(View view) { | |
MenuBehaviour.this.mIsAnimatingOut = true; | |
} | |
public void onAnimationEnd(View view) { | |
MenuBehaviour.this.mIsAnimatingOut = false; | |
view.setVisibility(View.GONE); | |
} | |
}).start(); | |
} else { | |
Animation anim = AnimationUtils.loadAnimation(button.getContext(), android.support.design.R.anim.design_fab_out); | |
anim.setInterpolator(INTERPOLATOR); | |
anim.setDuration(200L); | |
anim.setAnimationListener(new Animation.AnimationListener() { | |
public void onAnimationStart(Animation animation) { | |
MenuBehaviour.this.mIsAnimatingOut = true; | |
} | |
public void onAnimationEnd(Animation animation) { | |
MenuBehaviour.this.mIsAnimatingOut = false; | |
button.setVisibility(View.GONE); | |
} | |
@Override | |
public void onAnimationRepeat(final Animation animation) { | |
} | |
}); | |
button.startAnimation(anim); | |
} | |
} | |
// Same animation that FloatingActionButton.Behavior uses to show the FAB when the AppBarLayout enters | |
private void animateIn(FloatingActionsMenu button) { | |
button.setVisibility(View.VISIBLE); | |
if (Build.VERSION.SDK_INT >= 14) { | |
ViewCompat.animate(button).alpha(1.0F) | |
.setInterpolator(INTERPOLATOR).withLayer().setListener(null) | |
.start(); | |
} else { | |
Animation anim = AnimationUtils.loadAnimation(button.getContext(),android.support.design.R.anim.design_fab_in); | |
anim.setDuration(200L); | |
anim.setInterpolator(INTERPOLATOR); | |
button.startAnimation(anim); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment