Java 如何从ProgressBar获取秒数
我有一个圆形进度条,目前运行良好,但现在我需要添加一个Java 如何从ProgressBar获取秒数,java,android,Java,Android,我有一个圆形进度条,目前运行良好,但现在我需要添加一个TextView或一些东西来知道还剩多少秒 这是我的ProgressBar <ProgressBar android:id="@+id/progressBar" style="?android:attr/progressBarStyleHorizontal" android:layout_width="150dp" android:layout_height="150dp
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" />
为了进行测试,我放了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秒)并在文本视图上显示它,我该如何做到这一点
我还想知道为什么我的ProgressBar
不是从顶部开始,而是从右侧开始
如果你有其他的方法,请随意评论。我的目标是向ProgressBar
显示持续时间是动态的,然后在TextView
上显示剩余的秒数,直到现在我才有ProgressBar
,但其余的都不起作用 我建议改用倒计时装置。创建一个具有所需持续时间(比如5秒)的倒计时程序,然后在每个刻度上更新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();
您可以计算进度条的进度,然后在onTick中更新进度条。剩余秒数/总持续时间=完成百分比。然后,如果你愿意,用动画更新你的进度条。请随便举个例子,还有为什么我的进度条不是从12点开始的?它从3开始?我改进了我的例子,关于如何更新视图,你需要调整值使它感觉正确。但这应该会给你一个如何做的想法。至于起始位置,我不知道,也许可以旋转你的牵引杆。
// 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();