Android中如何设置动画速度?解决动画初始过快问题
调整Android ObjectAnimator动画执行速度的方法
你的动画过快是因为ObjectAnimator默认的动画时长仅为300毫秒,只需给每个动画设置更长的duration属性就能减慢速度。
修改后的代码示例
val animator1 = ObjectAnimator .ofFloat(binding.logoAnimated, "translationX", 0f, -(width - point.x)).apply { repeatCount = 1 repeatMode = ValueAnimator.REVERSE duration = 1000 // 设置为1秒,可根据需求调整数值(单位:毫秒) } val animator2 = ObjectAnimator .ofFloat(binding.logoAnimated,"translationX", 0f, +(width - point.x)).apply { repeatCount = 1 repeatMode = ValueAnimator.REVERSE duration = 1000 // 统一设置相同时长 } val animatorSet = AnimatorSet() animatorSet.playSequentially(animator1, animator2) animatorSet.start()
额外优化选项
- 若要让所有子动画共用同一时长,可直接在
AnimatorSet上设置duration,无需逐个配置:animatorSet.duration = 1000 - 还可通过插值器调整动画的速度曲线,比如使用
LinearInterpolator实现匀速运动,避免默认的先加速后减速效果:animator1.interpolator = LinearInterpolator() animator2.interpolator = LinearInterpolator()
内容的提问来源于stack exchange,提问作者DevMD
相关产品推荐
相关产品推荐

