4秒内从0到100的进度条

问题描述 投票:0回答:1

我希望进度条在 4 秒内精确地从 0 到 100,但是我记录的越多,得到的就越多。

这是我的代码

 private fun progress1 (int: Int=1){

        if (int in 1..100){

            if (int==1){
                startTime=System.currentTimeMillis()
                coroTine.launch {
                    aa.progressbar1.setProgress(int, true)
                    delay(40)
                    progress1(int + 1)
                }
            }
            if (int in 2..100){


                if (int in 2..99){
            coroTine.launch {
                aa.progressbar1.setProgress(int,true)
                delay(40)
                progress1 (int+1)
            }
            }
            }
            if (int==100){
                aa.progressbar1.setProgress(int,true)
                endTime=System.currentTimeMillis()
                val dif=endTime-startTime
                Log.i("dif", "dif is : $dif ")
                progress1(1)
            }
            
        }
    }

我的日志>

差异是:4118 差异是:4102 差异是:4103 差异是:4103 差异是: 4096 差异是: 4091 差异是: 4084 差异是: 4095 差异是: 4074 差异 是:4075 差异是:4064 差异是:4067 差异是:4060 差异是:4072 差异是:4047 差异是:4058

android kotlin android-progressbar
1个回答
0
投票

有趣的是它是用倒计时器修复的

private fun progress(){
        startTime=System.nanoTime()
        val min=4000
        val countDown=object :CountDownTimer(min.toLong(),40){
            override fun onTick(p0: Long) {

                progress1Value+=1
                aa.progressbar1.setProgress(progress1Value,true)
            }

            override fun onFinish() {
            endTime=System.nanoTime()
                val dif=(endTime-startTime)/1000000

                Log.i("dif", "dif is : $dif ")

            }

        }.start()
    }
© www.soinside.com 2019 - 2024. All rights reserved.