Progress bar from 0 to 100 in 4 seconds

76 Views Asked by At

I want the progress bar to be exactly 0 to 100 in 4 seconds, but the more I log, the more it gets.

its my code

 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)
            }
            
        }
    }

my logs>

dif is : 4118 dif is : 4102 dif is : 4103 dif is : 4103 dif is : 4096 dif is : 4091 dif is : 4084 dif is : 4095 dif is : 4074 dif is : 4075 dif is : 4064 dif is : 4067 dif is : 4060 dif is : 4072 dif is : 4047 dif is : 4058

1

There are 1 best solutions below

1
Rahimian On

It is interesting that it was fixed with a countdown timer

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()
    }