實現一種菜單,菜單從頂部彈入,而後從底部消失,頂部彈入時,有一個上下抖動的過程,底部消失時,先向上滑動,而後再向下滑動消失。
效果圖以下:android
implementation 'com.android.support:support-dynamic-animation:27.1.1'
建立SpringAnimation須要三個參數。git
ALPHA ROTATION ROTATION_X ROTATION_Y SCALE_X SCALE_Y SCROLL_X SCROLL_Y TRANSLATION_X TRANSLATION_Y TRANSLATION_Z X Y Z
上邊的gif圖爲DynamicAnimation爲TRANSLATION_Y的預覽圖,如今咱們把參數設置爲ROTATION,github
SpringAnimation signUpBtnAnimY = new SpringAnimation(constraintLayout, DynamicAnimation.ROTATION, 0);
效果圖以下:
ide
相對View的當前位置的偏移量。動畫
爲了讓動畫流暢,有彈簧的性質,須要設置SpringForce的相關參數。spa
即剛度,此值越大,產生的裏越大,動畫中彈性效果越不明顯,運動比較快。code
STIFFNESS_HIGH STIFFNESS_LOW STIFFNESS_MEDIUM STIFFNESS_VERY_LOW
設置方法爲:
signUpBtnAnimY.getSpring().setStiffness(SpringForce.STIFFNESS_LOW);
即阻尼比,此值越大,彈簧效果中止的越快圖片
DAMPING_RATIO_HIGH_BOUNCY DAMPING_RATIO_LOW_BOUNCY DAMPING_RATIO_MEDIUM_BOUNCY DAMPING_RATIO_NO_BOUNCY
設置方法爲:
signUpBtnAnimY.getSpring().setDampingRatio(SpringForce.DAMPING_RATIO_MEDIUM_BOUNCY);
啓動速度,默認速度爲0,單位是px/second.ci
總體代碼以下:rem
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(); }
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(); }
源碼: https://github.com/LSnumber1/...