0
ProgressBarを使用してカウントダウンタイマーを正常に動作させましたが、fab_play_pauseボタンをクリックするとタイマーを一時停止して再開する方法がわかりません。CountDown Timerをアンドロイドで一時停止して再開する方法
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");
}
}}