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

如何延迟SplashFragment切换时机以完成Lottie动画

解决Lottie动画未完成就跳转页面的问题

你的问题出在没有监听Lottie动画的完成事件,页面跳转逻辑可能在动画启动后立刻执行了。可以通过以下步骤修复:

  • 先确保布局里的LottieAnimationView设置了id,比如android:id="@+id/lottie_splash"
  • 在Fragment中获取该视图并绑定动画完成监听器
  • 仅在动画执行完毕后,再执行页面切换操作

修改后的代码示例:

class SplashFragment : Fragment() {

    private lateinit var binding: FragmentSplashBinding

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = FragmentSplashBinding.inflate(inflater, container, false)
        setupLottieListener()
        return binding.root
    }

    private fun setupLottieListener() {
        binding.lottieSplash.addAnimatorListener(object : Animator.AnimatorListener {
            override fun onAnimationStart(animation: Animator?) {}

            override fun onAnimationEnd(animation: Animator?) {
                // 动画完成后执行页面跳转,替换成你的导航逻辑
                findNavController().navigate(R.id.action_splash_to_main)
            }

            override fun onAnimationCancel(animation: Animator?) {}

            override fun onAnimationRepeat(animation: Animator?) {}
        })
    }
}

关键提醒

别用Handler().postDelayed设置固定3秒延迟,因为设备性能或动画文件差异可能导致实际时长波动,监听动画结束回调是更可靠的方式。同时要确认布局里的LottieAnimationView已经正确关联了你的动画资源(比如app:lottie_rawRes="@raw/your_splash_anim")。

内容的提问来源于stack exchange,提问作者Ali

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 18:30:43