SpringAnimation 实现菜单从顶部弹出从底部消失动画效果

2025-05-29 0 55

前言

实现一种菜单菜单从顶部弹入,然后从底部消失,顶部弹入时,有一个上下抖动的过程,底部消失时,先向上滑动,然后再向下滑动消失。

效果图如下:

SpringAnimation 实现菜单从顶部弹出从底部消失动画效果

引入依赖

?

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);

效果图如下:

SpringAnimation 实现菜单从顶部弹出从底部消失动画效果

– 创建动画的最终位置

相对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

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 SpringAnimation 实现菜单从顶部弹出从底部消失动画效果 https://www.kuaiidc.com/111713.html

相关文章

发表评论
暂无评论