安卓倒计时(android.os.Handler.Handler()
2015-10-10 11:40
756 查看
调用一次: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; } }
相关文章推荐
- JavaScript 时分秒时间代码(自动补零)
- Oracle SQL Developer显示的时间包含时分秒的设置方法
- extjs 中时间控件中 时分秒
- Qt 正则表达式匹配 时分秒 00:00:00
- Axis2传递date类型参数丢失时分秒
- 时间操作(JavaScript版)—页面显示格式:年月日 时分秒 星期
- jquery.ui.datepicker 添加时分秒
- js根据秒显示时分秒倒计时
- Linux ll命令显示年月日 时分秒
- 按时分秒倒计时、且获取服务器时间、且可以同时多个倒计时
- Java将时间段秒数值转化为时分秒格式
- 商城倒计时(时分秒倒计时、分秒毫秒倒计时)
- ORACLE如何将6位时间转换为时分秒格式
- 正则 日期
- java 计算时间差,还剩下xx天xx小时xx分xx秒
- Mybatis插入时间没有时分秒只有年月日
- oracle查询数据时间显示时分秒格式
- 原生JS获取当前时间
- GridView中日期不显示时分秒的完美解决方法
- java Date获取年月日时分秒的实现方法