简答的秒杀倒计时

时间:2022-01-05 17:09:20

简答的秒杀倒计时

CountTimeView

public class CountTimeView extends LinearLayout implements Runnable {

private TextView tv_hour;
private TextView tv_minute;
private TextView tv_second;
private onStopListener stopListener;
private Context context;


private long mHour, mMin, mSecond;//小时,分钟,秒
private boolean run=false; //是否启动了
private TextView sep_hour;
private TextView sep_second;
private TextView tv_pre;


public CountTimeView(Context context) {
super(context);
init(context);
}

public CountTimeView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}

private void init(Context context) {
this.context=context;
View view = View.inflate(context, R.layout.count_time_layout, this);
tv_hour = (TextView) view.findViewById(R.id.hour);
tv_minute = (TextView) view.findViewById(R.id.minute);
tv_second = (TextView) view.findViewById(R.id.second);
sep_hour = (TextView) view.findViewById(R.id.sep_hour);
sep_second = (TextView) view.findViewById(R.id.sep_second);
tv_pre = (TextView) view.findViewById(R.id.tv_pre);
}

/**
* 设置提示文字 如:离抢购结束还剩:
* @param text
*/

public void setPreText(String text){
tv_pre.setText(text);
}

/**
* 设置字体大小
* @param size
*/

public void setTextSize(int size){
size = dip2px(context, size);
tv_hour.setTextSize(size);
tv_minute.setTextSize(size);
tv_second.setTextSize(size);
sep_hour.setTextSize(size);
sep_second.setTextSize(size);
tv_pre.setTextSize(size+2);
}


/**
* 设置倒计时时间
* @param times 传一个数组,分别为小时,分钟,秒
*/

public CountTimeView setTimes(long[] times) {
mHour = times[0];
mMin = times[1];
mSecond = times[2];
return this;
}

/**
* 倒计时计算
*/

private void ComputeTime() {
mSecond--;
if (mSecond < 0) {
mMin--;
mSecond = 59;
if (mMin < 0) {
mMin = 59;
mHour--;
if (mHour <0) {
// 倒计时结束
mHour=0;
mSecond=0;
mMin=0;
stop();
}
}
}

}

public boolean isRun() {
return run;
}

/**
* 开始倒计时
*/

public void start() {
if(run)return;
if(mHour==0&&mMin==0&&mSecond==0)return;
this.run = true;
run();
}

/**
* 停止计时
*/

public void stop(){
this.run = false;
//倒计时完成监听
if(stopListener!=null){
stopListener.stopListener(mHour,mMin,mSecond);
}

if (mListRefreshList.size()>0) {
for (int i =0; i< mListRefreshList.size();i++) {
mListRefreshList.get(i).onRefreshListerner();
}
}

}


/**
* 在onDestroy方法移除回调
*/

public void removeCallbacks(){
removeCallbacks(this);
}

@Override
public void run() {
//标示已经启动
if(run){
ComputeTime();
setText();
postDelayed(this, 1000);
}else {
removeCallbacks(this);
}
}


private void setText(){
if(mHour<=9){
tv_hour.setText("0"+mHour);
}else {
tv_hour.setText(""+mHour);
}

if(mMin<=9){
tv_minute.setText("0"+mMin);

}else {
tv_minute.setText(""+mMin);
}

if(mSecond<=9){
tv_second.setText("0"+mSecond);
}else {
tv_second.setText(""+mSecond);
}
}

/**
* 设置倒计时完成的监听
* @param stopListener
*/

public void setOnStopListener(onStopListener stopListener){
this.stopListener=stopListener;
}


//********************************************************************************************************
private static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}

private List<IDataRefreshListener> mListRefreshList = new ArrayList<>();

public interface IDataRefreshListener {
public void onRefreshListerner();

}

public void addDataOnRefreshListener (IDataRefreshListener listener){
mListRefreshList.add(listener);
}

public void removeDataOnRefreshListener(IDataRefreshListener listener) {
mListRefreshList.remove(listener);
}

public interface onStopListener{
void stopListener(long hour, long min, long second);
}
}

count_time_layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
android:textColor="@color/small_gray_color"
android:textSize="13sp"
android:id="@+id/tv_pre"
android:text="距离抢购结束还有:"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<LinearLayout
android:layout_marginTop="8dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<TextView
android:text="00"
android:textSize="18sp"
android:textColor="#fff"
android:background="@drawable/shape_count_time"
android:id="@+id/hour"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<TextView
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textSize="18sp"
android:textColor="@color/gray_black"
android:textStyle="bold"
android:id="@+id/sep_hour"
android:text=":"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<TextView
android:text="00"
android:textSize="18sp"
android:textColor="#fff"
android:background="@drawable/shape_count_time"
android:id="@+id/minute"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<TextView
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textSize="18sp"
android:textColor="@color/gray_black"
android:textStyle="bold"
android:id="@+id/sep_second"
android:text=":"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<TextView
android:textSize="18sp"

android:text="00"
android:textColor="#fff"
android:background="@drawable/shape_count_time"
android:id="@+id/second"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>

</LinearLayout>

shape_count_time.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners
android:radius="3dp"
/>
<!-- 设置圆角半径 -->
<solid
android:color="@color/gray_black"/>
<!-- 填充的颜色 -->
<padding
android:left="5dp"
android:top="5dp"
android:right="5dp"
android:bottom="5dp"/>
<!-- 各方向的间隔 -->

</shape>

使用

 countTime.setTimes(new long[]{1,4,3}).start();