前言
實現一種菜單,菜單從頂部彈入,然后從底部消失,頂部彈入時,有一個上下抖動的過程,底部消失時,先向上滑動,然后再向下滑動消失。
效果圖如下:
引入依賴
1
|
|
創建springanimation需要三個參數。
•做動畫的view
•做動畫的類型(dynamicanimation)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
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,
1
|
springanimation signupbtnanimy = new springanimation(constraintlayout, dynamicanimation.rotation, 0 ); |
效果圖如下:
- 創建動畫的最終位置
相對view的當前位置的偏移量。
springforce
為了讓動畫流暢,有彈簧的性質,需要設置springforce的相關參數。
- stiffness
即剛度,此值越大,產生的里越大,動畫中彈性效果越不明顯,運動比較快。
1
2
3
4
|
stiffness_high stiffness_low stiffness_medium stiffness_very_low |
設置方法為:
1
|
signupbtnanimy.getspring().setstiffness(springforce.stiffness_low); |
•dampingratio阻尼比
即阻尼比,此值越大,彈簧效果停止的越快
1
2
3
4
|
damping_ratio_high_bouncy damping_ratio_low_bouncy damping_ratio_medium_bouncy damping_ratio_no_bouncy |
設置方法為:
1
|
signupbtnanimy.getspring().setdampingratio(springforce.damping_ratio_medium_bouncy); |
startvelocity
啟動速度,默認速度為0,單位是px/second.
整體代碼如下:
•顯示菜單動畫
1
2
3
4
5
6
7
8
|
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(); } |
•隱藏菜單動畫
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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
總結
以上所述是小編給大家介紹的springanimation 實現菜單從頂部彈出從底部消失動畫效果,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:https://blog.csdn.net/qin_shi/article/details/80438448