简单实现Android倒计时效果

本文实例为大家分享了Android倒计时效果的具体代码,供大家参考,具体内容如下

需求:

a.在后台添加时,如果是今日直播,则需要添加开始时间(精确到秒);

b.离开始时间超过1天,显示为:“离开时还有X天”;

c.离开时时间不到1天,显示为:“离开时还有XX:XX:XX”;

d.如果已经开始,显示为:“火热进行中”。

效果图:

/**

* 显示剩余时间

*/

private void setRestTime(final BaseViewHolder holder, IndexPicAdModel model) {

holder.setVisible(R.id.time, false);

holder.setVisible(R.id.dayTime, false);

if (null != timer) {

timer.cancel();

}

timer = new Timer();

timeRest = Long.valueOf(model.getStartTime()) - System.currentTimeMillis();

timer.schedule(new TimerTask() {

@Override

public void run() {

timeRest = timeRest - 1000;

final long day = timeRest / (24 * 60 * 60 * 1000);

final long hour = (timeRest / (60 * 60 * 1000) - day * 24);

final long min = ((timeRest / (60 * 1000)) - day * 24 * 60 - hour * 60);

final long second = (timeRest / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);

liveTodayHandler.post(new Runnable() {

@Override

public void run() {

//如果大于一天

if (day >= 1) {

holder.setText(R.id.day, day + "");

holder.setText(R.id.title, "离开始还有");

holder.setVisible(R.id.time, true);

holder.setVisible(R.id.dayTime, false);

} else if (timeRest <= 0) {

if (null != timer) {

timer.cancel();

}

holder.setText(R.id.title, "火热进行中");

holder.setVisible(R.id.time, false);

holder.setVisible(R.id.dayTime, false);

} else {

holder.setText(R.id.hour, (hour >= 10 ? hour + "" : "0" + hour));

holder.setText(R.id.min, (min >= 10 ? min + "" : "0" + min));

holder.setText(R.id.second, (second >= 10 ? second + "" : "0" + second));

holder.setText(R.id.title, "离开始还有");

holder.setVisible(R.id.time, false);

holder.setVisible(R.id.dayTime, true);

}

}

});

}

}, 0, 1000);

}

以上是 简单实现Android倒计时效果 的全部内容, 来源链接: utcz.com/z/336910.html

回到顶部