ImportAndroid.content.Context;ImportAndroid.os.Build;Importandroid.support.design.widget.CoordinatorLayout;ImportAndroid.support.design.widget.FloatingActionButton;ImportAndroid.support.v4.view.ViewCompat;ImportAndroid.support.v4.view.ViewPropertyAnimatorListener;ImportAndroid.support.v4.view.animation.FastOutSlowInInterpolator;ImportAndroid.util.AttributeSet;ImportAndroid.view.View;Importandroid.view.animation.Animation;Importandroid.view.animation.AnimationUtils;ImportAndroid.view.animation.Interpolator;/*** Created by Bruce Too * in 6/2/15. * at 09:14 * Only required in Layout * app:layout_behavior= "com.support.android.designlibd Emo. Scrollawarefabbehavior "Use on OK*/ Public classScrollawarefabbehaviorextendsFloatingactionbutton.behavior {Private Static FinalInterpolator interpolator =NewFastoutslowininterpolator (); Private BooleanMisanimatingout =false; PublicScrollawarefabbehavior (Context context, AttributeSet attrs) {Super(); } /*** Scrolling start of listening *@paramCoordinatorlayout *@paramChild Set Behavior View *@paramDirecttargetchild *@paramtarget needs to scroll the Sub view *@paramnestedscrollaxes Scroll axis direction scroll_axis_horizontal, scroll_axis_vertical *@return */@Override Public BooleanOnstartnestedscroll (FinalCoordinatorlayout Coordinatorlayout,FinalFloatingactionbutton Child,FinalView Directtargetchild,FinalView Target,Final intnestedscrollaxes) { //ensure we react to vertical scrolling returnNestedscrollaxes = =viewcompat.scroll_axis_vertical||Super. Onstartnestedscroll (Coordinatorlayout, Child, Directtargetchild, Target, nestedscrollaxes); } /*** monitoring when scrolling * each of the behavior is set and the direct child view of Coordinatorlayout calls this method * such as Floatactionbutton,appbarlayout * @paramCoordinatorlayout The Coordinatorlayout parent of the view this Behavior are associated with *@paramChild the child view of the coordinatorlayout this Behavior are associated with *@paramtarget the descendant view of the coordinatorlayout performing the nested scroll *@paramdxconsumed horizontal pixels consumed by the target ' s own scrolling operation *@paramdyconsumed vertical pixels consumed by the target ' s own scrolling operation *@paramdxunconsumed horizontal pixels not consumed by the target's own scrolling operation, but requested by the user */@Override Public voidOnnestedscroll (FinalCoordinatorlayout Coordinatorlayout,FinalFloatingactionbutton Child,FinalView Target,Final intDxconsumed,Final intdyconsumed,Final intDxunconsumed,Final intdyunconsumed) { Super. Onnestedscroll (coordinatorlayout, child, Target, dxconsumed, dyconsumed, dxunconsumed, dyunconsumed); if(dyconsumed > 0 &&!) This. Misanimatingout && child.getvisibility () = =view.visible) {//User scrolled down and the Fab is currently visible and hide the Fabanimateout (child); } Else if(dyconsumed < 0 && child.getvisibility ()! =view.visible) {//User scrolled up and the Fab are currently not visible-show the FabAnimatein (child); } /*** If >=v22.2.1 ' * can use the System preset animation*///if (dyconsumed > 0 && child.getvisibility () = = view.visible) {// //User scrolled down and the Fab is currently visible and hide the Fab//child.hide ();//} else if (dyconsumed < 0 && child.getvisibility ()! = view.visible) {// //User scrolled up and the Fab are currently not visible-show the Fab//child.show ();// } } //same animation that Floatingactionbutton.behavior uses to hide the FAB when the appbarlayout exits Private voidAnimateout (FinalFloatingactionbutton button) { if(Build.VERSION.SDK_INT >= 14) {viewcompat.animate (button). ScaleX (0.0F). ScaleY (0.0F). Alpha (0.0F). Setinterpolator (Interpolator). Withlayer (). Setlistener (NewViewpropertyanimatorlistener () { Public voidOnanimationstart (view view) {Scrollawarefabbehavior. This. Misanimatingout =true; } Public voidonanimationcancel (view view) {Scrollawarefabbehavior. This. Misanimatingout =false; } Public voidonanimationend (view view) {Scrollawarefabbehavior. This. Misanimatingout =false; View.setvisibility (View.gone); }}). Start (); } Else{Animation Anim=animationutils.loadanimation (Button.getcontext (), r.anim.fab_out); Anim.setinterpolator (Interpolator); Anim.setduration (200L); Anim.setanimationlistener (NewAnimation.animationlistener () { Public voidOnanimationstart (Animation Animation) {scrollawarefabbehavior. This. Misanimatingout =true; } Public voidonanimationend (Animation Animation) {scrollawarefabbehavior. This. Misanimatingout =false; Button.setvisibility (View.gone); } @Override Public voidOnanimationrepeat (FinalAnimation Animation) { } }); Button.startanimation (ANIM); } } //same animation that Floatingactionbutton.behavior uses to show the FAB when the Appbarlayout enters Private voidAnimatein (Floatingactionbutton button) {button.setvisibility (view.visible); if(Build.VERSION.SDK_INT >= 14) {viewcompat.animate (button). ScaleX (1.0F). ScaleY (1.0F). Alpha (1.0F). Setinterpolator (Interpolator). Withlayer (). Setlistener (NULL). Start (); } Else{Animation Anim=animationutils.loadanimation (Button.getcontext (), r.anim.fab_in); Anim.setduration (200L); Anim.setinterpolator (Interpolator); Button.startanimation (ANIM); } }}
Fab uses hidden animations in coordinatorlayout