activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<ProgressBar
android:id="@+id/progress_time"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_centerInParent="true"
android:background="@drawable/progress_bar_ct"
android:indeterminate="false"
android:progressDrawable="@drawable/progress_bar_drawable"
/>
<Button
android:id="@+id/text_time"
android:gravity="center"
android:layout_centerInParent="true"
android:layout_width="60dp"
android:background="@null"
android:layout_height="60dp">
</Button>
</RelativeLayout>
progress_bar_drawable.xml
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="270"
android:toDegrees="270">
<shape
android:innerRadiusRatio="2.5"
android:shape="ring"
android:thickness="3dp"
android:useLevel="true">
<gradient
android:angle="0"
android:endColor="#6DC6DA"
android:startColor="#6DC6DA"
android:type="sweep"
android:useLevel="false" />
</shape>
</rotate>
progress_bar_ct.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="ring"
android:innerRadiusRatio="2.5"
android:thickness="2dp"
android:useLevel="false">
<solid android:color="#28313F" />
</shape>
CountDownTimer
public abstract class CountDownTimer {
private final long mMillisInFuture;
private final long mCountdownInterval;
private long mMillisFinished = 0;
private long mElapsedRealtime;
private boolean mCancelled = false;
public CountDownTimer(long millisInFuture, long countDownInterval) {
mMillisInFuture = millisInFuture;
mCountdownInterval = countDownInterval;
}
public synchronized final void cancel() {
mCancelled = true;
mHandler.removeMessages(MSG);
}
public synchronized final CountDownTimer stop() {
mHandler.removeMessages(MSG);
long elapsedRealtime = mElapsedRealtime;
mElapsedRealtime = SystemClock.elapsedRealtime();
final long millis = mElapsedRealtime - elapsedRealtime;
mMillisFinished += millis;
onTick(mMillisFinished);
return this;
}
public synchronized final CountDownTimer start() {
mCancelled = false;
if (mMillisInFuture <= mMillisFinished) {
onFinish(mMillisFinished);
return this;
}
mElapsedRealtime = SystemClock.elapsedRealtime();
mHandler.sendMessage(mHandler.obtainMessage(MSG));
return this;
}
public synchronized final long getNowTime() {
return mMillisFinished + SystemClock.elapsedRealtime() - mElapsedRealtime;
}
public abstract void onTick(long millisUntilFinished);
public abstract void onFinish(long millisUntilFinished);
private static final int MSG = 1;
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
synchronized (CountDownTimer.this) {
if (mCancelled) {
return;
}
long elapsedRealtime = mElapsedRealtime;
mElapsedRealtime = SystemClock.elapsedRealtime();
final long millis = mElapsedRealtime - elapsedRealtime;
mMillisFinished += millis;
if (mMillisInFuture <= mMillisFinished) {
onFinish(mMillisFinished);
} else {
onTick(mMillisFinished);
long delay = mMillisInFuture - mMillisFinished;
if (delay > mCountdownInterval) {
delay = mElapsedRealtime + mCountdownInterval - SystemClock.elapsedRealtime() - mMillisFinished % mCountdownInterval;
}
while (delay < 0)
delay += mCountdownInterval;
sendMessageDelayed(obtainMessage(MSG), delay);
}
}
}
};
}
TimeCountRefresh
public class TimeCountRefresh extends CountDownTimer {
private ProgressBar mProgressBar;
private Button textView;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("mm:ss");
public TimeCountRefresh(long millisInFuture, long countDownInterval, final ProgressBar progressBar, Button textView) {
super(millisInFuture, countDownInterval);
this.mProgressBar = progressBar;
this.textView = textView;
}
@Override
public void onTick(long millisUntilFinished) {
mProgressBar.setProgress((int) millisUntilFinished + 800);
textView.setText(simpleDateFormat.format(millisUntilFinished));
}
@Override
public void onFinish(long millisUntilFinished) {
}
}
MainActivity
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private ProgressBar progress_time;
private Button text_time;
private TimeCountRefresh timeCountRefresh;
private int mMaxValue = 1 * 60 * 1000;
private int mIntervalValue = 1000;
private boolean mIsStop=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progress_time = findViewById(R.id.progress_time);
text_time = findViewById(R.id.text_time);
text_time.setOnClickListener(this);
progress_time.setMax(mMaxValue);
timeCountRefresh = new TimeCountRefresh(mMaxValue, mIntervalValue, progress_time,text_time);
timeCountRefresh.start();
}
@Override
protected void onDestroy() {
super.onDestroy();
if (timeCountRefresh != null) {
timeCountRefresh.cancel();
}
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.text_time:
clickPlayOrPause();
break;
}
}
private void clickPlayOrPause() {
if (!mIsStop) {
timeCountRefresh.stop();
mIsStop=true;
}
else {
timeCountRefresh.start();
mIsStop=false;
}
}
}