当前位置: 首页 > news >正文

自定义倒计时UI

创建

/**
* @author weimian
* 自定义倒计时文本控件
*/
public class TimeTextView extends TextView implements Runnable{
//Paint mPaint; //画笔,包含了画几何图形、文本等的样式和颜色信息
private long[] times;
private long mday, mhour, mmin, msecond;//天,小时,分钟,秒
private boolean run=false; //是否启动了
public TimeTextView(Context context, AttributeSet attrs) {
super(context, attrs);
// mPaint=new Paint();
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.TimeTextView);
array.recycle(); //一定要调用,否则这次的设定会对下次的使用造成影响
}
public TimeTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// mPaint=new Paint();
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.TimeTextView);
array.recycle(); //一定要调用,否则这次的设定会对下次的使用造成影响
}
public TimeTextView(Context context) {
super(context);
}
public long[] getTimes() {
return times;
}
public long[] getNowTimes(){//取得现在剩下的倒计时
if(times!=null){
times[0] = this.mday;
times[1] = this.mhour;
times[2] = this.mmin;
times[3] = this.msecond;
}
return times;
}
public void setTimes(long[] times) {
this.times = times;
mday = times[0];
mhour = times[1];
mmin = times[2];
msecond = times[3];
}
/**
* 倒计时计算
*/
private void ComputeTime() {
msecond--;
if (msecond < 0) {
mmin--;
msecond = 59;
if (mmin < 0) {
mmin = 59;
mhour--;
if (mhour < 0) {
// 倒计时结束
mhour = 23;
mday--;
}
}
}
}
public boolean isRun() {
return run;
}
public void setRun(boolean run) {
this.run = run;
}
@Override
public void run() {
//标识已经启动
run=true;
ComputeTime();
String strTime="倒计时:"+mday+""+"天"+mhour+"时"+mmin+"分"+msecond+"";
if(mday<0){
strTime = "已经结束了";
removeCallbacks(this);
}
this.setText(strTime);
postDelayed(this, 1000);
}
}

 

/**
* 分割时间
* @param endTime
* @return
*/
public static long[] getLongArray(String endTime,String now){
long[] arrayTime = null;
if(endTime!=null && !endTime.isEmpty()){
long it = Long.parseLong(endTime);
long nw = (System.currentTimeMillis()/1000);//默认客户端时间
if(now!=null && !now.isEmpty()){//当前时间则使用服务器时间
nw = Long.parseLong(now);
}
long difference=it-nw;
try {
if(difference>0){
long day=difference/(60*60*24);
long hour=difference%(60*60*24)/(60*60);
long min=difference%(60*60*24)%(60*60)/60;
long second=difference%(60*60*24)%(60*60)%60;
arrayTime = new long[4];
arrayTime[0] = day;
arrayTime[1] = hour;
arrayTime[2] = min;
arrayTime[3] = second;
return arrayTime;
}
} catch (Exception e) {
return arrayTime;
}
return arrayTime;
}
return arrayTime;
}

使用方式

long time_end=System.currentTimeMillis()+10*1000;//结束时间 10秒
long nowtime=System.currentTimeMillis();//当前时间
timeView.setTimes(getLongArray(time_end,nowtime));

if(!timeView.isRun()){
timeView.run();
}



http://www.jsqmd.com/news/816510/

相关文章:

  • 小满nestjs(第四章 装饰器实战:构建灵活可扩展的API)
  • 国内超声波镜面加工设备厂家权威实力实测排行盘点 - 奔跑123
  • 解锁 ActRIIA 酶活性抑制密码
  • 中山黄金回收优选:润富连锁VS零散小店,6店实力对比,为何本地人都选润富? - 润富黄金珠宝行
  • PDF不能编辑什么原因?去除编辑限制的方法有哪些?
  • 基于CW32L083 MCU的智能燃气表超低功耗与高可靠性设计实践
  • LeetCode 二维树状数组题解
  • 2026年义乌地板选购指南:实木地板与工厂直营深度评测 | 义乌地板批发实木三层实木多层木地板工厂直供15年超长质保 - 企业品牌优选推荐官
  • Python学习之包管理:pip + virtualenv(及 conda)
  • 叛逆期不是“麻烦期”:是孩子建立自我的关键期
  • 抖音图片怎么去水印文字?2026实测去水印文字方法+在线工具全整理
  • PS3游戏更新下载器:从索尼官方服务器获取游戏补丁的完整指南
  • Python 开发者如何通过 OpenAI 兼容协议快速接入 Taotoken
  • 群晖DSM 7.2.2视频解决方案:一键恢复Video Station完整功能
  • 2026年国内GEO服务商怎么选?这家百度系团队凭硬核技术连斩四项权威大奖,值得重点关注 - 深度智识库
  • 多FPGA原型验证:ASIC设计的关键技术与实践
  • VirtualWife虚拟数字人部署实战:从架构解析到私有化部署全攻略
  • 好用的青岛X射线探伤机服务商
  • 计算机组成原理期末救急:Cache地址划分(offset/index/tag)保姆级解题步骤
  • 标准化运营轻食加盟怎么选?2026热门品牌对比测评 - 品牌种草官
  • Cadence Allegro 17.2 PCB设计实战:从约束管理器到完成布局布线的保姆级避坑指南
  • 2026 网络推广平台综合测评:谁是 AI 时代企业营销增长的最优解? - 博客湾
  • LeetCode 树状数组应用题解
  • ARM系统寄存器ERXADDR解析与错误处理机制
  • BetterNCM安装器:让网易云音乐体验升级的智能管家
  • 城市移动机器人定位:单目视觉+低等级IMU+车轮里程计融合方案
  • 十六年技术沉淀,西恩士为何能成为 AI 液冷检测领域的破局者? - 工业干货社
  • 平移门电机厂家怎么选?专业选购指南帮你避坑 - 资讯速览
  • 杭州宝珀腕表抛光注意什么事项?五十噚/6654表壳划痕修复,别让“翻新”变“毁容”! - 亨得利官方维修中心
  • 展厅展览工程口碑好服务商