Android仿京东首页秒杀倒计时

本文实例为大家分享了Android仿京东首页秒杀倒计时的具体代码,供大家参考,具体内容如下

xml配置

<LinearLayout
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:background="#FFFFFF"
  android:orientation="vertical">
  <LinearLayout
  android:layout_width="match_parent"
  android:layout_height="40dp"
  android:gravity="center_vertical">
  <TextView
   android:id="@+id/tv_miaosha"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_marginLeft="8dp"
   android:text="京东秒杀"
   android:textColor="#f00"
   android:textSize="20sp" />
  <TextView
   android:id="@+id/tv_miaosha_time"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:padding="5dp"
   android:text="12点场"
   android:textSize="20sp" />
  <LinearLayout
   android:layout_width="wrap_content"
   android:layout_height="wrap_content">
   <TextView
   android:id="@+id/tv_miaosha_shi"
   android:layout_width="25dp"
   android:layout_height="25dp"
   android:background="@drawable/shape_miaosha_time"
   android:gravity="center"
   android:text="1"
   android:textColor="#fff"
   android:textSize="15sp" />
   <TextView
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:padding="3dp"
   android:text=":" />
   <TextView
   android:id="@+id/tv_miaosha_minter"
   android:layout_width="25dp"
   android:layout_height="25dp"
   android:background="@drawable/shape_miaosha_time"
   android:gravity="center"
   android:text="1"
   android:textColor="#fff"
   android:textSize="15sp" />
   <TextView
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:padding="3dp"
   android:text=":" />
   <TextView
   android:id="@+id/tv_miaosha_second"
   android:layout_width="25dp"
   android:layout_height="25dp"
   android:background="@drawable/shape_miaosha_time"
   android:gravity="center"
   android:text="1"
   android:textColor="#fff"
   android:textSize="15sp" />
  </LinearLayout>
 </LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
 android:shape="rectangle">
 <solid android:color="#000"></solid>
 <corners android:radius="2.5dp"></corners>
</shape>

方法内容:

