【问题标题】:how to make a countdown progress bar going down continuously?如何使倒计时进度条不断下降?
【发布时间】:2018-10-03 21:29:34
【问题描述】:

我在 android 中使用倒计时进度条。我希望它在不停止的情况下随新时间更新。它现在正在和我一起工作,但它会停止一小段时间,然后加入进度我不希望它总是不停地减少。

这是我的 xml 代码:

<ProgressBar
    android:id="@+id/timerProgressbar"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="match_parent"
    android:layout_height="5dp"
    android:layout_below="@id/appBar"
    android:layout_marginTop="-2dp"
    android:layout_marginLeft="-1dp"
    android:layout_marginRight="-1dp"
    android:max="20"
    android:rotation="180"
    android:progress="20" />

这是java代码:

timer = new CountDownTimer(21000, 1) {

            public void onTick(long millisUntilFinished) {
                index++;
                timerProgressBar.setProgress((int)millisUntilFinished/1000);
            }

            public void onFinish() {
                index++;
                timerProgressBar.setProgress(0);
                navigate();
            }

        };

【问题讨论】:

  • CountDownTimer(21000, 1) 你不是说1000。 1 表示 1 毫秒?。
  • 我把它设为 1 是因为我希望它每毫秒更新一次
  • 每 1 毫秒更新一次进度开销太大。 setProgress 是一个 UI 操作,它可能需要超过 1 毫秒。尝试将其设置为 200 左右。它仍然看起来很平滑
  • 我改了,还是没解决问题

标签: java android android-progressbar countdowntimer


【解决方案1】:

你可以这样做。显示 30 秒倒计时:

new CountDownTimer(30000, 1000) {

     public void onTick(long millisUntilFinished) {
            index++;
            timerProgressBar.setProgress((int)millisUntilFinished/1000);
     }

     public void onFinish() {
          index++;
          timerProgressBar.setProgress(0);
          navigate();
     }
  }.start();

【讨论】:

  • 这将使它每秒更新一次进度条,但我希望它始终显示给用户。
  • 在下面查看另一个答案。
【解决方案2】:

您不应该像现在这样运行繁忙的循环。该应用程序可能会锁定。您需要让系统的其余部分继续工作。此外,人眼可以跟踪的最快速度是 30 毫秒左右。下面的计时器计数毫秒数最多为 10。您可以通过更改设置最大计数值 if(mcount==yourvalue).

long seconds=0;
long millis=0;
int mCount=0;
Timer timer;

//timer textview
timeflg = (TextView)findViewById(R.id.timerflg);

timer = new Timer();
       timer.scheduleAtFixedRate(new TimerTask() {
       @Override
       public void run() {
       // TODO Auto-generated method stub
                timerMethod();
       }
}, 1,100);

//method
private void timerMethod(){
   this.runOnUiThread(generate);
}
private Runnable generate= new Runnable() {
     @Override
     public void run() {
         timeflg.setText("TIME "+ seconds + ":" + mCount);
         mCount++;
         if(mCount==10){
            mCount=0;
            second++;
         }
     }
};

【讨论】:

  • second++ 应该是 second(s)++
  • textView 中的时间每 1/10 秒更新一次,但进度条仍然每 1 秒更新一次
  • 那么我猜进度条更新事件有一种低优先级线程的限制。不过不确定。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多