模仿淘寶客戶端倒計(jì)時(shí)控件
來(lái)源:程序員人生 發(fā)布時(shí)間:2014-10-08 08:00:01 閱讀次數(shù):2898次
在前面的文章中,我們分析了淘寶android客戶端的一些界面實(shí)現(xiàn)和用戶體驗(yàn),今天這篇文章,主要介紹如何使用自定義控件,實(shí)現(xiàn)搶購(gòu)倒計(jì)時(shí)的功能。
首先,我們看一下實(shí)現(xiàn)的效果。

實(shí)現(xiàn)效果很簡(jiǎn)單哈,就是一個(gè)倒計(jì)時(shí)的自定義控件。
下面簡(jiǎn)單介紹一下實(shí)現(xiàn)的思路。
首先,顯示時(shí)間使用的是Textview,因?yàn)闆](méi)有很特殊的效果,因此,我們可以自己寫(xiě)一個(gè)簡(jiǎn)單的布局文件,來(lái)作為顯示的界面。
而關(guān)于時(shí)間的變更,我們使用timer類(lèi)就可以實(shí)現(xiàn),用一個(gè)1000毫秒的Timer,每過(guò)一秒,更新一下界面即可。
但是在更新時(shí)間的顯示數(shù)字的時(shí)候,有一個(gè)問(wèn)題需要注意,我的思路是用6個(gè)Textview來(lái)顯示時(shí)間,因此,時(shí)分秒的十位數(shù)字和個(gè)位數(shù)字需要單獨(dú)顯示,個(gè)位上顯示的數(shù)字是0-9,十位上顯示的數(shù)字范圍是0-5,所以需要分開(kāi)實(shí)現(xiàn)。
當(dāng)秒的十位個(gè)位都是0的時(shí)候,在過(guò)一秒,分的個(gè)位就要減一,這就涉及到借位的問(wèn)題,因此,每變更一次數(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: 開(kāi)始計(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í)長(zhǎng)
* @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。
項(xiàng)目地址
https://github.com/ZhaoKaiQiang/RushBuyCountDownTimerView
歡迎指教。
生活不易,碼農(nóng)辛苦
如果您覺(jué)得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)