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

如何从进度条动画(计时器)中获取剩余秒数?

问题:获取Fragment退出时进度条计时器的剩余秒数

我在Fragment中实现了一个10秒的进度条递减动画(从100到0),每次进入Fragment都会启动。现在需要在点击退出按钮时,获取并打印剩余的秒数(比如剩余4秒就打印4),当前代码仅实现了进度条动画,没有这个功能。

原代码如下:

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                          savedInstanceState: Bundle?): View? {
        val binding = DataBindingUtil.inflate<FragmentGameBinding>(
            inflater, R.layout.fragment_game, container, false)

        binding.game = this
        timer(binding)
        
        // when user clicks submitButton, it will exit current fragment
        binding.submitButton.setOnClickListener{ view: View ->
           Log.d("asd","print seconds left when exit the fragment : ") //print here
           view.findNavController().navigate(GameFragmentDirections.actionGameFragmentToGameOverFragment())
        }
        
        return binding.root
}
private fun timer(binding:FragmentGameBinding){
    val mProgressBar = binding.progressBar
    val progressAnimator = ObjectAnimator.ofInt(mProgressBar, "progress", 100, 0) //max 100, min 0
    progressAnimator.duration = 10000 //10 s = 10000 ms
    progressAnimator.interpolator = LinearInterpolator()
    progressAnimator.start()
}

实现步骤

  • 将progressAnimator声明为Fragment的类成员变量,确保在按钮点击事件中能访问到它。
  • 在按钮点击时,通过currentPlayTime获取动画已运行的毫秒数,用总时长减去已运行时长得到剩余毫秒数,再转换为秒数(取整)。
  • 打印计算出的剩余秒数。

修改后的完整代码:

class GameFragment : Fragment() {
    // 声明为类成员变量,方便点击事件中访问
    private lateinit var progressAnimator: ObjectAnimator

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        val binding = DataBindingUtil.inflate<FragmentGameBinding>(
            inflater, R.layout.fragment_game, container, false)

        binding.game = this
        timer(binding)
        
        binding.submitButton.setOnClickListener{ view: View ->
            // 计算剩余秒数
            val elapsedMs = progressAnimator.currentPlayTime
            val remainingMs = progressAnimator.duration - elapsedMs
            val remainingSeconds = (remainingMs / 1000).toInt()
            Log.d("asd","print seconds left when exit the fragment : $remainingSeconds")
            
            view.findNavController().navigate(GameFragmentDirections.actionGameFragmentToGameOverFragment())
        }
        
        return binding.root
    }

    private fun timer(binding:FragmentGameBinding){
        val mProgressBar = binding.progressBar
        progressAnimator = ObjectAnimator.ofInt(mProgressBar, "progress", 100, 0)
        progressAnimator.duration = 10000
        progressAnimator.interpolator = LinearInterpolator()
        progressAnimator.start()
    }
}

关键说明

  • progressAnimator.currentPlayTime:获取动画已经运行的毫秒数,动画未启动时返回0,结束后返回总时长。
  • 因为使用了LinearInterpolator线性插值,进度和时间完全对应,所以剩余时间的计算是准确的。
  • 最后将剩余毫秒数除以1000取整,得到剩余的整秒数。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 04:50:26