如何制作水平进度条取决于Android中的秒数?

Ric*_*rdo 4 android seconds progress-bar

我想知道如何根据秒数制作水平进度条.例如,我必须写入进度条的代码在0秒开始时为0%,在60秒后达到100%?

恢复我想制作一个仅取决于秒而不是其它的水平进度条.

DAr*_*rkO 9

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();
Run Code Online (Sandbox Code Playgroud)

我编辑了代码.现在看.这是怎么回事 首先你在进度条上设置一个总数,在你的情况下将是60.然后你需要计算自开始以来经过多少时间的百分比,你得到timePassed/60*100并将其转换为int.因此,在每个刻度线上,您将进度增加总尺寸的1/100.希望这更清楚.

  • 什么是变量timePassed? (4认同)

Thi*_*van 6

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

    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();
Run Code Online (Sandbox Code Playgroud)