//得到控件
 mMiaoshaShiTv = view.findViewById(R.id.tv_miaosha_shi);
 mMiaoshaTimeTv = view.findViewById(R.id.tv_miaosha_time);
 mMiaoshaMinterTv = view.findViewById(R.id.tv_miaosha_minter);
 mMiaoshaSecondTv = view.findViewById(R.id.tv_miaosha_second);

 /****************京东秒杀倒计时**********************/

 //使用handler用于更新UI
 private Handler handler = new Handler() {
 @Override
 public void handleMessage(Message msg) {
  super.handleMessage(msg);
  countDown();
  handler.sendEmptyMessageDelayed(0, 1000);
 }
 };

 /**
 * 秒杀
 */
 private void countDown() {
 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 Date curDate = new Date(System.currentTimeMillis());
 String format = df.format(curDate);
 StringBuffer buffer = new StringBuffer();
 String substring = format.substring(0, 11);
 buffer.append(substring);
 Log.d("ccc", substring);
 Calendar calendar = Calendar.getInstance();
 int hour = calendar.get(Calendar.HOUR_OF_DAY);
 if (hour % 2 == 0) {
  mMiaoshaTimeTv.setText(hour + "点场");
  buffer.append((hour + 2));
  buffer.append(":00:00");
 } else {
  mMiaoshaTimeTv.setText((hour - 1) + "点场");
  buffer.append((hour + 1));
  buffer.append(":00:00");
 }
 String totime = buffer.toString();
 try {
  java.util.Date date = df.parse(totime);
  java.util.Date date1 = df.parse(format);
  long defferenttime = date.getTime() - date1.getTime();
  long days = defferenttime / (1000 * 60 * 60 * 24);
  long hours = (defferenttime - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
  long minute = (defferenttime - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);
  long seconds = defferenttime % 60000;
  long second = Math.round((float) seconds / 1000);
  mMiaoshaShiTv.setText("0" + hours + "");
  if (minute >= 10) {
  mMiaoshaMinterTv.setText(minute + "");
  } else {
  mMiaoshaMinterTv.setText("0" + minute + "");
  }
  if (second >= 10) {
  mMiaoshaSecondTv.setText(second + "");
  } else {
  mMiaoshaSecondTv.setText("0" + second + "");
  }
 } catch (ParseException e) {
  e.printStackTrace();
 }
 }

 /***************京东秒杀倒计时****************/

开启倒计时:

handler.sendEmptyMessage(0);

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Android通过实现GridView的横向滚动实现仿京东秒杀效果

    实现GridView的横向滚动 效果如下图: 具体实现的代码 •1. 主界面布局代码:activity_main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent"

  • Android实现京东秒杀界面

    本文实例为大家分享了Android实现京东秒杀界面展示的具体代码,供大家参考,具体内容如下 效果图: 京东秒杀是两个小时一个场次,判断本机的时间进行场次定时,然后在这两个小时里面进行倒计时. MainActivity package com.bwie.com.myapplication; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; im

  • Android 仿京东秒杀倒计时代码

    效果图如下所示: 由于我仿的京东是分模块的,所以,这次主要描述秒杀模块! 首先设置好时间的背景 drawable文件下创建shape_miaosha_time.xml <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectang

  • Android开发必备:秒杀真机超快模拟器Genymotion介绍

    第一,这货速度太快,第二,模仿真机环境,第三,秒杀任何Android模拟器包括真机,不多说上图,我忒忙! 官网: http://www.genymotion.com/ 镜像图片可以创建多个模拟器 关键是有一切Google的服务,在Google眼里就是一个真机 骚年不知道外界的Android,已经发生了翻天覆地的变化!

  • Android定时器和倒计时实现淘宝秒杀功能

    本文实例为大家分享了Android实现淘宝秒杀的具体代码,供大家参考,具体内容如下 目录结构 效果图: imageViewHolder public class imageViewHolder extends RecyclerView.ViewHolder { public ImageView imageView; public imageViewHolder(View itemView) { super(itemView); imageView = (ImageView) itemView;

  • Android仿京东首页秒杀倒计时

    本文实例为大家分享了Android仿京东首页秒杀倒计时的具体代码,供大家参考,具体内容如下 xml配置 <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#FFFFFF" android:orientation="vertical"> <Linea

  • JavaScript仿京东实现秒杀倒计时案例详解

    功能介绍: 1.这个倒计时是不断变化的,因此需要定时器来自动变化(setInterval) 2.三个黑色的盒子,分别存放时.分秒 3.三个盒子利用innerHTML存入倒计时 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content=&quo

  • Android仿京东首页画轴效果

    记得之前京东首页有一个效果,有一个画轴,然后可以滚动画轴,去打开画(不知道怎么去形容这个效果,就叫做画轴效果吧- -!),然后去做相关操作,刚开始看到这个效果,想法是动态的去改变一个ImageView的高度,基本效果也就出来了,不过滚动部分的内容,当时接触的也不是很多,只是看过一些大牛的博客,略微了解了一点,当时也忙着写项目,也就没去多想,前些天忽然想到这个效果,就想着实现一下,不过京东新版本好像去掉这个东西了,只能凭着自己的记忆来大概搞一下,也是对滑动这部分内容的一个小练习吧. 先看一下效果图

  • Android 仿京东商城底部布局的选择效果(Selector 选择器的实现)

    京东商城的底部布局的选择效果看上去很复杂,其实很简单,这主要是要感谢 selector 选择器,本文将讲解仿照京东商城的底部布局的选择效果,如何实现 selector 选择器,在不同的状态下,给 view 设置不同的背景. 京东商城底部布局的选择效果如下. View主要的几种状态 主要状态有8种,设置状态的代码以及相应的含义如下. android:state_pressed = "true/false" //true表示按下状态,false表示非按下状态. android:state_

  • Android仿京东手机端类别页

    京东手机端的类别标签页, 是一个左侧滑动可选择类别, 右侧一个类别明细的列表联动页面. 当用户选择左侧选项, 可在右侧显示更多选项来选择. 实现方式也不少. 最常见的当然是左侧和右侧各一个Fragment, 左侧Fragment放置ListView, 右侧放显示类别明细的Fragment. 如果觉得页面包含的Fragment太多, 左侧直接给一个ListView就可以了.不影响效果. 效果图: 例子中值得注意的三点: 左侧列表点击某个Item可以自动上下滑动,使所点击的item自动移至列表中间

  • Android仿京东分类模块左侧分类条目效果

    本文实例为大家分享了Android仿京东左侧分类条目效果的具体代码,供大家参考,具体内容如下 import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.LinearLayout; import android.widget.ScrollView; import android.widget.TextView; import com.frame.R;

  • Android实现京东首页效果

    本文实例为大家分享了Android实现京东首页效果的具体代码,供大家参考,具体内容如下 1.效果图: 2.布局 源码链接 <?xml version="1.0" encoding="utf-8"?> <com.scwang.smart.refresh.layout.SmartRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns

  • Android仿京东淘宝自动无限循环轮播控件思路详解

    在App的开发中,很多的时候都需要实现类似京东淘宝一样的自动无限轮播的广告栏,所以就自己写了一个,下面是我自定义控件的思路和过程. 一.自定义控件属性 新建自定义控件SliderLayout继承于RelativeLayout,首先要考虑的就是自定义的控件需要扩展那些属性,把这些属性列出来.在这里是要实现类似于京东淘宝的无限轮播广告栏,那么首先想到的就是轮播的时长.轮播指示器的样式等等.我在这里列举了一些并且结合到了代码中. 1.扩展属性 (1)是否开启自动轮播的功能. (2)指示器的图形样式,一

  • Android仿QQ首页ListView左滑置顶、删除功能

    Android 仿QQ首页ListView左滑置顶.删除等实现源码,具体内容如下 效果图 实现源码:package com.duguang.baseanimation.ui.listivew.deletelistview; import android.content.Context; import android.util.AttributeSet; import android.util.Log; import android.util.TypedValue; import android.

随机推荐