前言
实现一种菜单,菜单从顶部弹入,然后从底部消失,顶部弹入时,有一个上下抖动的过程,底部消失时,先向上滑动,然后再向下滑动消失。
效果图如下:
引入依赖
?
|
1
|
implementation 'com.android.support:support-dynamic-animation:27.1.1'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 实现菜单从顶部弹出从底部消失动画效果,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对快网idc网站的支持!
原文链接:https://blog.csdn.net/qin_shi/article/details/80438448
相关文章
猜你喜欢
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-29 21
-
2025-05-29 55
-
2025-05-29 80
-
2025-05-27 55
-
2025-05-25 71
热门评论



