【问题标题】:How to make an horizontal progress bar depend on seconds in Android?如何使水平进度条取决于Android中的秒数?
【发布时间】:2011-09-16 16:23:34
【问题描述】:

我想知道如何使水平进度条取决于秒。例如,我必须向进度条写入什么代码,从 0 秒开始,到 60 秒后达到 100%?

恢复我想制作一个仅取决于秒数的水平进度条。

【问题讨论】:

    标签: android progress-bar seconds


    【解决方案1】:

    这个答案是根据上面的答案修改的。

        progressBar = (ProgressBar) findViewById(R.id.progressbar);
    
        // timer for seekbar
            final int oneMin = 1 * 60 * 1000; // 1 minute in milli seconds
    
            /** CountDownTimer starts with 1 minutes and every onTick is 1 second */
            new CountDownTimer(oneMin, 1000) {
                public void onTick(long millisUntilFinished) {
    
                    //forward progress
                    long finishedSeconds = oneMin - millisUntilFinished;
                    int total = (int) (((float)finishedSeconds / (float)oneMin) * 100.0);
                    progressBar.setProgress(total);
    
    //                //backward progress
    //                int total = (int) (((float) millisUntilFinished / (float) oneMin) * 100.0);
    //                progressBar.setProgress(total);
    
                }
    
                public void onFinish() {
                    // DO something when 1 minute is up
                }
            }.start();
    

    【讨论】:

      【解决方案2】:
      bar = (ProgressBar) findViewById(R.id.progress);
          bar.setProgress(total);
          int oneMin= 1 * 60 * 1000; // 1 minute in milli seconds
      
          /** CountDownTimer starts with 1 minutes and every onTick is 1 second */
          cdt = new CountDownTimer(oneMin, 1000) { 
      
              public void onTick(long millisUntilFinished) {
      
                  total = (int) ((timePassed/ 60) * 100);
                  bar.setProgress(total);
              }
      
              public void onFinish() {
                   // DO something when 1 minute is up
              }
          }.start();
      

      我已经编辑了代码。现在看看。那么这是如何工作的。首先,您在进度条上设置一个总数,在您的情况下为 60。然后您需要计算自开始以来经过的时间百分比以及使用 timePassed/60*100 获得的百分比并将其转换为 int。因此,在每个刻度上,您都会将进度增加总大小的 1/100。 希望这能让它更清楚。

      【讨论】:

      • 什么是变量timePassed?
      【解决方案3】:

      我假设您有一些代码已经数到 60。基于此:

      int time; // 0-60 seconds
      ProgressBar bar = (ProgressBar) findViewById(R.id.progressBar);
      bar.setProgress((time / 60.0) * 100);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-01
        • 2012-06-26
        • 2013-01-12
        • 1970-01-01
        相关资源
        最近更新 更多