问题描述
我的布局包含一个圆形的ProgressBar和一个显示倒计时的TextView。 如何在进度栏中显示计时器的当前进度?进度栏在第一次显示时应该是满的,而在计时器计时时应该是空白的。
private fun setProgressBar(timetoShow: Int) {
countDownTimer = object : CountDownTimer(
timetoShow * ONE_SECOND_INTERVAL,ONE_SECOND_INTERVAL
) {
override fun onTick(millisUntilFinished: Long) {
question_time.text = (millisUntilFinished / ONE_SECOND_INTERVAL).toString()
//handleProgressBar
}
override fun onFinish() {
hideLayout()
}
}.start()
}
<ProgressBar
android:id="@+id/question_time_progress"
android:layout_width="match_parent"
android:layout_height="match_parent"
style="?android:progressBarStyleHorizontal"
android:progressDrawable="@drawable/circle_progressbar"
/>
<TextView
android:id="@+id/question_time"
android:layout_width="26dp"
android:layout_height="26dp"
android:layout_gravity="center"
android:gravity="center"
android:textColor="@color/test_color_light"
android:textStyle="bold"
tools:text="14" />
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:shape="ring"
android:thicknessRatio="16"
android:useLevel="false">
<solid android:color="#DDD" />
</shape>
</item>
<item>
<rotate
android:fromdegrees="270"
android:todegrees="270">
<shape
android:shape="ring"
android:thicknessRatio="16"
android:useLevel="true">
<gradient
android:endColor="@color/colorPrimary"
android:startColor="@color/colorAccent"
android:type="sweep" />
</shape>
</rotate>
</item>
</layer-list>
解决方法
您应该将最大进度设置为倒数开始的数字。 例如,如果倒数从60开始。 您应该设置progressBar.setMax(60)
然后还在每次倒数时设置进度条的值 例如progressBar.setValue(59),然后是58,然后是57...。 进度栏将按照您想要的方式工作。