Android 实现抢购倒计时功能的示例

 更新时间:2021年03月24日 10:53:35   作者:龙旋  
这篇文章主要介绍了Android 实现抢购倒计时功能的示例,帮助大家更好的理解和学习使用Android开发,感兴趣的朋友可以了解下

一、效果图

二、思路

算多少秒,秒数取余60,(满足分后剩下的秒数)
算多少分,秒数除60,再取余60 (总分数满足小时后剩下的分数)
算多少时,秒数除60,除60,再取余24 (总小时满足天后剩下的小时)
算多少天,秒数除60,除60,除24 等到的整数就是天数

三、实现步骤:

我们这里的时间格式为后台返回,格式为:

2021-12-24 00:00:00

1、时间转换的工具类

 //将年-月-天 时:分:秒转化为毫秒格式
 public static long residueTimeout(String endDate, String newDate) throws ParseException {

  SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  Date d1 = df.parse(endDate);
  Date d2 = df.parse(newDate);
  long diff = d1.getTime() - d2.getTime();

  return diff;
 }

 /*
  * 将毫秒转换成时间戳
  */
 public static String stampToDate(Long s) {
  String res;
  SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  Date date = new Date(s);
  res = simpleDateFormat.format(date);
  return res;
 }

2、时间倒计时工具类

package com.sjl.keeplive.huawei;
import android.os.CountDownTimer;

/**
 * 倒计时工具类
 */
public class CountDownTimerUtils {
 /**
  * 倒计时结束的回调接口
  */
 public interface FinishDelegate {
  void onFinish();
 }

 /**
  * 定期回调的接口
  */
 public interface TickDelegate {
  void onTick(long pMillisUntilFinished);
 }

 private final static long ONE_SECOND = 1000;
 /**
  * 总倒计时时间
  */
 private long mMillisInFuture = 0;
 /**
  * 定期回调的时间 必须大于0 否则会出现ANR
  */
 private long mCountDownInterval;
 /**
  * 倒计时结束的回调
  */
 private FinishDelegate mFinishDelegate;
 /**
  * 定期回调
  */
 private TickDelegate mTickDelegate;
 private MyCountDownTimer mCountDownTimer;

 /**
  * 获取 CountDownTimerUtils
  *
  * @return CountDownTimerUtils
  */
 public static CountDownTimerUtils getCountDownTimer() {
  return new CountDownTimerUtils();
 }

 /**
  * 设置定期回调的时间 调用{@link #setTickDelegate(TickDelegate)}
  *
  * @param pCountDownInterval 定期回调的时间 必须大于0
  * @return CountDownTimerUtils
  */
 public CountDownTimerUtils setCountDownInterval(long pCountDownInterval) {
  this.mCountDownInterval = pCountDownInterval;
  return this;
 }

 /**
  * 设置倒计时结束的回调
  *
  * @param pFinishDelegate 倒计时结束的回调接口
  * @return CountDownTimerUtils
  */
 public CountDownTimerUtils setFinishDelegate(FinishDelegate pFinishDelegate) {
  this.mFinishDelegate = pFinishDelegate;
  return this;
 }

 /**
  * 设置总倒计时时间
  *
  * @param pMillisInFuture 总倒计时时间
  * @return CountDownTimerUtils
  */
 public CountDownTimerUtils setMillisInFuture(long pMillisInFuture) {
  this.mMillisInFuture = pMillisInFuture;
  return this;
 }

 /**
  * 设置定期回调
  *
  * @param pTickDelegate 定期回调接口
  * @return CountDownTimerUtils
  */
 public CountDownTimerUtils setTickDelegate(TickDelegate pTickDelegate) {
  this.mTickDelegate = pTickDelegate;
  return this;
 }

 public void create() {
  if (mCountDownTimer != null) {
   mCountDownTimer.cancel();
   mCountDownTimer = null;
  }
  if (mCountDownInterval <= 0) {
   mCountDownInterval = mMillisInFuture + ONE_SECOND;
  }
  mCountDownTimer = new MyCountDownTimer(mMillisInFuture, mCountDownInterval);
  mCountDownTimer.setTickDelegate(mTickDelegate);
  mCountDownTimer.setFinishDelegate(mFinishDelegate);
 }

