效果如下:

剩余 00:59:21

package com.bihu.advertiserapp.widgets;

import android.annotation.TargetApi;
import android.content.Context;
import android.icu.text.SimpleDateFormat;
import android.os.Handler;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.TextView; import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask; /** 倒计时控件
* Created by huqiang2 on 2016/12/7.
*/ public class CountdownTextView extends TextView { long mSeconds;
String mStrFormat;
Map<Integer,Timer> mTimerMap;
TimerTask mTimerTask;
final int what_count_down_tick = 1;
String TAG = "CountdownTextView";
public CountdownTextView(Context context) {
super(context);
} public CountdownTextView(Context context, AttributeSet attrs) {
super(context, attrs);
} public CountdownTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@TargetApi(21)
public CountdownTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
} /**
*
* @param format 例如:剩余%s
* @param seconds
*/
public void init(String format,long seconds){
mTimerMap = new HashMap<>();
if(!TextUtils.isEmpty(format)){
mStrFormat = format;
}
mSeconds = seconds; //设置总共的秒数
mTimerTask = new TimerTask() {
@Override
public void run() {
if(mSeconds > 0){
mSeconds --;
mHandler.sendEmptyMessage(what_count_down_tick);
}
}
};
}
public void start(int position){
if(mTimerMap.get(position) == null){
Timer timer = new Timer();
mTimerMap.put(position,timer);
mTimerMap.get(position).schedule(mTimerTask,0,1000);
}
}
private Handler mHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what){
case what_count_down_tick:
if(mSeconds <= 0){
setText(String.format(mStrFormat,"00:00:00"));
}else {
Log.e(TAG,"mSeconds="+mSeconds+"#what_count_down_tick:"+second2TimeSecond(mSeconds)+"#"+String.format(mStrFormat,second2TimeSecond(mSeconds)));
setText(mStrFormat== null ?second2TimeSecond(mSeconds):String.format(mStrFormat,second2TimeSecond(mSeconds)));
}
break;
}
}
}; @Override
public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
Log.e(TAG,"removeOnLayoutChangeListener");
super.removeOnLayoutChangeListener(listener);
} @Override
public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Log.e(TAG,"removeOnAttachStateChangeListener");
super.removeOnAttachStateChangeListener(listener);
} /**
* 转化为 hh:mm:ss 格式
* @param second
* @return
*/
private String second2TimeSecond(long second) {
long hours = second / 3600;
long minutes = (second%3600)/ 60;
long seconds = second % 60; String hourString = "";
String minuteString = "";
String secondString = "";
if(hours < 10){
hourString = "0";
if(hours == 0){
hourString += "0";
}else{
hourString += String.valueOf(hours);
}
}else{
hourString = String.valueOf(hours);
}
if (minutes < 10) {
minuteString = "0";
if (minutes == 0) {
minuteString += "0";
}else {
minuteString += String.valueOf(minutes);
}
}else{
minuteString = String.valueOf(minutes);
}
if(seconds < 10){
secondString = "0";
if (seconds == 0) {
secondString += "0";
}else {
secondString += String.valueOf(seconds);
}
}else {
secondString = String.valueOf(seconds);
}
return hourString + ":" + minuteString+":"+secondString;
}
}

最新文章

  1. CentOs 6.5 安装Ganglia步骤
  2. How To Handle a Loss of Confidence in Yourself
  3. dll 劫持
  4. 安卓系统上安装.net运行时 mono runtime
  5. 2016北京PHP39期 ThinkPHP Discuz Dedecms 微信开发视频教程
  6. TCP三次握手
  7. css居中的几种方式
  8. SQL Server高级内容之case语法函数
  9. 另一个 SqlParameterCollection 中已包含 SqlParameter
  10. Sort Colors —— LeetCode
  11. SQL查询今天、昨天、7天内、30天
  12. Ubuntu开启ApacheRewrite功能
  13. SSM Spring+SpringMVC+mybatis+maven+mysql环境搭建
  14. Some notes in Stanford CS106A(4)
  15. spring简述
  16. Atitit 翻页功能的解决方案与版本历史 v4 r49
  17. SpringBoot与Dubbo整合的三种方式
  18. POJ 2115:C Looooops
  19. 数论——算数基本定理 - HDU 4497 GCD and LCM
  20. 手工执行sql tuning advisor和sql access advisor

热门文章

  1. JDK1.6新特性,网络增强(Networking features and enhancements)
  2. RecyclerView 与 ItemTouchHelper 实现拖拽效果
  3. VS2015中运行ASPX老项目出错HTTP Error 500.23 - Internal Server Error错误
  4. centos chroot使用
  5. 并发编程 – Concurrent 用户指南--转
  6. hdu 3371 Connect the Cities(最小生成树)
  7. [Windows Azure] Windows Azure Identity
  8. [Windows Azure] How to use the Windows Azure Blob Storage Service in .NET
  9. 【Android】Intent解读
  10. vue2 如何操作dom