关于倒计时的实现,通常有如下几个方法:http://blog.youkuaiyun.com/t12x3456/article/details/7816500
不过由于项目中展示效果为卡片,通过滑动的实行进行切换卡片,上文链接中所描述的几种都不适用,或者说使用效果不佳,需要每一秒都需要刷新所有卡片,导致卡片根本无法滑动,体验不佳,于是使用了自定义控件的方案,自定义一个TimerTextView,同时继承Runnable接口,覆写run方法,用于更新展现的时间,该方法的好处就是没必要每秒都刷新整个列表,而是使用TimerTextView自己更新自己的展现内容,滑动效果更流畅。
/**
* 自定义倒计时文本控件
*/
public class TimeTextView extends TextView implements Runnable {
private String[] times;
private long mhour, mmin, msecond;//天,小时,分钟,秒
private boolean run = false; //是否启动了
public TimeTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TimeTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public TimeTextView(Context context) {
super(context);
}
public String[] getTimes() {
return times;
}
public void setTimes(String[] times) {
this.times = times;
mhour = Long.parseLong(times[0].trim());
mmin = Long.parseLong(times[1].trim());
msecond = Long.parseLong(times[2].trim());
}
/**
* 倒计时计算
*/
private void ComputeTime() {
msecond--;
if (msecond < 0) {
mmin--;
msecond = 59;
if (mmin < 0) {
mmin = 59;
mhour--;
if (mhour < 0) {
// 倒计时结束
mhour = 59;
}
}
}
}
public boolean isRun() {
return run;
}
public void setRun(boolean run) {
this.run = run;
}
private String get2Num(long l) {
if (l < 10) {
return "0" + l;
} else return String.valueOf(l);
}
@Override
public void run() {
//标示已经启动
removeCallbacks(this);
if (!run) {
return;
}
ComputeTime();
Log.i("wj", "TextTimer正在运行");
String strTime = get2Num(mhour) + ":" + get2Num(mmin) + ":" + get2Num(msecond);
this.setText(Html.fromHtml(strTime));
postDelayed(this, 1000);
}
}
在此有几点需要说明:
1、该方法会针对每一个Item开启一个线程,于是初始化的时候需要默认不启动,设置run为false;
2、当多个都在计时的时候,需要计时停止未展现的项,否则线程越来越多,内存会一直上涨;
3、每次都调用removeCallbacks(this),将当前现场从队列中清楚,目的还是避免内存上涨;