如何从ProgressBar获取秒

时间:2018-06-20 07:10:36

标签: java android

我有一个圆形的进度条,目前可以正常工作,但是现在我需要添加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,其余的都无法正常工作。

1 个答案:

答案 0 :(得分:0)

我建议改用protected Record getNext() throws Exception { if (hits == null) { hits = index.query(query); } T next = hits.next(); if (next == null) { return finished(); } else { return nodeToRecord.call(next); } } 。创建一个具有所需持续时间(例如5秒钟)的CountDownTimer,然后在每个刻度上更新CountDownTimerProgressBar。它将为您提供剩余时间。

TextView