我有一个循环进度条,目前运行良好,但现在我需要添加一个像TextView
之类的东西来知道还剩多少秒。
这是我的ProgressBar
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:max="500"
android:progress="0"
android:progressDrawable="@drawable/circular" />
然后我就有了这个Drawable
<?xml version="1.0" encoding="utf-8"?>
<shape android:useLevel="true"
android:innerRadiusRatio="2.3"
android:shape="ring"
android:thickness="3.8sp"
xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/colorPrimary" />
</shape>
为了测试,我设置了5秒,如下所示:
ProgressBar progressBar = rootView.findViewById(R.id.progressBar);
ObjectAnimator animation = ObjectAnimator.ofInt(progressBar, "progress", 0, 500);
animation.setDuration(5000);
animation.setInterpolator(new DecelerateInterpolator());
animation.start();
我试着用addUpdateListener
来了解当前的动画值,但我意识到它是0到500。
我尝试的是这样的:(valueAnimator.getAnimatedValue()/ 60) * 100)
,但它不工作。
如何在动态添加持续时间(5秒)并将其显示在TextView
上的情况下执行此操作
另外,我想知道为什么我的ProgressBar
不是从顶部开始,而是从右侧开始。
如果您有任何其他方法,请随时发表评论。我的目标是向ProgressBar
显示持续时间是动态的,然后显示在TextView
上剩下的秒数,直到现在我有了ProgressBar
,但其余的都不能工作。
发布于 2018-06-20 15:19:59
我建议使用CountDownTimer
。创建一个具有所需持续时间(例如5秒)的CountDownTimer
,然后在每个节拍上更新ProgressBar
和TextView
。它将为您提供剩余的持续时间。
// Creating a CountDownTimer for 5 seconds, that will call onTick every second.
final int totalTime = 5000;
new CountDownTimer(totalTime, 100) {
public void onTick(long millisUntilFinished) {
long secondsRemaining = ( millisUntilFinished / 1000);
// Updating the TextView with seconds remaining
textView.setText(String.valueOf(secondsRemaining));
// Calculating the progress out of 100.
int progress = (int) millisUntilFinished / totalTime;
// Creating an animation to smoothly update the progress bar between each tick.
ObjectAnimator animation = ObjectAnimator.ofInt(progressBar, "progress", progressBar.getProgress(), progress);
animation.setDuration(100);
animation.setInterpolator(new DecelerateInterpolator());
animation.start();
}
public void onFinish() {
// done
}
}.start();
https://stackoverflow.com/questions/50942286
复制相似问题