Android實(shí)現(xiàn)淘寶客戶端倒計(jì)時(shí)界面
在前面的文章中,我們分析了淘寶android客戶端的一些界面實(shí)現(xiàn)和用戶體驗(yàn),今天這篇文章,主要介紹如何使用自定義控件,實(shí)現(xiàn)搶購倒計(jì)時(shí)的功能。
首先,我們看一下實(shí)現(xiàn)的效果。

實(shí)現(xiàn)效果很簡單哈,就是一個(gè)倒計(jì)時(shí)的自定義控件。
下面簡單介紹一下實(shí)現(xiàn)的思路。
首先,顯示時(shí)間使用的是Textview,因?yàn)闆]有很特殊的效果,因此,我們可以自己寫一個(gè)簡單的布局文件,來作為顯示的界面。
而關(guān)于時(shí)間的變更,我們使用timer類就可以實(shí)現(xiàn),用一個(gè)1000毫秒的Timer,每過一秒,更新一下界面即可。
但是在更新時(shí)間的顯示數(shù)字的時(shí)候,有一個(gè)問題需要注意,我的思路是用6個(gè)Textview來顯示時(shí)間,因此,時(shí)分秒的十位數(shù)字和個(gè)位數(shù)字需要單獨(dú)顯示,個(gè)位上顯示的數(shù)字是0-9,十位上顯示的數(shù)字范圍是0-5,所以需要分開實(shí)現(xiàn)。
當(dāng)秒的十位個(gè)位都是0的時(shí)候,在過一秒,分的個(gè)位就要減一,這就涉及到借位的問題,因此,每變更一次數(shù)字,都需要判斷是否需要借位。
具體的實(shí)現(xiàn)思路,大家還是看代碼吧。
/*
* Copyright (c) 2014, 青島司通科技有限公司 All rights reserved.
* File Name:RushBuyCountDownTimerView.java
* Version:V1.0
* Author:zhaokaiqiang
* Date:2014-9-26
*/
package com.qust.widght;
import java.util.Timer;
import java.util.TimerTask;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.qust.rushbuycountdowntimerview.R;
@SuppressLint("HandlerLeak")
public class RushBuyCountDownTimerView extends LinearLayout {
// 小時(shí),十位
private TextView tv_hour_decade;
// 小時(shí),個(gè)位
private TextView tv_hour_unit;
// 分鐘,十位
private TextView tv_min_decade;
// 分鐘,個(gè)位
private TextView tv_min_unit;
// 秒,十位
private TextView tv_sec_decade;
// 秒,個(gè)位
private TextView tv_sec_unit;
private Context context;
private int hour_decade;
private int hour_unit;
private int min_decade;
private int min_unit;
private int sec_decade;
private int sec_unit;
// 計(jì)時(shí)器
private Timer timer;
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
countDown();
};
};
public RushBuyCountDownTimerView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.view_countdowntimer, this);
tv_hour_decade = (TextView) view.findViewById(R.id.tv_hour_decade);
tv_hour_unit = (TextView) view.findViewById(R.id.tv_hour_unit);
tv_min_decade = (TextView) view.findViewById(R.id.tv_min_decade);
tv_min_unit = (TextView) view.findViewById(R.id.tv_min_unit);
tv_sec_decade = (TextView) view.findViewById(R.id.tv_sec_decade);
tv_sec_unit = (TextView) view.findViewById(R.id.tv_sec_unit);
}
/**
*
* @Description: 開始計(jì)時(shí)
* @param
* @return void
* @throws
*/
public void start() {
if (timer == null) {
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
handler.sendEmptyMessage(0);
}
}, 0, 1000);
}
}
/**
*
* @Description: 停止計(jì)時(shí)
* @param
* @return void
* @throws
*/
public void stop() {
if (timer != null) {
timer.cancel();
timer = null;
}
}
/**
* @throws Exception
*
* @Description: 設(shè)置倒計(jì)時(shí)的時(shí)長
* @param
* @return void
* @throws
*/
public void setTime(int hour, int min, int sec) {
if (hour >= 60 || min >= 60 || sec >= 60 || hour < 0 || min < 0
|| sec < 0) {
throw new RuntimeException("Time format is error,please check out your code");
}
hour_decade = hour / 10;
hour_unit = hour - hour_decade * 10;
min_decade = min / 10;
min_unit = min - min_decade * 10;
sec_decade = sec / 10;
sec_unit = sec - sec_decade * 10;
tv_hour_decade.setText(hour_decade + "");
tv_hour_unit.setText(hour_unit + "");
tv_min_decade.setText(min_decade + "");
tv_min_unit.setText(min_unit + "");
tv_sec_decade.setText(sec_decade + "");
tv_sec_unit.setText(sec_unit + "");
}
/**
*
* @Description: 倒計(jì)時(shí)
* @param
* @return boolean
* @throws
*/
private void countDown() {
if (isCarry4Unit(tv_sec_unit)) {
if (isCarry4Decade(tv_sec_decade)) {
if (isCarry4Unit(tv_min_unit)) {
if (isCarry4Decade(tv_min_decade)) {
if (isCarry4Unit(tv_hour_unit)) {
if (isCarry4Decade(tv_hour_decade)) {
Toast.makeText(context, "時(shí)間到了",
Toast.LENGTH_SHORT).show();
stop();
}
}
}
}
}
}
}
/**
*
* @Description: 變化十位,并判斷是否需要進(jìn)位
* @param
* @return boolean
* @throws
*/
private boolean isCarry4Decade(TextView tv) {
int time = Integer.valueOf(tv.getText().toString());
time = time - 1;
if (time < 0) {
time = 5;
tv.setText(time + "");
return true;
} else {
tv.setText(time + "");
return false;
}
}
/**
*
* @Description: 變化個(gè)位,并判斷是否需要進(jìn)位
* @param
* @return boolean
* @throws
*/
private boolean isCarry4Unit(TextView tv) {
int time = Integer.valueOf(tv.getText().toString());
time = time - 1;
if (time < 0) {
time = 9;
tv.setText(time + "");
return true;
} else {
tv.setText(time + "");
return false;
}
}
}
項(xiàng)目在我的github上,大家可以下載,也可以提交BUG。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android定時(shí)器和倒計(jì)時(shí)實(shí)現(xiàn)淘寶秒殺功能
- Android中使用TextView實(shí)現(xiàn)高仿京東淘寶各種倒計(jì)時(shí)效果
- android自定義倒計(jì)時(shí)控件示例
- android實(shí)現(xiàn)倒計(jì)時(shí)功能代碼
- Android實(shí)現(xiàn)計(jì)時(shí)與倒計(jì)時(shí)的常用方法小結(jié)
- Android實(shí)現(xiàn)加載廣告圖片和倒計(jì)時(shí)的開屏布局
- Android 實(shí)現(xiàn)閃屏頁和右上角的倒計(jì)時(shí)跳轉(zhuǎn)實(shí)例代碼
- Android賬號(hào)注冊(cè)實(shí)現(xiàn)點(diǎn)擊獲取驗(yàn)證碼倒計(jì)時(shí)效果
- Android自定義圓形倒計(jì)時(shí)進(jìn)度條
相關(guān)文章
淺談AnDroidDraw+DroidDraw實(shí)現(xiàn)Android程序UI設(shè)計(jì)的分析說明
本篇文章是對(duì)AnDroidDraw+DroidDraw實(shí)現(xiàn)Android程序UI設(shè)計(jì)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
ubuntu下 AndroidStudio4.1啟動(dòng)報(bào)錯(cuò)問題的解決
這篇文章主要介紹了ubuntu下 AndroidStudio4.1啟動(dòng)報(bào)錯(cuò)問題的解決,本文給大家分享個(gè)人經(jīng)驗(yàn)對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10
Android 使用AsyncTask實(shí)現(xiàn)斷點(diǎn)續(xù)傳
這篇文章主要介紹了Android 使用AsyncTask實(shí)現(xiàn)斷點(diǎn)續(xù)傳的實(shí)例代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2018-05-05
Android LaunchMode四種啟動(dòng)模式詳細(xì)介紹
這篇文章主要介紹了Android LaunchMode四種啟動(dòng)模式詳細(xì)介紹的相關(guān)資料,這里對(duì)launchmode的使用方法進(jìn)行了詳解及啟動(dòng)模式的比較,需要的朋友可以參考下2016-12-12
深入淺出RxJava+Retrofit+OkHttp網(wǎng)絡(luò)請(qǐng)求
本篇文章主要介紹了深入淺出RxJava+Retrofit+OkHttp網(wǎng)絡(luò)請(qǐng)求,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-11-11
Android使用ViewPager實(shí)現(xiàn)圖片滑動(dòng)預(yù)覽效果
這篇文章主要為大家詳細(xì)介紹了Android使用ViewPager實(shí)現(xiàn)圖片滑動(dòng)預(yù)覽效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
封裝的android監(jiān)聽手指左右滑動(dòng)屏幕的事件類分享
這篇文章主要介紹了封裝的android監(jiān)聽手指左右滑動(dòng)屏幕的事件類分享,本文分別給出了簡單處理方法的代碼和封裝好的處理類代碼,需要的朋友可以參考下2015-05-05
Android使用AsyncTask實(shí)現(xiàn)多線程下載的方法
這篇文章主要介紹了Android使用AsyncTask實(shí)現(xiàn)多線程下載的方法,以完整實(shí)例形式詳細(xì)分析了Android使用AsyncTask實(shí)現(xiàn)多線程下載的功能代碼,界面布局及權(quán)限控制的具體方法,需要的朋友可以參考下2016-03-03

