You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 00:00:57