SpringAnimation 實現(xiàn)菜單從頂部彈出從底部消失動畫效果
前言
實現(xiàn)一種菜單,菜單從頂部彈入,然后從底部消失,頂部彈入時,有一個上下抖動的過程,底部消失時,先向上滑動,然后再向下滑動消失。
效果圖如下:
引入依賴
implementation 'com.android.support:support-dynamic-animation:27.1.1'1
創(chuàng)建SpringAnimation需要三個參數(shù)。
•做動畫的View
•做動畫的類型(DynamicAnimation)
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的預覽圖,現(xiàn)在我們把參數(shù)設(shè)置為ROTATION,
SpringAnimation signUpBtnAnimY = new SpringAnimation(constraintLayout, DynamicAnimation.ROTATION, 0);
效果圖如下:
- 創(chuàng)建動畫的最終位置
相對View的當前位置的偏移量。
SpringForce
為了讓動畫流暢,有彈簧的性質(zhì),需要設(shè)置SpringForce的相關(guān)參數(shù)。
- Stiffness
即剛度,此值越大,產(chǎn)生的里越大,動畫中彈性效果越不明顯,運動比較快。
STIFFNESS_HIGH STIFFNESS_LOW STIFFNESS_MEDIUM STIFFNESS_VERY_LOW
設(shè)置方法為:
signUpBtnAnimY.getSpring().setStiffness(SpringForce.STIFFNESS_LOW);
•DampingRatio阻尼比
即阻尼比,此值越大,彈簧效果停止的越快
DAMPING_RATIO_HIGH_BOUNCY DAMPING_RATIO_LOW_BOUNCY DAMPING_RATIO_MEDIUM_BOUNCY DAMPING_RATIO_NO_BOUNCY
設(shè)置方法為:
signUpBtnAnimY.getSpring().setDampingRatio(SpringForce.DAMPING_RATIO_MEDIUM_BOUNCY);
StartVelocity
啟動速度,默認速度為0,單位是px/second.
整體代碼如下:
•顯示菜單動畫
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/StudySpringAnimation
總結(jié)
以上所述是小編給大家介紹的SpringAnimation 實現(xiàn)菜單從頂部彈出從底部消失動畫效果,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Spring?Cloud?OAuth2實現(xiàn)自定義token返回格式
Spring?Security?OAuth的token返回格式都是默認的,但是往往這個格式是不適配系統(tǒng)。本文將用一個接口優(yōu)雅的實現(xiàn)?Spring?Cloud?OAuth2?自定義token返回格式,需要的可以參考一下2022-06-06Spring Cloud之服務(wù)監(jiān)控turbine的示例
這篇文章主要介紹了Spring Cloud之服務(wù)監(jiān)控turbine的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05

SpringSecurity登錄使用JSON格式數(shù)據(jù)的方法

SpringBoot中@EnableAsync和@Async注解的使用小結(jié)

解決IDEA2020 創(chuàng)建maven項目沒有src/main/java目錄和webapp目錄問題