 /**
  * 开始倒计时
  */
 public void start() {
  if (mCountDownTimer == null) {
   create();
  }
  mCountDownTimer.start();
 }

 /**
  * 取消倒计时
  */
 public void cancel() {
  if (mCountDownTimer != null) {
   mCountDownTimer.cancel();
  }
 }

 private static class MyCountDownTimer extends CountDownTimer {
  private FinishDelegate mFinishDelegate;
  private TickDelegate mTickDelegate;

  /**
   * @param millisInFuture The number of millis in the future from the call
   *       to {@link #start()} until the countdown is done and {@link #onFinish()}
   *       is called.
   * @param countDownInterval The interval along the way to receive
   *       {@link #onTick(long)} callbacks.
   */
  public MyCountDownTimer(long millisInFuture, long countDownInterval) {
   super(millisInFuture, countDownInterval);
  }

  @Override
  public void onTick(long millisUntilFinished) {
   if (mTickDelegate != null) {
    mTickDelegate.onTick(millisUntilFinished);
   }
  }

  @Override
  public void onFinish() {
   if (mFinishDelegate != null) {
    mFinishDelegate.onFinish();
   }
  }

  void setFinishDelegate(FinishDelegate pFinishDelegate) {
   this.mFinishDelegate = pFinishDelegate;
  }

  void setTickDelegate(TickDelegate pTickDelegate) {
   this.mTickDelegate = pTickDelegate;
  }
 }
}

3、布局文件

 <LinearLayout
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="center"
  android:orientation="horizontal">

  <TextView
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="抢购倒计时:" />

  <TextView
   android:id="@+id/text_day"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:background="#ff0000"
   android:padding="5dp"
   android:text="00" />

  <TextView
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text=" 天 " />

  <TextView
   android:id="@+id/text_hour"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:background="#ff0000"
   android:padding="5dp"
   android:text="00" />

  <TextView
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text=" 时 " />

  <TextView
   android:id="@+id/text_minute"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:background="#ff0000"
   android:padding="5dp"
   android:text="00" />

  <TextView
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text=" 分 " />

  <TextView
   android:id="@+id/text_second"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:background="#ff0000"
   android:padding="5dp"
   android:text="00" />

  <TextView
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text=" 秒 " />

 </LinearLayout>

4、倒计时显示处理

 public static void liveDescCountTime(long ms, TextView tvDays, TextView tvHour, TextView tvMinutes, TextView tvSeconds) {
  long totalSeconds = ms / 1000;
  long seconds = totalSeconds % 60;
  long minutes = totalSeconds / 60 % 60;
  long hours = totalSeconds / 60 / 60 % 24;
  long days = totalSeconds / 60 / 60 / 24;

  String dayStr = "";
  if (days > 0) {
   if (days > 9) {
    dayStr += days + "";
   } else if (days > 0) {
    dayStr += "0" + days + "";
   } else {
    dayStr += "00";
   }
  } else {
   dayStr = "00";
  }
  tvDays.setText(dayStr);

  String hourStr = "";
  if (hours > 0) {
   if (hours > 9) {
    hourStr += hours + "";
   } else if (hours > 0) {
    hourStr += "0" + hours + "";
   } else {
    hourStr += "00";
   }
  } else {
   hourStr = "00";
  }
  tvHour.setText(hourStr);

  String minutesStr = "";
  if (minutes > 0) {
   if (minutes > 9) {
    minutesStr += minutes + "";
   } else if (minutes > 0) {
    minutesStr += "0" + minutes + "";
   } else {
    minutesStr += "00";
   }
  } else {
   minutesStr = "00";
  }
  tvMinutes.setText(minutesStr);

  String secondStr = "";
  if (minutes > 0) {
   if (seconds > 9) {
    secondStr += seconds;
   } else if (seconds > 0) {
    secondStr += "0" + seconds;
   } else {
    secondStr += "00";
   }
  } else {
   secondStr = "00";
  }
  tvSeconds.setText(secondStr);
 }

