带有Seekbar的CountDownTimer

时间:2016-01-20 06:17:07

标签: android seekbar countdowntimer

您好我已经用CountDownTimer实现了SeekBar计时器并且它工作正常,我的问题是当我拖动搜索栏时我无法更改计时器如何实现此目的。

示例:如果我拖动并增加CountDownTimer,它应该增加时间,就像减少一样。

CountDownTimer代码:

private void startTimer(final int minuti) {
    countDownTimer = new CountDownTimer(60 * minuti * 1000 ,
            500) {

        // 500 means, onTick function will be called at every 500
        // milliseconds

        @Override
        public void onTick(long leftTimeInMilliseconds) {


            circularSeekbar.setMax(60 * minuti);



            int barVal = 0;


                barVal = ((int) (minuti * 60 ) - (int) (seconds));



            circularSeekbar.setProgress(-barVal);


            mTvTime.setText(String.format("%02d", seconds / 60) + ":"
                    + String.format("%02d", seconds % 60));
            Log.d("TIME", mTvTime.getText().toString());




        }

        @Override
        public void onFinish() {

        }
    }.start();

}

Seekbar代码:

public class CircleSeekBarListener implements
                OnCircularSeekBarChangeListener {
            @Override
            public void onProgressChanged(CircularSeekBar circularSeekBar,
                    int progress, boolean fromUser) {

                if (fromUser) {


                }

            }

            @Override
            public void onStopTrackingTouch(CircularSeekBar seekBar) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onStartTrackingTouch(CircularSeekBar seekBar) {
                // TODO Auto-generated method stub

            }
        }

2 个答案:

答案 0 :(得分:0)

试试这段代码。

public class MainActivity extends AppCompatActivity {
    private int seconds = 120; // two min
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        circularSeekbar = (SeekBar) findViewById(R.id.circularSeekbar);
        mTvTime = (TextView) findViewById(R.id.mTvTime);


        circularSeekbar.setMax(seconds);
        circularSeekbar.setProgress(seconds);
        circularSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if(fromUser) {
                  seconds = progress;
                }

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                startTimer(seconds * 1000);
            }
        });

        startTimer(seconds * 1000);
    }


    private void startTimer(final long milisecods) {
        if (countDownTimer != null) {
            countDownTimer.cancel();
        }
        countDownTimer = new CountDownTimer(milisecods, 500) {

            // 500 means, onTick function will be called at every 500
            // milliseconds

            @Override
            public void onTick(long leftTimeInMilliseconds) {

                int barVal = (int) leftTimeInMilliseconds / 1000;
                circularSeekbar.setProgress(barVal);
                mTvTime.setText(String.format("%02d:%02d",
                        TimeUnit.MILLISECONDS.toMinutes(leftTimeInMilliseconds),
                        TimeUnit.MILLISECONDS.toSeconds(leftTimeInMilliseconds) -
                                TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(leftTimeInMilliseconds))
                ));
            }

            @Override
            public void onFinish() {

            }
        }.start();

    }
}

答案 1 :(得分:0)

here is code
set your seekbar in onTick 
       timer = new CounterClass(150000, 1000);
                timer.start();
    public class CounterClass extends CountDownTimer {
        public CounterClass(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {
            // timeTextView.setText("Completed.");

            this.cancel();
            AppUtills.FROMTIME_FINISH = true;
            finish();
        }

        public void onTick(long millisUntilFinished) {
            long millis = millisUntilFinished;
            caaryLong = millis;
            String hms = String.format(
                    "%02d:%02d",
                    TimeUnit.MILLISECONDS.toMinutes(millis)
                            - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS
                                    .toHours(millis)),
                    TimeUnit.MILLISECONDS.toSeconds(millis)
                            - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS
                                    .toMinutes(millis)));
            System.out.println(hms);

            timeTextView.setText(hms);
        }
    }