首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何从ProgressBar获取秒数

如何从ProgressBar获取秒数
EN

Stack Overflow用户
提问于 2018-06-20 15:10:36
回答 1查看 288关注 0票数 0

我有一个循环进度条,目前运行良好,但现在我需要添加一个像TextView之类的东西来知道还剩多少秒。

这是我的ProgressBar

代码语言:javascript
复制
 <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

代码语言:javascript
复制
<?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秒,如下所示:

代码语言:javascript
复制
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,但其余的都不能工作。

EN

回答 1

Stack Overflow用户

发布于 2018-06-20 15:19:59

我建议使用CountDownTimer。创建一个具有所需持续时间(例如5秒)的CountDownTimer,然后在每个节拍上更新ProgressBarTextView。它将为您提供剩余的持续时间。

代码语言:javascript
复制
    // 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();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50942286

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档