调用一次:updateCountDown()进行倒计时
1、倒计时:时/分/秒
// ===============================================================================
// 限时抢购倒计时
// ===============================================================================
private final int COUNTDOWN = 600; // 倒计时60 * 10 (秒)
public static long startTime = 0;
private Handler handler = new Handler();
/**
* 记录当前的时间值
*/
public static void recordCurrentTime()
{
startTime = new Date().getTime() / 1000;
}
// 显示当前时间值
private void showTime()
{
TextView2 mTextView2 = (TextView2) layout.findViewById(R.id.countDown_time);
if (startTime <= 0) recordCurrentTime(); // 获取初始时间值
// 倒计时
long countDown = COUNTDOWN - (new Date().getTime() / 1000 - startTime);
while(countDown < 0) // 循环倒计时
{
countDown += COUNTDOWN;
startTime += COUNTDOWN;
}
mTextView2.setText(Tools.secondFormat(countDown));
}
// 更新显示倒计时
private void updateCountDown()
{
handler.postDelayed(new Runnable()
{
@Override
public void run()
{
showTime();
if(!formExit) updateCountDown(); // 若界面未关闭则继续更新倒计时显示
}
}, 1000);
}
/**
* 秒数值格式化为 时/分/秒 字符串
*/
public static String secondFormat(long second)
{
String str = "";
long tmp = 0;
// 秒
tmp = second % 60;
str = (tmp < 10 ? "0" + tmp : tmp) + str;
// 分
second /= 60;
tmp = second % 60;
str = (tmp < 10 ? "0" + tmp : tmp) + ":" + str;
// 时
second /= 60;
tmp = second % 60;
str = (tmp < 10 ? "0" + tmp : tmp) + ":" + str;
return str;
}
2、倒计时 :分/秒/百分秒
// ===============================================================================
// 限时抢购倒计时, 分/秒/百分秒 —— 调用:updateCountDown()
// ===============================================================================
private Handler handler = new Handler(); // 用于更新倒计时
private final int UNIT = 10; // 最小时间尺寸,10ms
private final int COUNTDOWN = 600 * (1000 / UNIT); // 倒计时600 (秒)
public static long startTime = 0;
private View[] TimeView = null;
boolean formExit = false; // 标识界面是否关闭
/**
* 记录当前的时间值
*/
private void recordCurrentTime()
{
startTime = new Date().getTime() / UNIT;
}
// 显示当前时间值
private void showTime()
{
if (startTime <= 0) recordCurrentTime(); // 获取初始时间值
// 倒计时
long countDown = COUNTDOWN - (new Date().getTime() / UNIT - startTime);
while (countDown < 0) // 循环倒计时
{
countDown += COUNTDOWN;
startTime += COUNTDOWN;
}
if(TimeView == null) TimeView = timeView();
setView(TimeView, Format(countDown));
}
/** 设置时间值显示, 在V中显示时间time*/
private void setView(View[] V, int[] time)
{
// 数值0-9对应的图像资源
final int[] resId = { R.drawable.xuanren_xianshi0, R.drawable.xuanren_xianshi1, R.drawable.xuanren_xianshi2, R.drawable.xuanren_xianshi3,
R.drawable.xuanren_xianshi4, R.drawable.xuanren_xianshi5, R.drawable.xuanren_xianshi6, R.drawable.xuanren_xianshi7,
R.drawable.xuanren_xianshi8, R.drawable.xuanren_xianshi9 };
for (int i = 0; i < V.length; i++)
{
V[i].setBackgroundResource(resId[time[i]]);
}
}
/**
* 获取时间值,对应控件数组
*/
private View[] timeView()
{
final int[] resId = { R.id.time_num1, R.id.time_num2, R.id.time_num3, R.id.time_num4, R.id.time_num5, R.id.time_num6 };
View[] V = new View[resId.length];
for(int i=0; i<resId.length; i++)
{
V[i] = this.findViewById(resId[i]);
}
return V;
}
/**
* 秒数值格式化为 :分/秒/百分秒,数值数组
*/
private int[] Format(long second)
{
int[] time = new int[6];
long tmp = 0;
// 秒/100
tmp = second % (1000 / UNIT);
time[5] = (int) tmp % 10;
time[4] = (int) tmp / 10;
second /= (1000 / UNIT);
// 秒
tmp = second % 60;
time[3] = (int) tmp % 10;
time[2] = (int) tmp / 10;
second /= 60;
// 分
tmp = second % 60;
time[1] = (int) tmp % 10;
time[0] = (int) tmp / 10;
return time;
}
/** 更新显示倒计时 */
private void updateCountDown()
{
handler.postDelayed(new Runnable()
{
@Override
public void run()
{
showTime();
if (!formExit) updateCountDown(); // 若界面未关闭则继续更新倒计时显示
}
}, 30);
}
3、计时函数,简化调用
import java.util.Date;
import android.app.Activity;
import android.os.Handler;
import android.view.View;
import com.CL.CrazyRacing.game.R;
/**
* Timer 计时函数,集成倒计时逻辑
* -----
* 2015-11-20 下午2:42:49
* wangzhongyuan
*/
public class Timer
{
// ===============================================================================
// 限时抢购倒计时, 分/秒/百分秒
// 启动:Timer.start(Timer.getTimeView(root));
// 停止:Timer.stop();
// ===============================================================================
private static Handler handler; // 用于更新倒计时
private static View[] TimeView;
private static final int UNIT = 10; // 最小时间尺寸,10ms
private static final int COUNTDOWN = 600 * (1000 / UNIT); // 倒计时600 (秒)
public static long startTime = 0;
private static boolean running = false; // 标识界面是否关闭
/** 在控件组V上逐位显示倒计时时间值 */
public static void start(View[] V)
{
TimeView = V;
running = true;
updateCountDown();
}
/** 停止倒计时 */
public static void stop()
{
running = false;
}
/** 重新开始倒计时 */
public static void restart(View[] V)
{
startTime = 0;
start(V);
}
//--------------------------------------------------------------------------------
/** 更新显示倒计时 */
private static void updateCountDown()
{
if(handler == null) handler = new Handler();
handler.postDelayed(new Runnable()
{
@Override
public void run()
{
showTime();
if (running) updateCountDown(); // 若处于计时状态则继续更新倒计时显示
else
{
TimeView = null;
handler = null;
}
}
}, 30);
}
/**
* 记录当前的时间值
*/
private static void recordCurrentTime()
{
startTime = new Date().getTime() / UNIT;
}
// 显示当前时间值
private static void showTime()
{
if (startTime <= 0) recordCurrentTime(); // 获取初始时间值
// 倒计时
long countDown = COUNTDOWN - (new Date().getTime() / UNIT - startTime);
while (countDown < 0) // 循环倒计时
{
countDown += COUNTDOWN;
startTime += COUNTDOWN;
}
setView(TimeView, Format(countDown));
}
/** 设置时间值显示, 在V中显示时间time*/
private static void setView(View[] V, int[] time)
{
// 数值0-9对应的图像资源
final int[] resId = { R.drawable.xuanren_xianshi0, R.drawable.xuanren_xianshi1, R.drawable.xuanren_xianshi2, R.drawable.xuanren_xianshi3,
R.drawable.xuanren_xianshi4, R.drawable.xuanren_xianshi5, R.drawable.xuanren_xianshi6, R.drawable.xuanren_xianshi7,
R.drawable.xuanren_xianshi8, R.drawable.xuanren_xianshi9 };
for (int i = 0; i < V.length; i++)
{
V[i].setBackgroundResource(resId[time[i]]);
}
}
/**
* 获取,显示时间值的控件数组
*/
public static View[] getTimeView(Activity root)
{
final int[] resId = { R.id.time_num1, R.id.time_num2, R.id.time_num3, R.id.time_num4, R.id.time_num5, R.id.time_num6 };
View[] V = new View[resId.length];
for (int i = 0; i < resId.length; i++)
{
V[i] = root.findViewById(resId[i]);
}
return V;
}
/**
* 秒数值格式化为 :分/秒/百分秒,数值数组
*/
public static int[] Format(long second)
{
int[] time = new int[6];
long tmp = 0;
// 秒/100
tmp = second % (1000 / UNIT);
time[5] = (int) tmp % 10;
time[4] = (int) tmp / 10;
second /= (1000 / UNIT);
// 秒
tmp = second % 60;
time[3] = (int) tmp % 10;
time[2] = (int) tmp / 10;
second /= 60;
// 分
tmp = second % 60;
time[1] = (int) tmp % 10;
time[0] = (int) tmp / 10;
return time;
}
}