Fab uses hidden animations in coordinatorlayout

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.