如何在android中暂停和恢复CountDown Timer

时间:2016-08-28 07:22:10

标签: java android android-studio timer countdown

我开发了一个带有ProgressBar的CountDown计时器,它运行正常但我不确定当我点击fab_play_pause按钮时如何暂停和恢复计时器。我需要帮助,非常感谢你

这是我的代码:

public class StandingSideStretch extends Fragment {

View rootView;
private TextView timer;
private FloatingActionButton fab_play_pause, fab_next, fab_back;
private ProgressBar mProgress;
MyCountDownTimer myCountDownTimer;
private CountDownTimer countDownTimer;

private final long startTime = 60 * 1000;
private final long interval = 1 * 1000;

private boolean timerHasStarted = false;
private ObjectAnimator animation;

private static final String PLAY_ICON = "playIcon";
private static final String PAUSE_ICON = "pauseImage";


public StandingSideStretch() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.fragment_standing_side_stretch, container, false);

    setFabs();

    timer = (TextView)rootView.findViewById(R.id.timer);
    countDownTimer = new MyCountDownTimer(startTime, interval);
    timer.setText("60's");

    return rootView;
}

private void setFabs(){
    fab_play_pause = (FloatingActionButton)rootView.findViewById(R.id.fab_play_pause);
    fab_back = (FloatingActionButton) rootView.findViewById(R.id.fab_back);
    fab_next = (FloatingActionButton) rootView.findViewById(R.id.fab_next);

    fab_play_pause.setImageResource(R.drawable.ic_play_arrow_white_24dp);
    fab_play_pause.setTag(PLAY_ICON);
    fab_play_pause.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            String tag = (String) view.getTag();

            if (view.getTag() == PLAY_ICON){

                fab_play_pause.setTag(PAUSE_ICON);
                fab_play_pause.setImageResource(R.drawable.ic_pause_white_24dp);
                timerHasStarted = true;

                countDownTimer.start();

                Resources res = getResources();
                Drawable drawable = res.getDrawable(R.drawable.custom_progressbar_drawable);
                final ProgressBar mProgress = (ProgressBar) rootView.findViewById(R.id.circularProgressbar);
                mProgress.setProgress(0);   // Main Progress
                mProgress.setSecondaryProgress(100); // Secondary Progress
                mProgress.setMax(100); // Maximum Progress
                mProgress.setProgressDrawable(drawable);
                mProgress.setRotation(0);

                animation = ObjectAnimator.ofInt(mProgress, "progress", 0, 100);
                animation.setDuration(startTime);
                animation.setInterpolator(new DecelerateInterpolator());
                animation.start();

            }else if (view.getTag() == PAUSE_ICON){
                fab_play_pause.setTag(PLAY_ICON);
                fab_play_pause.setImageResource(R.drawable.ic_play_arrow_white_24dp);
                countDownTimer.cancel();
                animation.cancel();
            }
        }
    });

    fab_back.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
        }
    });

}

public class MyCountDownTimer extends CountDownTimer{

    public MyCountDownTimer(long startTime, long interval) {
        super(startTime, interval);
    }

    @Override
    public void onTick(long millisUntilFinished) {
        timer.setText("" + millisUntilFinished / 1000);

    }

    @Override
    public void onFinish() {
        timer.setText("0");

    }
}}

1 个答案:

答案 0 :(得分:1)

抱歉,countdownTimer类没有暂停和恢复方法。

当您想要开始时,您可以使用

timer.start()

当你想要停止时,你可以打电话

timer.cancel()

相关问题