Preface
Implement a menu where the menu pops in from the top and then disappears from the bottom. When the top pops in, there is a process of shaking up and down. When the bottom disappears, slide up first, then slide down and then disappear.
The renderings are as follows:
Introduce dependencies
implementation 'com.android.support:support-dynamic-animation:27.1.1'1
Creating SpringAnimation requires three parameters.
•Animated View
•DynamicAnimation type
ALPHAROTATIONROTATION_XROTATION_YSCALE_XSCALE_YSCROLL_XSCROLL_YTRANSLATION_XTRANSLATION_YTRANSLATION_ZXYZ
The gif diagram above is a preview diagram with DynamicAnimation TRANSLATION_Y. Now we set the parameter to ROTATION.
SpringAnimation signUpBtnAnimY = new SpringAnimation(constraintLayout, DynamicAnimation.ROTATION, 0);
The renderings are as follows:
- The final location for creating an animation
The offset relative to the current position of the View.
SpringForce
In order to make the animation smooth and springy, SpringForce related parameters need to be set.
- Stiffness
That is, stiffness. The larger this value, the larger the inner part produced, the less obvious the elastic effect in the animation, and the faster the movement.
STIFFNESS_HIGHSTIFFNESS_LOWSTIFFNESS_MEDIUMSTIFFNESS_VERY_LOW
The setting method is:
signUpBtnAnimY.getSpring().setStiffness(SpringForce.STIFFNESS_LOW);
•DampingRatio damping ratio
That is, the damping ratio, the larger this value, the faster the spring effect stops
DAMPING_RATIO_HIGH_BOUNCYDAMPING_RATIO_LOW_BOUNCYDAMPING_RATIO_MEDIUM_BOUNCYDAMPING_RATIO_NO_BOUNCY
The setting method is:
signUpBtnAnimY.getSpring().setDampingRatio(SpringForce.DAMPING_RATIO_MEDIUM_BOUNCY);
StartVelocity
Startup speed, the default speed is 0, and the unit is px/second.
The overall code is as follows:
•Show menu animation
public void showAnimal() { setVisibility(View.VISIBLE); SpringAnimation signUpBtnAnimY = new SpringAnimation(constraintLayout, DynamicAnimation.TRANSLATION_Y, 0); signUpBtnAnimY.getSpring().setStiffness(SpringForce.STIFFNESS_LOW); signUpBtnAnimY.getSpring().setDampingRatio(SpringForce.DAMPING_RATIO_MEDIUM_BOUNCY); signUpBtnAnimY.setStartVelocity(5000); signUpBtnAnimY.start(); }•Hide menu animation
public void hideAnimal() { height = (ScreenTools.getScreenHeight(getContext()) - constraintLayout.getHeight()) / 2 + constraintLayout.getHeight() + ScreenTools.dp2px(getContext(),50); ObjectAnimator animator = ObjectAnimator.ofFloat(constraintLayout, "translationY", 0f, -100f, height); animator.setDuration(600); animator.setInterpolator(new DecelerateInterpolator()); animator.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { super.onAnimationEnd(animation); setVisibility(GONE); reLayout(); } }); animator.start(); } Source code: https://github.com/LSnumber1/StudySpringAnimation
Summarize
The above is the SpringAnimation implementation menu pops up from the top and disappears from the bottom. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!