When doing the project want to add a bit of dynamic, so there is the following effect:
Implementation is also very simple, through the property animation and Recyclerview sliding combination is very good implementation.
By adding a swipe listener to the Recycleview: handles the animation by scrolling the difference value
Mrecyclerview.addonscrolllistener (NewRecyclerview.onscrolllistener () {@Override Public voidOnscrollstatechanged (Recyclerview Recyclerview,intnewstate) {} @Override Public voidOnscrolled (Recyclerview Recyclerview,intDxintdy) { if(Dy >0) {Hidenfabanim (); }Else{Showfabanim (); } } });
The two animations are as follows:
/** * Animated Hide Floating button*/ Private voidHidenfabanim () {if(!isfabanimg && Mactionbutton! =NULL&& mactionbutton.getvisibility () = =view.visible) {Animator Animator= Objectanimator.offloat (Mactionbutton,"Translationy", 0f, 100f); Animator.setduration ( -); Animator.addlistener (NewAnimator.animatorlistener () {@Override Public voidOnanimationstart (Animator animation) {isfabanimg=true; } @Override Public voidonanimationend (Animator animation) {isfabanimg=false; Mactionbutton.setvisibility (View.gone); } @Override Public voidOnanimationcancel (Animator animation) {isfabanimg=false; } @Override Public voidonanimationrepeat (Animator animation) {}}); Animator.start (); } } /** * Animated display floating button*/ Private voidShowfabanim () {if(Mactionbutton! =NULL&&!isfabanimg && mactionbutton.getvisibility () = =view.gone) {Animator Animator= Objectanimator.offloat (Mactionbutton,"Translationy", 100f,0f); Animator.setduration ( -); Animator.addlistener (NewAnimator.animatorlistener () {@Override Public voidOnanimationstart (Animator animation) {isfabanimg=true; } @Override Public voidonanimationend (Animator animation) {isfabanimg=false; } @Override Public voidOnanimationcancel (Animator animation) {isfabanimg=false; } @Override Public voidonanimationrepeat (Animator animation) {}}); Mactionbutton.setvisibility (view.visible); Animator.start (); } }
The distance given in the animation to move out of the screen and display on the screen is 100, the value is not so deterministic, the item needs to be calculated by the size of the screen density, in order to adapt to better sliding effect, define a
The isfabanimg variable, used to clarify the state of the animation, avoids repeated display or hidden animations.
Visible in Android to achieve some of the small effect is very convenient
The scaling effect of the Android floating button