5、开始倒计时

        final TextView text_day = findViewById(R.id.text_day);
  final TextView text_hour = findViewById(R.id.text_hour);
  final TextView text_minute = findViewById(R.id.text_minute);
  final TextView text_second = findViewById(R.id.text_second);

  long residueTime = 0;
        //获取当前时间
  String stampToDate = stampToDate(System.currentTimeMillis());
  try {
            //2021-12-24 00:00:00为模拟倒计时间数据
   residueTime = residueTimeout("2021-12-24 00:00:00", stampToDate);
  } catch (ParseException e) {
   e.printStackTrace();
  }

        //倒计时
  CountDownTimerUtils.getCountDownTimer()
    .setMillisInFuture(residueTime)
    .setCountDownInterval(1000)
    .setTickDelegate(new CountDownTimerUtils.TickDelegate() {
     @Override
     public void onTick(long pMillisUntilFinished) {
      liveDescCountTime(pMillisUntilFinished, text_day, text_hour, text_minute, text_second);
     }
    })
    .setFinishDelegate(new CountDownTimerUtils.FinishDelegate() {
     @Override
     public void onFinish() {
      //倒计时完成后处理
     }
    }).start();

以上就是Android 实现抢购倒计时功能的示例的详细内容,更多关于Android 抢购倒计时功能的资料请关注脚本之家其它相关文章!

相关文章

  • Android Studio配置本地SDK的方法

    Android Studio配置本地SDK的方法

    这篇文章主要介绍了Android Studio配置本地SDK的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-11-11
  • Android图片缓存之Lru算法(二)

    Android图片缓存之Lru算法(二)

    LRU缓存简单的说就是缓存一定量的数据,当超过设定的阈值时就把一些过期的数据删除掉,这篇文章主要介绍了Android图片缓存Lru算法,感兴趣的小伙伴们可以参考一下
    2016-08-08
  • Android仿QQ空间动态界面分享功能

    Android仿QQ空间动态界面分享功能

    这篇文章主要介绍了Android仿QQ空间动态界面分享功能,本文图文并茂给大家介绍的非常详细,需要的朋友可以参考下
    2017-04-04
  • Android TextView高级显示技巧实例小结

    Android TextView高级显示技巧实例小结

    这篇文章主要介绍了Android TextView高级显示技巧,结合实例形式总结分析了Android TextView控件进行文字与图片显示的相关操作技巧,需要的朋友可以参考下
    2016-10-10
  • Android利用ViewDragHelper轻松实现拼图游戏的示例

    Android利用ViewDragHelper轻松实现拼图游戏的示例

    本篇文章主要介绍了Android利用ViewDragHelper轻松实现拼图游戏的示例,非常具有实用价值,需要的朋友可以参考下
    2017-11-11
  • Google大佬都用的广播goAsync源码分析

    Google大佬都用的广播goAsync源码分析

    这篇文章主要为大家介绍了Google大佬都用的广播 goAsync源码分析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-01-01
  • Android 实现高斯模糊效果且兼容低版本

    Android 实现高斯模糊效果且兼容低版本

    这篇文章主要介绍了Android 实现高斯模糊效果且兼容低版本的相关资料,本文图文并茂介绍的非常详细,需要的朋友可以参考下
    2016-09-09
  • 在Android中使用WebSocket实现消息通信的方法详解

    在Android中使用WebSocket实现消息通信的方法详解

    这篇文章主要介绍了在Android中使用WebSocket实现消息通信的方法详解,消息推送功能可以说移动APP不可缺少的功能之一,使用WebSocket实现消息推送功能。感兴趣的可以了解一下
    2020-07-07
  • Android实现PDF预览打印功能

    Android实现PDF预览打印功能

    这篇文章主要为大家详细介绍了Android实现PDF预览打印功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-12-12
  • 你值得拥有的Android Studio开发小技巧

    你值得拥有的Android Studio开发小技巧

    这篇文章主要为大家分享了值得拥有的Android Studio开发小技巧,介绍几个比较好用的技巧和快捷键,提升我们的编码效率,感兴趣的小伙伴们可以参考一下
    2016-06-06

最新评论