陈力
2023-06-09 cca5f79b3af36e5a908c5dfecbd30110febe3baa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package com.lotaai.canguiayw.smiledialog;
 
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.TextView;
 
import androidx.annotation.Nullable;
 
import java.util.Timer;
import java.util.TimerTask;
 
@SuppressLint("AppCompatCustomView")
public class TimeRunTextView extends TextView {
    private long mHour;//小时
    private long mMin;//分钟
    private long mSecond;//秒
    private OnTimeViewListener timeViewListener;//时间结束
    private String MODE = "1";//时间展示模式
    private Timer timer = null;
    private TimerTask timerTask = null;
    private final Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            String getResult = (String) msg.obj;
            TimeRunTextView.this.setText(getResult);
        }
 
    };
 
 
    public TimeRunTextView(Context context) {
        super(context);
    }
 
    public TimeRunTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }
 
    public TimeRunTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
 
    public void setTimeViewListener(OnTimeViewListener timeViewListener) {
        this.timeViewListener = timeViewListener;
    }
 
    public void startTime(long timeCount, String mode) {
        initTime();//先清空时间的工具类
        if (!TextUtils.isEmpty(mode)) {
            this.MODE = mode;
        }
        startTime(timeCount);
    }
 
    public void changeTime(long timeCount){
        mHour = timeCount / (60 * 60);  //  对3600 取整  就是小时
        mMin = (timeCount % (60 * 60)) / 60;//  对3600 取余除以60 就是多出的分
        mSecond = timeCount % 60; //  对60 取余  就是多出的秒
    }
 
 
    public void startTime(long timeCount) {
        initTime();//先清空时间的工具类
        mHour = timeCount / (60 * 60);  //  对3600 取整  就是小时
        mMin = (timeCount % (60 * 60)) / 60;//  对3600 取余除以60 就是多出的分
        mSecond = timeCount % 60; //  对60 取余  就是多出的秒
        if (timer == null) {
            timer = new Timer();
        }
        if (timerTask == null) {
            timerTask = new TimerTask() {
                @Override
                public void run() {
                    if ((mHour + mMin + mSecond) > 0) {
                        ComputeTime();//计算时分秒
                    } else {
                        stopTime();
                    }
 
                }
            };
        }
        timer.schedule(timerTask, 0, 1000);
        if (timeViewListener != null) {
            timeViewListener.onTimeStart();
        }
    }
 
    public void stopTime() {
        try {
            if (timerTask != null) {
                timerTask.cancel();
                timerTask = null;
            }
            if (timer != null) {
                timer.cancel();
                timer = null;
            }
        } catch (Exception e) {
 
        }
        if (timeViewListener != null) {
            timeViewListener.onTimeEnd();
        }
    }
 
 
    public void initTime() {
        try {
            if (timerTask != null) {
                timerTask.cancel();
                timerTask = null;
            }
            if (timer != null) {
                timer.cancel();
                timer = null;
            }
        } catch (Exception e) {
 
        }
    }
 
 
    private void ComputeTime() {
        if (mSecond == 0) {
            //  秒为0  判断min 是否为0
            if (mMin == 0) {
                //  分为0 判断hour
                if (mHour == 0) {
                    Log.e("mcy--", "时间结束"); //  秒 分 时  都为0  倒计时结束
 
                } else {
                    //  此处为hour 不为0 秒 分 为0  所以 hour-- 秒 分 为 59
                    mHour--;
                    mMin = 59;
                    mSecond = 59;
                }
 
            } else {
                //  分不为0  所以不用修改 hour  分减一即可 秒 变为 59
                mMin--;
                mSecond = 59;
            }
 
        } else {
            //  秒不为0  秒减一 分 和时  不变 继续循环
            mSecond--;
        }
        String strTime = "";
        switch (MODE) {
            case "1":
                strTime = mHour + "时" + mMin + "分" + mSecond + "秒";
                break;
            case "2":
                strTime = mHour + ":" + mMin + ":" + mSecond;
                break;
            case "3":
                mSecond = mHour * 3600 + mMin * 60 + mSecond;
                mHour = 0;
                mMin = 0;
                strTime = mSecond + "s";
                break;
            default:
                strTime = mHour + "时" + mMin + "分" + mSecond + "秒";
                break;
        }
 
        Message msg = Message.obtain();
        msg.obj = strTime;   //从这里把你想传递的数据放进去就行了
        handler.sendMessage(msg);
 
    }
 
 
    public interface OnTimeViewListener {
        void onTimeStart();
 
        void onTimeEnd();
    }
 
 
}