android倒计时控件示例
更新时间:2016年03月08日 10:41:23 作者:SRian215
这篇文章主要为大家详细介绍了android倒计时控件示例,感兴趣的小伙伴们可以参考一下
本文为大家分享了android倒计时控件,供大家参考,具体代码如下
/* * Copyright (C) 2012 The * Project * All right reserved. * Version 1.00 2012-2-11 * Author veally@foxmail.com */ package com.ly.sxh.view; import android.content.Context; import android.database.ContentObserver; import android.os.Handler; import android.os.SystemClock; import android.provider.Settings; import android.util.AttributeSet; import android.widget.DigitalClock; import java.util.Calendar; /** * Custom digital clock * 倒计时控件 * * @author */ public class CustomDigitalClock extends DigitalClock { Calendar mCalendar; private final static String m12 = "h:mm aa"; private final static String m24 = "k:mm"; private FormatChangeObserver mFormatChangeObserver; private Runnable mTicker; private Handler mHandler; private long endTime; private ClockListener mClockListener; private boolean mTickerStopped = false; @SuppressWarnings("unused") private String mFormat; public CustomDigitalClock(Context context) { super(context); initClock(context); } public CustomDigitalClock(Context context, AttributeSet attrs) { super(context, attrs); initClock(context); } private void initClock(Context context) { if (mCalendar == null) { mCalendar = Calendar.getInstance(); } mFormatChangeObserver = new FormatChangeObserver(); getContext().getContentResolver().registerContentObserver(Settings.System.CONTENT_URI, true, mFormatChangeObserver); setFormat(); } @Override protected void onAttachedToWindow() { mTickerStopped = false; super.onAttachedToWindow(); mHandler = new Handler(); /** * requests a tick on the next hard-second boundary */ mTicker = new Runnable() { public void run() { if (mTickerStopped) return; long currentTime = System.currentTimeMillis(); if (currentTime / 1000 == endTime / 1000 - 5 * 60) { mClockListener.remainFiveMinutes(); } long distanceTime = endTime - currentTime; distanceTime /= 1000; if (distanceTime == 0) { setText("00:00:00"); onDetachedFromWindow(); mClockListener.timeEnd(); } else if (distanceTime < 0) { setText("00:00:00"); } else { setText(dealTime(distanceTime)); } invalidate(); long now = SystemClock.uptimeMillis(); long next = now + (1000 - now % 1000); mHandler.postAtTime(mTicker, next); } }; mTicker.run(); } /** * deal time string * * @param time * @return */ public static String dealTime(long time) { StringBuffer returnString = new StringBuffer(); long day = time / (24 * 60 * 60); long hours = (time % (24 * 60 * 60)) / (60 * 60); long minutes = ((time % (24 * 60 * 60)) % (60 * 60)) / 60; long second = ((time % (24 * 60 * 60)) % (60 * 60)) % 60; String dayStr = String.valueOf(day); String hoursStr = timeStrFormat(String.valueOf(hours)); String minutesStr = timeStrFormat(String.valueOf(minutes)); String secondStr = timeStrFormat(String.valueOf(second)); returnString.append(hoursStr).append(":").append(minutesStr).append(":").append(secondStr); return returnString.toString(); } /** * format time * * @param timeStr * @return */ private static String timeStrFormat(String timeStr) { switch (timeStr.length()) { case 1: timeStr = "0" + timeStr; break; } return timeStr; } @Override protected void onDetachedFromWindow() { super.onDetachedFromWindow(); mTickerStopped = true; } /** * Clock end time from now on. * * @param endTime */ public void setEndTime(long endTime) { this.endTime = endTime; } /** * Pulls 12/24 mode from system settings */ private boolean get24HourMode() { return android.text.format.DateFormat.is24HourFormat(getContext()); } private void setFormat() { if (get24HourMode()) { mFormat = m24; } else { mFormat = m12; } } private class FormatChangeObserver extends ContentObserver { public FormatChangeObserver() { super(new Handler()); } @Override public void onChange(boolean selfChange) { setFormat(); } } public void setClockListener(ClockListener clockListener) { this.mClockListener = clockListener; } public interface ClockListener { void timeEnd(); void remainFiveMinutes(); } }
希望本文所述对大家学习Android软件编程有所帮助。
相关文章
Android ListView长按弹出菜单二种实现方式示例
这篇文章主要介绍了Android ListView长按弹出菜单的方法,大家参考实现2013-11-11kotlin项目加入Glide图片加载库并使用GlideApp的方法
这篇文章主要给大家介绍了关于kotlin项目加入Glide图片加载库并使用GlideApp的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起看看吧2019-01-01android 仿微信demo——微信消息界面实现(服务端)
本系列文章主要介绍了微信小程序-阅读小程序实例(demo),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧,希望能给你们提供帮助2021-06-06Android OpenGLES2.0等腰直角三角形和彩色的三角形(三)
这篇文章主要为大家详细介绍了Android OpenGLES2.0等腰直角三角形和彩色的三角形,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2016-12-12
最新评论