微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

带有 CircularProgressIndicator 的倒数计时器

如何解决带有 CircularProgressIndicator 的倒数计时器

我尝试将带有 circular progress indicator(来自 material design components)的倒数计时器添加到我的应用中,但我在设置 circular progress indicator 的初始值时遇到问题。

以下是我编写的代码的相关部分:

countDownTimer = object : CountDownTimer(60000,1000) {

                    override fun onTick(millisUntilFinished: Long) {
                        binding.apply {
                            countDownTimer.text =  (millisUntilFinished / 1000).toString()
                            progressIndicator.progress = (millisUntilFinished / 1000).toInt()
                        }
                    }
                    override fun onFinish() {
                        // if 60 secs are finished,then we stop the record
                        stopRecording()
                    }
                }

<androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="top|center">

            <TextView
                android:id="@+id/count_down_timer"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:textSize="20sp"
                android:textStyle="bold"
                android:background="@color/black"
                android:textColor="@color/white"
                tools:text="60"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent" />

            <com.google.android.material.progressindicator.CircularProgressIndicator
                android:id="@+id/progress_indicator"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                app:indicatorColor="@color/navajowhite"
                app:trackCornerRadius="10dp"
                app:indicatorSize="15dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintTop_toBottomOf="@id/count_down_timer"
                android:layout_margin="10dp"/>

        </androidx.constraintlayout.widget.ConstraintLayout>

我让计数器从 60 秒开始到 0。但我认为我的循环进度指示器的进度值是错误的。因为它的初始启动看起来像这样(我创建了一个草图来向您展示计数器启动时圆形进度指示器的初始状态):

enter image description here

如您所见,圆圈并不完整。您看不到的是 circular progress indicator 的“轨迹”从此递减,直到达到 0。所以,不知何故,circular progress indicator 的递减是有效的。但出发点是错误的。

我想要的一个初始的开始,应该是这样的:

enter image description here

在这里做错了什么?

解决方法

您的进度计算不正确。

progressIndicator.progress = (millisUntilFinished / 1000).toInt()

如果为 millisUntilFinished == 59000,您最终将获得 59 的进度。你应该在这里计算一个百分比而不是这个。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。