用Android實(shí)現(xiàn)京東秒殺功能詳解
首先看效果圖:

京東秒殺是兩個(gè)小時(shí)一個(gè)場(chǎng)次,我們獲取到場(chǎng)次后,通過(guò)場(chǎng)次+兩個(gè)小時(shí)后,獲取到最終的時(shí)間,拿最終時(shí)間的時(shí)間戳,與當(dāng)前時(shí)間時(shí)間戳相減,求得剩余的小時(shí),分鐘,秒數(shù),即可實(shí)現(xiàn)倒計(jì)時(shí)功能!
具體代碼實(shí)現(xiàn)如下:
1.布局頁(yè)面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="幾點(diǎn)場(chǎng):"
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類,具體注釋已經(jīng)在代碼中給出
public class SeckillActivity extends AppCompatActivity {
//秒殺場(chǎng)次
private TextView tv_screening;
//2個(gè)小時(shí)一個(gè)秒殺場(chǎng)次,距離秒殺結(jié)束剩余多少小時(shí)
private TextView tv_hours;
//剩余分鐘數(shù)
private TextView tv_minutes;
//剩余秒數(shù)
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);
//計(jì)算秒殺場(chǎng)次,兩個(gè)小時(shí)一個(gè)場(chǎng)次
handler.sendEmptyMessage(0x00);
}
Handler handler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(@NonNull Message msg) {
if (msg.what == 0x00) {
//設(shè)置時(shí)間
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());
//獲取當(dāng)前的年月日
String substring = format.substring(0, 11);
stringBuilder.append(substring);
//獲取日歷對(duì)象
Calendar calendar = Calendar.getInstance();
//獲取一天中當(dāng)前的小時(shí)數(shù) 24小時(shí)制的
int hours = calendar.get(Calendar.HOUR_OF_DAY);
//獲取秒殺場(chǎng)次
if (hours % 2 == 0) {
tv_screening.setText(hours + "點(diǎn)場(chǎng)");
stringBuilder.append(hours + 2);
} else {
tv_screening.setText((hours - 1) + "點(diǎn)場(chǎng)");
stringBuilder.append(hours + 1);
}
stringBuilder.append(":00:00");
Date sessionDate = sdf.parse(stringBuilder.toString());
//獲取秒殺場(chǎng)次+兩個(gè)小時(shí) 的時(shí)間戳
long sessionDateTime = sessionDate.getTime();
//獲取當(dāng)前時(shí)間的時(shí)間戳
Date date = new Date();
long millisecond = date.getTime();
//間隔時(shí)間戳
long timestampMillisecond = sessionDateTime - millisecond;
//剩余小時(shí)數(shù)
long hour = timestampMillisecond / (1000 * 60 * 60);
//剩余分鐘數(shù)
long minute = (timestampMillisecond - hour * (1000 * 60 * 60)) / (1000 * 60);
//第①種方法: 獲得剩余秒數(shù)
// long second = (timestampMillisecond - hour * (1000 * 60 * 60) - minute * (1000 * 60)) / 1000;
//第②種方法: 獲得剩余秒數(shù)
//取余數(shù) 得到的也是毫秒數(shù)
long test = timestampMillisecond % (60 * 1000);
//剩余的秒數(shù) Math.round按照四舍五入返回最接近參數(shù)的int型整數(shù)
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();
}
}
}
以上就是京東秒殺的具體實(shí)現(xiàn)
總結(jié)
到此這篇關(guān)于用Android實(shí)現(xiàn)京東秒殺功能詳解的文章就介紹到這了,更多相關(guān)Android京東秒殺功能內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
android使用flutter的ListView實(shí)現(xiàn)滾動(dòng)列表的示例代碼
現(xiàn)如今打開(kāi)一個(gè) App,比如頭條、微博,都會(huì)有長(zhǎng)列表,那么android使用flutter的ListView滾動(dòng)列表如何實(shí)現(xiàn),本文就來(lái)詳細(xì)的介紹一下,感興趣的同學(xué)可以來(lái)了解一下2018-12-12
Android編程開(kāi)發(fā)之RadioGroup用法實(shí)例
這篇文章主要介紹了Android編程開(kāi)發(fā)之RadioGroup用法,結(jié)合實(shí)例形式分析了Android中RadioGroup單選按鈕的具體使用技巧,需要的朋友可以參考下2015-12-12
Android簡(jiǎn)單實(shí)現(xiàn) 緩存數(shù)據(jù)
這篇文章主要介紹了Android簡(jiǎn)單實(shí)現(xiàn) 緩存數(shù)據(jù),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11
ListView的Adapter使用(綁定數(shù)據(jù)) 之 自定義每一項(xiàng)的布局去綁定數(shù)據(jù)
之前寫的綁定數(shù)據(jù)是只是簡(jiǎn)單的綁定了字符串,這次我們將一次綁定多條數(shù)據(jù)并且嘗試用自定義的布局。在這篇文章中首先講解的是用Hashmap 去綁定數(shù)據(jù),第二個(gè)例子,講解自定義布局然后綁定數(shù)據(jù)2013-06-06
android中AutoCompleteTextView的簡(jiǎn)單用法(實(shí)現(xiàn)搜索歷史)
本篇文章主要介紹了android中AutoCompleteTextView的簡(jiǎn)單用法(自動(dòng)提示),有需要的可以了解一下。2016-11-11

