首先看效果图:
京东秒杀是两个小时一个场次,我们获取到场次后,通过场次+两个小时后,获取到最终的时间,拿最终时间的时间戳,与当前时间时间戳相减,求得剩余的小时,分钟,秒数,即可实现倒计时功能!
具体代码实现如下:
1.布局页面activity_seckill.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".SeckillActivity"> <TextView android:layout_width="match_parent" android:layout_height="40dp" android:gravity="center" android:text="仿京东秒杀" android:textColor="@color/black" android:textSize="20sp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:gravity="center" android:orientation="horizontal"> <TextView android:id="@+id/tv_screening" android:layout_width="100dp" android:layout_height="match_parent" android:gravity="center" android:text="几点场:" android:textColor="@color/black" android:textSize="15sp" /> <TextView android:id="@+id/tv_hours" android:layout_width="50dp" android:layout_height="40dp" android:background="@drawable/time_back" android:gravity="center" android:text="00" android:textColor="@color/white" android:textSize="15sp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=":" android:textColor="#fd5343" android:textSize="15sp" android:textStyle="bold" /> <TextView android:id="@+id/tv_minutes" android:layout_width="50dp" android:layout_height="40dp" android:background="@drawable/time_back" android:gravity="center" android:text="00" android:textColor="@color/white" android:textSize="15sp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=":" android:textColor="#fd5343" android:textSize="15sp" android:textStyle="bold" /> <TextView android:id="@+id/tv_second" android:layout_width="50dp" android:layout_height="40dp" android:background="@drawable/time_back" android:gravity="center" android:text="00" android:textColor="@color/white" android:textSize="15sp" /> </LinearLayout> </LinearLayout>
2.文本的背景文件为time_back.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#fd5343" /> <corners android:radius="10dp" /> </shape>
3.SeckillActivity类,具体注释已经在代码中给出
public class SeckillActivity extends AppCompatActivity { //秒杀场次 private TextView tv_screening; //2个小时一个秒杀场次,距离秒杀结束剩余多少小时 private TextView tv_hours; //剩余分钟数 private TextView tv_minutes; //剩余秒数 private TextView tv_second; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_seckill); tv_screening = findViewById(R.id.tv_screening); tv_hours = findViewById(R.id.tv_hours); tv_minutes = findViewById(R.id.tv_minutes); tv_second = findViewById(R.id.tv_second); //计算秒杀场次,两个小时一个场次 handler.sendEmptyMessage(0x00); } Handler handler = new Handler(new Handler.Callback() { @Override public boolean handleMessage(@NonNull Message msg) { if (msg.what == 0x00) { //设置时间 mkTime(); } handler.sendEmptyMessageDelayed(0x00, 1000); return true; } }); private void mkTime() { try { //使用给定的模式解析日期 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); StringBuilder stringBuilder = new StringBuilder(); String format = sdf.format(new Date()); //获取当前的年月日 String substring = format.substring(0, 11); stringBuilder.append(substring); //获取日历对象 Calendar calendar = Calendar.getInstance(); //获取一天中当前的小时数 24小时制的 int hours = calendar.get(Calendar.HOUR_OF_DAY); //获取秒杀场次 if (hours % 2 == 0) { tv_screening.setText(hours + "点场"); stringBuilder.append(hours + 2); } else { tv_screening.setText((hours - 1) + "点场"); stringBuilder.append(hours + 1); } stringBuilder.append(":00:00"); Date sessionDate = sdf.parse(stringBuilder.toString()); //获取秒杀场次+两个小时 的时间戳 long sessionDateTime = sessionDate.getTime(); //获取当前时间的时间戳 Date date = new Date(); long millisecond = date.getTime(); //间隔时间戳 long timestampMillisecond = sessionDateTime - millisecond; //剩余小时数 long hour = timestampMillisecond / (1000 * 60 * 60); //剩余分钟数 long minute = (timestampMillisecond - hour * (1000 * 60 * 60)) / (1000 * 60); //第①种方法: 获得剩余秒数 // long second = (timestampMillisecond - hour * (1000 * 60 * 60) - minute * (1000 * 60)) / 1000; //第②种方法: 获得剩余秒数 //取余数 得到的也是毫秒数 long test = timestampMillisecond % (60 * 1000); //剩余的秒数 Math.round按照四舍五入返回最接近参数的int型整数 long second = Math.round((float) (test / 1000)); tv_hours.setText("0" + hour); if (minute >= 10) { tv_minutes.setText(minute + ""); } else { tv_minutes.setText("0" + minute); } if (second >= 10) { tv_second.setText(second + ""); } else { tv_second.setText("0" + second); } } catch (Exception e) { e.printStackTrace(); } } }
以上就是京东秒杀的具体实现,如有不当之处,请指正~