昨天的程序需求中需要一个倒计时器,我自己用TextView封装了一个,只是能用而已,今天早上发现Android 系统自己已经实现了一个,很好用,在这里记录一下,方便下次使用。
我自己封装的如下:
package com.hy.testfragment;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.TextView;
public class HyTextViewTimer extends TextView {
private int timer = -1;
private Paint paint = new Paint();
private HyEndOfTimerListener hyEndOfTimerListener = null;
private MyThread th = null;
Rect targetRect = new Rect(0, 0, 10, 10);
private void init() {
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setStrokeWidth(3);
paint.setTextSize(26);
paint.setColor(Color.WHITE);
paint.setTextAlign(Paint.Align.CENTER);
setFocusable(true);
}
public HyTextViewTimer(Context context) {
super(context);
init();
}
public HyTextViewTimer(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public HyTextViewTimer(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
class MyThread extends Thread {
HyTextViewTimer hyTextViewTimer = null;
boolean bFlag = true;
public MyThread(HyTextViewTimer hyTextViewTimer) {
this.hyTextViewTimer = hyTextViewTimer;
bFlag = true;
}
public void hy_stop() {
bFlag = false;
}
@Override
public void run() {
while (bFlag) {
this.hyTextViewTimer.postInvalidate();
try {
for (int i = 0; i < 10; i++) {
Thread.sleep(100);
if(!bFlag) {
break;
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
if (timer >= 0) {
if (timer == 0 && hyEndOfTimerListener != null) {
if(bFlag) {
hyEndOfTimerListener.OnEnd();
}
break;
}
if(bFlag) {
timer--;
}
}
}
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
targetRect.set(0, 0, getWidth(), getHeight());
Paint.FontMetricsInt fontMetrics = paint.getFontMetricsInt();
int baseline = (targetRect.bottom + targetRect.top - fontMetrics.bottom
- fontMetrics.top) / 2;
canvas.drawText(String.valueOf(timer < 0 ? 0 : timer),
targetRect.centerX(), baseline, paint);
}
public interface HyEndOfTimerListener {
/**
* 倒计时结束时调用
*/
void OnEnd();
}
public void setTimer(int value) {
this.timer = value;
}
public void start() {
if(th != null) {
th.hy_stop();
th = null;
}
th = new MyThread(this);
th.start();
}
public void stop() {
if(th != null) {
th.hy_stop();
th = null;
}
}
public void setTextSize(int fontSize) {
paint.setTextSize(fontSize);
}
public void setEndOfTimerListener(HyEndOfTimerListener hyEndOfTimerListener) {
this.hyEndOfTimerListener = hyEndOfTimerListener;
}
}
使用方法如下:
<com.hy.testfragment.HyTextViewTimer
android:id="@+id/hyt"
android:layout_width="250dp"
android:layout_height="260dp"
android:layout_marginLeft="100dp"
android:textSize="25sp"/>
hyt = (HyTextViewTimer) findViewById(R.id.hyt);
hyt.setTextSize(80);
hyt.setEndOfTimerListener(new HyTextViewTimer.HyEndOfTimerListener() {
@Override
public void OnEnd() {
//Toast.makeText(MainActivity.this, "the end", Toast.LENGTH_SHORT).show();
System.out.println("---> the end");
Message msg = handler.obtainMessage();
handler.sendMessage(msg);
}
});
// 开始使用
hyt.setTimer(9);
hyt.start();
// 停止使用
hyt.stop();
Android 系统自带的是 CountDownTimer, 使用方法如下:
private CountDownTimer timer = null;
public void btnStartClick(View view) throws Exception {
if (timer != null) {
timer.cancel();
timer = null;
}
timer = new CountDownTimer(10000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
System.out.println((millisUntilFinished / 1000) + "秒后重发");
}
@Override
public void onFinish() {
Toast.makeText(MainActivity.this, "HHH CCC", Toast.LENGTH_SHORT).show();
}
};
timer.start();
}
Android系统自带的 CountDownTimer 使用确实很方便。