|
| 1 | +package common.base.views; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.graphics.Color; |
| 5 | +import android.support.annotation.ColorInt; |
| 6 | +import android.support.annotation.Nullable; |
| 7 | +import android.text.SpannableString; |
| 8 | +import android.util.AttributeSet; |
| 9 | + |
| 10 | +import common.base.utils.CheckUtil; |
| 11 | +import common.base.utils.CommonLog; |
| 12 | +import common.base.utils.CountdownTimerImpl; |
| 13 | +import common.base.utils.TimeUtil; |
| 14 | +import common.base.utils.Util; |
| 15 | + |
| 16 | +/** |
| 17 | + * *****************(^_^)***********************<br> |
| 18 | + * User: fee(QQ/WeiXin:1176610771)<br> |
| 19 | + * Date: 2020/2/12<br> |
| 20 | + * Time: 10:11<br> |
| 21 | + * <P>DESC: |
| 22 | + * 主动倒计时(按相应间隔时间递减)TextView |
| 23 | + * </p> |
| 24 | + * ******************(^_^)*********************** |
| 25 | + */ |
| 26 | +public class CountdownTextView extends CustomTextView implements CountdownTimerImpl.ICountdown{ |
| 27 | + private final String TAG = "CountdownTextView"; |
| 28 | + private CountdownTimerImpl countDownTimer; |
| 29 | + |
| 30 | + /** |
| 31 | + * 倒计时 前缀 文本 |
| 32 | + * eg.: 剩余时间: |
| 33 | + */ |
| 34 | + private CharSequence countdownPrefixWords; |
| 35 | + |
| 36 | + /** |
| 37 | + * 时、分、秒、背景颜色 |
| 38 | + * 如果有的话,需要处理 |
| 39 | + * def:0,透明,透明时不处理 |
| 40 | + */ |
| 41 | + private @ColorInt int hourMinuteSecondBgColor; |
| 42 | + |
| 43 | + /** |
| 44 | + * 时、分、秒、背景颜色 圆角的大小 |
| 45 | + * def = 0;如果为0,则不使用圆角矩形背景 |
| 46 | + */ |
| 47 | + private int bgColorSpanRadiusPxValue; |
| 48 | + /** |
| 49 | + * 时、分、秒、背景颜色 SPAN |
| 50 | + * 如果有的话,需要处理 |
| 51 | + */ |
| 52 | + private RadiusCornerBackgroundColorSpan colorSpan4HourText; |
| 53 | + private RadiusCornerBackgroundColorSpan colorSpan4MinuteText; |
| 54 | + private RadiusCornerBackgroundColorSpan colorSpan4SecondText; |
| 55 | + /** |
| 56 | + * 总时间,单位:毫秒 |
| 57 | + */ |
| 58 | + private long mMillisInFuture; |
| 59 | + |
| 60 | + /** |
| 61 | + * The interval in millis that the user receives callbacks |
| 62 | + * 单位:毫秒 |
| 63 | + * 倒数间隔:即以多长的时间间隔倒数一次,eg.: 1000(即以1秒的时间间隔倒数一次) |
| 64 | + */ |
| 65 | + private long mCountdownInterval; |
| 66 | + |
| 67 | + protected boolean isDetachFromWindow = true; |
| 68 | + public CountdownTextView(Context context) { |
| 69 | + this(context,null); |
| 70 | + } |
| 71 | + |
| 72 | + public CountdownTextView(Context context, @Nullable AttributeSet attrs) { |
| 73 | + super(context, attrs); |
| 74 | + //自定义属性 |
| 75 | + |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * 开始倒数 |
| 80 | + * @param mMillisInFuture 总倒数的时间,单位:毫秒 |
| 81 | + * @param mCountdownInterval 每次倒数时间间隔,单位:毫秒 |
| 82 | + */ |
| 83 | + public void start(long mMillisInFuture, long mCountdownInterval) { |
| 84 | + boolean willRestart = false; |
| 85 | + if (mCountdownInterval <= 0 || mCountdownInterval > mMillisInFuture) {//该条件即包含了 |
| 86 | + // mMillisInFuture <= 0了 |
| 87 | + //这些个条件时,都不能执行任务 |
| 88 | + if (countDownTimer != null) { |
| 89 | + countDownTimer.cancel(); |
| 90 | + } |
| 91 | + onFinish(); |
| 92 | + countDownTimer = null; |
| 93 | + return; |
| 94 | + } |
| 95 | + if (this.mMillisInFuture != mMillisInFuture || this.mCountdownInterval != mCountdownInterval) { |
| 96 | + willRestart = true; |
| 97 | + } |
| 98 | + this.mMillisInFuture = mMillisInFuture; |
| 99 | + this.mCountdownInterval = mCountdownInterval; |
| 100 | + if (willRestart) { |
| 101 | + if (countDownTimer != null) { |
| 102 | + countDownTimer.cancel(); |
| 103 | + } |
| 104 | + countDownTimer = null; |
| 105 | + } |
| 106 | + if (countDownTimer == null) { |
| 107 | + countDownTimer = new CountdownTimerImpl(mMillisInFuture, mCountdownInterval); |
| 108 | + countDownTimer.setCountdown(this); |
| 109 | + onTick(mMillisInFuture); |
| 110 | + countDownTimer.start(); |
| 111 | + } |
| 112 | + else { |
| 113 | + //如果不 为 null(可能时间参数一致的情况下),不需要 再start() |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + protected void onDetachedFromWindow() { |
| 119 | + isDetachFromWindow = true; |
| 120 | + super.onDetachedFromWindow(); |
| 121 | + log("onDetachedFromWindow()"); |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + protected void onAttachedToWindow() { |
| 126 | + isDetachFromWindow = false; |
| 127 | + super.onAttachedToWindow(); |
| 128 | + log("onAttachedToWindow()"); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * 倒数结束 |
| 133 | + */ |
| 134 | + @Override |
| 135 | + public void onFinish() { |
| 136 | + log("onFinish()"); |
| 137 | + showCountdownResultInfo(0); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * 一次倒数回调 |
| 142 | + * @param leftMillisUntilFinished 倒数后剩下的毫秒数 |
| 143 | + */ |
| 144 | + @Override |
| 145 | + public void onTick(long leftMillisUntilFinished) { |
| 146 | +// log("onTick() leftMillisUntilFinished = " + leftMillisUntilFinished); |
| 147 | + if (isDetachFromWindow) { |
| 148 | + return; |
| 149 | + } |
| 150 | + showCountdownResultInfo(leftMillisUntilFinished); |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * 显示倒数一次后的信息 |
| 155 | + * @param leftMillisUntilFinished 剩余的 时间,单位:毫秒 |
| 156 | + */ |
| 157 | + protected void showCountdownResultInfo(long leftMillisUntilFinished) { |
| 158 | + // TODO: 2020/2/13 ???是否要交给外部来决定怎么显示 |
| 159 | + int[] hms = TimeUtil.convertATotalMillisTime2Hms(leftMillisUntilFinished); |
| 160 | + //格式化时、分、秒 |
| 161 | + String formatHmsStr = Util.formatStr("%02d:%02d:%02d", hms[0], hms[1], hms[2]); |
| 162 | + |
| 163 | + String wholeShowText = formatHmsStr; |
| 164 | + if (!CheckUtil.isEmpty(countdownPrefixWords)) { |
| 165 | + wholeShowText = countdownPrefixWords + formatHmsStr; |
| 166 | + } |
| 167 | + if (hourMinuteSecondBgColor != 0) { |
| 168 | + //有背景颜色要处理 |
| 169 | + int prefixWordsLen = 0; |
| 170 | + if (countdownPrefixWords != null) { |
| 171 | + prefixWordsLen = countdownPrefixWords.length(); |
| 172 | + } |
| 173 | + int hourTextStartIndex = prefixWordsLen; |
| 174 | + int minuteTextStartIndex = hourTextStartIndex + 3; |
| 175 | + int secondTextStartIndex = minuteTextStartIndex + 3; |
| 176 | + SpannableString ss = new SpannableString(wholeShowText); |
| 177 | + ss.setSpan(colorSpan4HourText, hourTextStartIndex, |
| 178 | + hourTextStartIndex + 2, |
| 179 | + SpannableString.SPAN_INCLUSIVE_INCLUSIVE); |
| 180 | + |
| 181 | + ss.setSpan(colorSpan4MinuteText, minuteTextStartIndex, |
| 182 | + minuteTextStartIndex + 2, |
| 183 | + SpannableString.SPAN_INCLUSIVE_INCLUSIVE); |
| 184 | + |
| 185 | + ss.setSpan(colorSpan4SecondText, secondTextStartIndex, |
| 186 | + secondTextStartIndex + 2, |
| 187 | + SpannableString.SPAN_INCLUSIVE_INCLUSIVE); |
| 188 | + setText(ss); |
| 189 | + } |
| 190 | + else { |
| 191 | + setText(wholeShowText); |
| 192 | + } |
| 193 | + |
| 194 | + } |
| 195 | + |
| 196 | + private void log(String msg) { |
| 197 | + CommonLog.d(TAG, "---> " + msg); |
| 198 | + } |
| 199 | + |
| 200 | + public CharSequence getCountdownPrefixWords() { |
| 201 | + return countdownPrefixWords; |
| 202 | + } |
| 203 | + |
| 204 | + public void setCountdownPrefixWords(CharSequence countdownPrefixWords) { |
| 205 | + this.countdownPrefixWords = countdownPrefixWords; |
| 206 | + } |
| 207 | + |
| 208 | + |
| 209 | + public void setHourMinuteSecondBgColor(String hourMinuteSecondBgColor) { |
| 210 | + int theBgColor = 0; |
| 211 | + if (!CheckUtil.isEmpty(hourMinuteSecondBgColor)) { |
| 212 | + try { |
| 213 | + theBgColor = Color.parseColor(hourMinuteSecondBgColor); |
| 214 | + } catch (Exception ignore) { |
| 215 | + } |
| 216 | + } |
| 217 | + if (theBgColor != this.hourMinuteSecondBgColor) { |
| 218 | + this.hourMinuteSecondBgColor = theBgColor; |
| 219 | + buildColorSpans(); |
| 220 | + } |
| 221 | + } |
| 222 | + |
| 223 | + public void setHourMinuteSecondBgColor(@ColorInt int hourMinuteSecondBgColor) { |
| 224 | + if (this.hourMinuteSecondBgColor != hourMinuteSecondBgColor) { |
| 225 | + this.hourMinuteSecondBgColor = hourMinuteSecondBgColor; |
| 226 | + buildColorSpans(); |
| 227 | + } |
| 228 | + } |
| 229 | + |
| 230 | + public void setColorBgCornerRadius(int radius) { |
| 231 | + this.bgColorSpanRadiusPxValue = radius; |
| 232 | + } |
| 233 | + private void buildColorSpans() { |
| 234 | + if (this.hourMinuteSecondBgColor != 0) { |
| 235 | + colorSpan4HourText = new RadiusCornerBackgroundColorSpan(hourMinuteSecondBgColor,bgColorSpanRadiusPxValue); |
| 236 | + colorSpan4MinuteText = new RadiusCornerBackgroundColorSpan(hourMinuteSecondBgColor,bgColorSpanRadiusPxValue); |
| 237 | + colorSpan4SecondText = new RadiusCornerBackgroundColorSpan(hourMinuteSecondBgColor,bgColorSpanRadiusPxValue); |
| 238 | + } |
| 239 | + } |
| 240 | +} |
0 commit comments