admin
2022-08-09 399ac289f80b7a40aa4210341db6b447cacdcf14
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
package com.tejia.lijin.app.util.view.countdown;
 
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
 
import com.tejia.lijin.app.R;
 
public class CountdownView extends View {
    private static final String DEFAULT_SUFFIX = ":";
    private static final float DEFAULT_SUFFIX_LR_MARGIN = 3; // dp
    private static final float DEFAULT_TIME_BG_DIVISION_LINE_SIZE = 0.5f; // dp
 
    private Context mContext;
    private int mDay, mHour, mMinute;
    private long mRemainTime;
    private OnCountdownEndListener mOnCountdownEndListener;
    private OnCountdownIntervalListener mOnCountdownIntervalListener;
    private CustomCountDownTimer mCustomCountDownTimer;
 
    private boolean isShowDay, isShowHour, isShowMinute;
    private boolean mHasSetIsShowDay, mHasSetIsShowHour;
    private boolean isHideTimeBackground;
    private boolean isShowTimeBgDivisionLine;
    private boolean isTimeTextBold;
    private boolean isSuffixTextBold;
 
    private Paint mTimeTextPaint;
    private Paint mSuffixTextPaint;
    private Paint mTimeTextBgPaint;
    private Paint mTimeTextBgDivisionLinePaint;
 
    private RectF mDayBgRectF, mHourBgRectF, mMinuteBgRectF;
 
    private float mTimeTextWidth;
    private float mTimeTextHeight;
    private float mTimeTextSize;
    private float mTimeBgSize;
    private int mTimeTextColor;
    private int mTimeBgColor;
    private float mTimeBgRadius;
    private int mTimeBgDivisionLineColor;
    private float mTimeBgDivisionLineSize;
    private float mTimeTextBaseY;
    private float mTimeBgDivisionLineYPos;
 
    private String mSuffix, mSuffixDay, mSuffixHour, mSuffixMinute;
    private int mSuffixTextColor;
    private float mSuffixTextSize;
    private float mSuffixDayTextWidth, mSuffixHourTextWidth,
            mSuffixMinuteTextWidth, mSuffixSecondTextWidth;
    private int mSuffixGravity;
    private float mSuffixLRMargin;
    private float mSuffixDayLeftMargin, mSuffixDayRightMargin;
    private float mSuffixHourLeftMargin, mSuffixHourRightMargin;
    private float mSuffixMinuteLeftMargin, mSuffixMinuteRightMargin;
    private float mSuffixSecondLeftMargin, mSuffixSecondRightMargin;
    private float mSuffixDayTextBaseline, mSuffixHourTextBaseline,
            mSuffixMinuteTextBaseline;
 
    private float mTempSuffixDayLeftMargin, mTempSuffixDayRightMargin;
    private float mTempSuffixHourLeftMargin, mTempSuffixHourRightMargin;
    private float mTempSuffixMinuteLeftMargin, mTempSuffixMinuteRightMargin;
    private float mTempSuffixSecondLeftMargin, mTempSuffixSecondRightMargin;
    private String mTempSuffixMinute;
 
    private float mTimeTextBaseline;
    private float mLeftPaddingSize;
    private float mTopPaddingSize;
    private int mContentAllWidth;
    private int mContentAllHeight;
    private int mViewWidth;
    private int mViewHeight;
    private int mTimeTextBottom;
 
    private float mDayTimeTextWidth;
    private float mDayTimeBgWidth;
    private boolean isDayLargeNinetyNine;
 
    private long mInterval;
    private long mPreviouIntervalCallbackTime;
    private boolean hasCustomSomeSuffix = false;
 
    public CountdownView(Context context) {
        this(context, null);
    }
 
    public CountdownView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
 
    public CountdownView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
 
        TypedArray ta = context.obtainStyledAttributes(attrs,
                R.styleable.CountdownView);
        mTimeBgColor = ta.getColor(R.styleable.CountdownView_timeBgColor,
                0xFF444444);
        mTimeBgRadius = ta.getDimension(R.styleable.CountdownView_timeBgRadius,
                0);
        isShowTimeBgDivisionLine = ta.getBoolean(
                R.styleable.CountdownView_isShowTimeBgDivisionLine, true);
        mTimeBgDivisionLineColor = ta.getColor(
                R.styleable.CountdownView_timeBgDivisionLineColor,
                Color.parseColor("#30FFFFFF"));
        mTimeBgDivisionLineSize = ta.getDimension(
                R.styleable.CountdownView_timeBgDivisionLineSize,
                dp2px(DEFAULT_TIME_BG_DIVISION_LINE_SIZE));
        mTimeBgSize = ta.getDimension(R.styleable.CountdownView_timeBgSize, 0);
 
        isTimeTextBold = ta.getBoolean(
                R.styleable.CountdownView_isTimeTextBold, false);
        mTimeTextSize = ta.getDimension(R.styleable.CountdownView_timeTextSize,
                sp2px(12));
        mTimeTextColor = ta.getColor(R.styleable.CountdownView_timeTextColor,
                0xFF000000);
        isHideTimeBackground = ta.getBoolean(
                R.styleable.CountdownView_isHideTimeBackground, true);
        isShowDay = ta.getBoolean(R.styleable.CountdownView_isShowDay, false);
        isShowHour = ta.getBoolean(R.styleable.CountdownView_isShowHour, false);
        isShowMinute = ta.getBoolean(R.styleable.CountdownView_isShowMinute,
                true);
 
        mHasSetIsShowDay = ta.hasValue(R.styleable.CountdownView_isShowDay);
        mHasSetIsShowHour = ta.hasValue(R.styleable.CountdownView_isShowHour);
 
        isSuffixTextBold = ta.getBoolean(
                R.styleable.CountdownView_isSuffixTextBold, false);
        mSuffixTextSize = ta.getDimension(
                R.styleable.CountdownView_suffixTextSize, sp2px(12));
        mSuffixTextColor = ta.getColor(
                R.styleable.CountdownView_suffixTextColor, 0xFF000000);
        mSuffix = ta.getString(R.styleable.CountdownView_suffix);
        mSuffixDay = ta.getString(R.styleable.CountdownView_suffixDay);
        mSuffixHour = ta.getString(R.styleable.CountdownView_suffixHour);
        mSuffixMinute = ta.getString(R.styleable.CountdownView_suffixMinute);
        mSuffixGravity = ta.getInt(R.styleable.CountdownView_suffixGravity, 1);
        mSuffixLRMargin = ta.getDimension(
                R.styleable.CountdownView_suffixLRMargin, -1);
        mSuffixDayLeftMargin = ta.getDimension(
                R.styleable.CountdownView_suffixDayLeftMargin, -1);
        mSuffixDayRightMargin = ta.getDimension(
                R.styleable.CountdownView_suffixDayRightMargin, -1);
        mSuffixHourLeftMargin = ta.getDimension(
                R.styleable.CountdownView_suffixHourLeftMargin, -1);
        mSuffixHourRightMargin = ta.getDimension(
                R.styleable.CountdownView_suffixHourRightMargin, -1);
        mSuffixMinuteLeftMargin = ta.getDimension(
                R.styleable.CountdownView_suffixMinuteLeftMargin, -1);
        mSuffixMinuteRightMargin = ta.getDimension(
                R.styleable.CountdownView_suffixMinuteRightMargin, -1);
        mSuffixSecondLeftMargin = ta.getDimension(
                R.styleable.CountdownView_suffixSecondLeftMargin, -1);
        mSuffixSecondRightMargin = ta.getDimension(
                R.styleable.CountdownView_suffixSecondRightMargin, -1);
        ta.recycle();
 
        // temporarily saved suffix left and right margins
        mTempSuffixDayLeftMargin = mSuffixDayLeftMargin;
        mTempSuffixDayRightMargin = mSuffixDayRightMargin;
        mTempSuffixHourLeftMargin = mSuffixHourLeftMargin;
        mTempSuffixHourRightMargin = mSuffixHourRightMargin;
        mTempSuffixMinuteLeftMargin = mSuffixMinuteLeftMargin;
        mTempSuffixMinuteRightMargin = mSuffixMinuteRightMargin;
        mTempSuffixSecondLeftMargin = mSuffixSecondLeftMargin;
        mTempSuffixSecondRightMargin = mSuffixSecondRightMargin;
 
        mTempSuffixMinute = mSuffixMinute;
 
        // initialize
        initPaint();
        initSuffix(true);
        initSuffixMargin();
 
        // regular time data
        // pick one of two (minute and second)
 
        // initialize time text width and height
        Rect rect = new Rect();
        mTimeTextPaint.getTextBounds("00", 0, 2, rect);
        mTimeTextWidth = rect.width();
        mTimeTextHeight = rect.height();
        mTimeTextBottom = rect.bottom;
 
        if (!isHideTimeBackground && mTimeBgSize < mTimeTextWidth) {
            mTimeBgSize = mTimeTextWidth + (dp2px(2) * 4);
        }
    }
 
    /**
     * initialize Paint
     */
    private void initPaint() {
        // time text
        mTimeTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mTimeTextPaint.setColor(mTimeTextColor);
        mTimeTextPaint.setTextAlign(Paint.Align.CENTER);
        mTimeTextPaint.setTextSize(mTimeTextSize);
        if (isTimeTextBold) {
            mTimeTextPaint.setFakeBoldText(true);
        }
 
        // suffix text
        mSuffixTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mSuffixTextPaint.setColor(mSuffixTextColor);
        mSuffixTextPaint.setTextSize(mSuffixTextSize);
        if (isSuffixTextBold) {
            mSuffixTextPaint.setFakeBoldText(true);
        }
 
        // time background
        mTimeTextBgPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mTimeTextBgPaint.setStyle(Paint.Style.FILL);
        mTimeTextBgPaint.setColor(mTimeBgColor);
 
        // time background division line
        mTimeTextBgDivisionLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mTimeTextBgDivisionLinePaint.setColor(mTimeBgDivisionLineColor);
        mTimeTextBgDivisionLinePaint.setStrokeWidth(mTimeBgDivisionLineSize);
    }
 
    /**
     * initialize suffix
     */
    private void initSuffix(boolean isInit) {
        boolean isSuffixNull = true;
        float mSuffixTextWidth = 0;
        float mDefSuffixTextWidth = mSuffixTextPaint
                .measureText(DEFAULT_SUFFIX);
 
        if (!TextUtils.isEmpty(mSuffix)) {
            isSuffixNull = false;
            mSuffixTextWidth = mSuffixTextPaint.measureText(mSuffix);
        }
 
        boolean isSetSuffixDay = !TextUtils.isEmpty(mSuffixDay);
        boolean isSetSuffixHour = !TextUtils.isEmpty(mSuffixHour);
        boolean isSetSuffixMinute = !TextUtils.isEmpty(mSuffixMinute);
 
        if (isInit) {
            if ((isShowDay && isSetSuffixDay)
                    || (isShowHour && isSetSuffixHour)
                    || (isShowMinute && isSetSuffixMinute)
                    ) {
                hasCustomSomeSuffix = true;
            }
        }
 
        if (isShowDay) {
            if (isSetSuffixDay) {
                mSuffixDayTextWidth = mSuffixTextPaint.measureText(mSuffixDay);
            } else {
                if (!isSuffixNull) {
                    mSuffixDay = mSuffix;
                    mSuffixDayTextWidth = mSuffixTextWidth;
                } else if (!hasCustomSomeSuffix) {
                    mSuffixDay = DEFAULT_SUFFIX;
                    mSuffixDayTextWidth = mDefSuffixTextWidth;
                }
            }
        } else {
            mSuffixDayTextWidth = 0;
        }
 
        if (isShowHour) {
            if (isSetSuffixHour) {
                mSuffixHourTextWidth = mSuffixTextPaint
                        .measureText(mSuffixHour);
            } else {
                if (!isSuffixNull) {
                    mSuffixHour = mSuffix;
                    mSuffixHourTextWidth = mSuffixTextWidth;
                } else if (!hasCustomSomeSuffix) {
                    mSuffixHour = DEFAULT_SUFFIX;
                    mSuffixHourTextWidth = mDefSuffixTextWidth;
                }
            }
        } else {
            mSuffixHourTextWidth = 0;
        }
 
        if (isShowMinute) {
            if (isSetSuffixMinute) {
                mSuffixMinuteTextWidth = mSuffixTextPaint
                        .measureText(mSuffixMinute);
            } else {
                mSuffixMinuteTextWidth = 0;
            }
        } else {
            mSuffixMinuteTextWidth = 0;
        }
 
        if (isShowMinute) {
            if (isSetSuffixMinute) {
                mSuffixMinuteTextWidth = mSuffixTextPaint
                        .measureText(mSuffixMinute);
            } else {
                mSuffixMinuteTextWidth = 0;
            }
        } else {
            mSuffixMinuteTextWidth = 0;
        }
 
        mSuffixSecondTextWidth = 0;
    }
 
    /**
     * initialize suffix margin
     */
    private void initSuffixMargin() {
        int defSuffixLRMargin = dp2px(DEFAULT_SUFFIX_LR_MARGIN);
        boolean isSuffixLRMarginNull = true;
 
        if (mSuffixLRMargin >= 0) {
            isSuffixLRMarginNull = false;
        }
 
        if (isShowDay && mSuffixDayTextWidth > 0) {
            if (mSuffixDayLeftMargin < 0) {
                if (!isSuffixLRMarginNull) {
                    mSuffixDayLeftMargin = mSuffixLRMargin;
                } else {
                    mSuffixDayLeftMargin = defSuffixLRMargin;
                }
            }
 
            if (mSuffixDayRightMargin < 0) {
                if (!isSuffixLRMarginNull) {
                    mSuffixDayRightMargin = mSuffixLRMargin;
                } else {
                    mSuffixDayRightMargin = defSuffixLRMargin;
                }
            }
        } else {
            mSuffixDayLeftMargin = 0;
            mSuffixDayRightMargin = 0;
        }
 
        if (isShowHour && mSuffixHourTextWidth > 0) {
            if (mSuffixHourLeftMargin < 0) {
                if (!isSuffixLRMarginNull) {
                    mSuffixHourLeftMargin = mSuffixLRMargin;
                } else {
                    mSuffixHourLeftMargin = defSuffixLRMargin;
                }
            }
 
            if (mSuffixHourRightMargin < 0) {
                if (!isSuffixLRMarginNull) {
                    mSuffixHourRightMargin = mSuffixLRMargin;
                } else {
                    mSuffixHourRightMargin = defSuffixLRMargin;
                }
            }
        } else {
            mSuffixHourLeftMargin = 0;
            mSuffixHourRightMargin = 0;
        }
 
        if (isShowMinute && mSuffixMinuteTextWidth > 0) {
            if (mSuffixMinuteLeftMargin < 0) {
                if (!isSuffixLRMarginNull) {
                    mSuffixMinuteLeftMargin = mSuffixLRMargin;
                } else {
                    mSuffixMinuteLeftMargin = defSuffixLRMargin;
                }
            }
 
            mSuffixMinuteRightMargin = 0;
        } else {
            mSuffixMinuteLeftMargin = 0;
            mSuffixMinuteRightMargin = 0;
        }
 
        mSuffixSecondLeftMargin = 0;
        mSuffixSecondRightMargin = 0;
    }
 
    /**
     * initialize time initialize rectF
     */
    private void initTimeBgRect() {
        if (!isHideTimeBackground) {
            float mHourLeft;
            float mMinuteLeft;
            float mSecondLeft;
 
            if (isShowDay) {
                // initialize day background rectF
                mDayBgRectF = new RectF(mLeftPaddingSize, mTopPaddingSize,
                        mLeftPaddingSize + mDayTimeBgWidth, mTopPaddingSize
                        + mDayTimeBgWidth);
                // hour left point
                mHourLeft = mLeftPaddingSize + mDayTimeBgWidth
                        + mSuffixDayTextWidth + mSuffixDayLeftMargin
                        + mSuffixDayRightMargin;
            } else {
                // hour left point
                mHourLeft = mLeftPaddingSize;
            }
 
            if (isShowHour) {
                // initialize hour background rectF
                mHourBgRectF = new RectF(mHourLeft, mTopPaddingSize, mHourLeft
                        + mTimeBgSize, mTopPaddingSize + mTimeBgSize);
                // minute left point
                mMinuteLeft = mHourLeft + mTimeBgSize + mSuffixHourTextWidth
                        + mSuffixHourLeftMargin + mSuffixHourRightMargin;
            } else {
                // minute left point
                mMinuteLeft = mHourLeft;
            }
 
            if (isShowMinute) {
                // initialize minute background rectF
                mMinuteBgRectF = new RectF(mMinuteLeft, mTopPaddingSize,
                        mMinuteLeft + mTimeBgSize, mTopPaddingSize
                        + mTimeBgSize);
                // second left point
                mSecondLeft = mMinuteLeft + mTimeBgSize
                        + mSuffixMinuteTextWidth + mSuffixMinuteLeftMargin
                        + mSuffixMinuteRightMargin;
            } else {
                // second left point
                mSecondLeft = mMinuteLeft;
            }
 
        }
    }
 
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
 
        mContentAllWidth = getAllContentWidth();
        mContentAllHeight = isHideTimeBackground ? (int) mTimeTextHeight
                : (int) mTimeBgSize;
 
        mViewWidth = measureSize(1, mContentAllWidth, widthMeasureSpec);
        mViewHeight = measureSize(2, mContentAllHeight, heightMeasureSpec);
 
        setMeasuredDimension(mViewWidth, mViewHeight);
 
        initTimeTextBaselineAndTimeBgTopPadding();
        initLeftPaddingSize();
        initTimeBgRect();
    }
 
    /**
     * initialize time text baseline and time background top padding
     */
    private void initTimeTextBaselineAndTimeBgTopPadding() {
        if (getPaddingTop() == getPaddingBottom()) {
            // center
            mTimeTextBaseline = mViewHeight / 2 + mTimeTextHeight / 2
                    - mTimeTextBottom;
 
            mTopPaddingSize = (mViewHeight - mContentAllHeight) / 2;
        } else {
            // padding top
            mTimeTextBaseline = mViewHeight - (mViewHeight - getPaddingTop())
                    + mTimeTextHeight - mTimeTextBottom;
 
            mTopPaddingSize = getPaddingTop();
        }
 
        if (isShowDay && mSuffixDayTextWidth > 0) {
            mSuffixDayTextBaseline = getSuffixTextBaseLine(mSuffixDay);
        }
 
        if (isShowHour && mSuffixHourTextWidth > 0) {
            mSuffixHourTextBaseline = getSuffixTextBaseLine(mSuffixHour);
        }
 
        if (isShowMinute && mSuffixMinuteTextWidth > 0) {
            mSuffixMinuteTextBaseline = getSuffixTextBaseLine(mSuffixMinute);
        }
 
 
    }
 
    private float getSuffixTextBaseLine(String suffixText) {
        Rect tempRect = new Rect();
        mSuffixTextPaint.getTextBounds(suffixText, 0, suffixText.length(),
                tempRect);
 
        float ret;
        switch (mSuffixGravity) {
            case 0:
                // top
                if (isHideTimeBackground) {
                    ret = mTimeTextBaseline - mTimeTextHeight - tempRect.top;
                } else {
                    ret = mTopPaddingSize - tempRect.top;
                }
                break;
            default:
            case 1:
                // center
                if (isHideTimeBackground) {
                    ret = mTimeTextBaseline - mTimeTextHeight / 2
                            + tempRect.height() / 2;
                } else {
                    ret = mTopPaddingSize + mTimeBgSize - mTimeBgSize / 2
                            + tempRect.height() / 2;
                }
                break;
            case 2:
                // bottom
                if (isHideTimeBackground) {
                    ret = mTimeTextBaseline - tempRect.bottom;
                } else {
                    ret = mTopPaddingSize + mTimeBgSize - tempRect.bottom;
                }
                break;
        }
 
        return ret;
    }
 
    /**
     * initialize view left padding
     */
    private void initLeftPaddingSize() {
        if (getPaddingLeft() == getPaddingRight()) {
            // center
            mLeftPaddingSize = (mViewWidth - mContentAllWidth) / 2;
        } else {
            // padding left
            mLeftPaddingSize = getPaddingLeft();
        }
    }
 
    /**
     * measure view Size
     *
     * @param specType    1 width 2 height
     * @param contentSize all content view size
     * @param measureSpec spec
     * @return measureSize
     */
    private int measureSize(int specType, int contentSize, int measureSpec) {
        int result;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
 
        if (specMode == MeasureSpec.EXACTLY) {
            result = Math.max(contentSize, specSize);
        } else {
            result = contentSize;
 
            if (specType == 1) {
                // width
                result += (getPaddingLeft() + getPaddingRight());
            } else {
                // height
                result += (getPaddingTop() + getPaddingBottom());
            }
        }
 
        return result;
    }
 
    /**
     * get all view width
     *
     * @return all view width
     */
    private int getAllContentWidth() {
        float timeWidth = isHideTimeBackground ? mTimeTextWidth : mTimeBgSize;
        float width = (mSuffixDayTextWidth + mSuffixHourTextWidth
                + mSuffixMinuteTextWidth + mSuffixSecondTextWidth);
        width += (mSuffixDayLeftMargin + mSuffixDayRightMargin
                + mSuffixHourLeftMargin + mSuffixHourRightMargin
                + mSuffixMinuteLeftMargin + mSuffixMinuteRightMargin
                + mSuffixSecondLeftMargin + mSuffixSecondRightMargin);
 
        if (isShowDay) {
            if (isDayLargeNinetyNine) {
                Rect rect = new Rect();
                String tempDay = String.valueOf(mDay);
                mTimeTextPaint
                        .getTextBounds(tempDay, 0, tempDay.length(), rect);
                mDayTimeTextWidth = rect.width();
 
                if (!isHideTimeBackground) {
                    mDayTimeBgWidth = mDayTimeTextWidth + (dp2px(2) * 4);
                    width += mDayTimeBgWidth;
                } else {
                    width += mDayTimeTextWidth;
                }
            } else {
                mDayTimeTextWidth = mTimeTextWidth;
                mDayTimeBgWidth = mTimeBgSize;
                width += timeWidth;
            }
        }
 
        if (isShowHour) {
            width += timeWidth;
        }
 
        if (isShowMinute) {
            width += timeWidth;
        }
 
        return (int) Math.ceil(width);
    }
 
    private void refTimeShow(boolean isShowDay, boolean isShowHour,
                             boolean isShowMinute) {
        boolean isRef = false;
 
        if (this.isShowDay != isShowDay) {
            this.isShowDay = isShowDay;
            isRef = true;
 
            // reset day margins
            if (isShowDay) {
                mSuffixDayLeftMargin = mTempSuffixDayLeftMargin;
                mSuffixDayRightMargin = mTempSuffixDayRightMargin;
            }
        }
 
        if (this.isShowHour != isShowHour) {
            this.isShowHour = isShowHour;
            isRef = true;
 
            // reset hour margins
            if (isShowHour) {
                mSuffixHourLeftMargin = mTempSuffixHourLeftMargin;
                mSuffixHourRightMargin = mTempSuffixHourRightMargin;
            }
        }
 
        if (this.isShowMinute != isShowMinute) {
            this.isShowMinute = isShowMinute;
            isRef = true;
 
            // reset minute margins
            if (isShowMinute) {
                mSuffixMinuteLeftMargin = mTempSuffixMinuteLeftMargin;
                mSuffixMinuteRightMargin = mTempSuffixMinuteRightMargin;
                mSuffixMinute = mTempSuffixMinute;
            }
        }
 
        boolean isModCountdownInterval = false;
 
        // judge modify countdown interval
        if (isModCountdownInterval) {
            start(mRemainTime);
        }
 
        if (isRef) {
            initSuffix(false);
            initSuffixMargin();
            requestLayout();
        }
    }
 
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
 
        float mHourLeft;
        float mMinuteLeft;
        float mSecondLeft;
 
        if (isHideTimeBackground) {
            // no background
 
            if (isShowDay) {
                // draw day text
                canvas.drawText(formatNum(mDay), mLeftPaddingSize
                                + mDayTimeTextWidth / 2, mTimeTextBaseline,
                        mTimeTextPaint);
                if (mSuffixDayTextWidth > 0) {
                    // draw day suffix
                    canvas.drawText(mSuffixDay, mLeftPaddingSize
                                    + mDayTimeTextWidth + mSuffixDayLeftMargin,
                            mSuffixDayTextBaseline, mSuffixTextPaint);
                }
 
                // hour left point
                mHourLeft = mLeftPaddingSize + mDayTimeTextWidth
                        + mSuffixDayTextWidth + mSuffixDayLeftMargin
                        + mSuffixDayRightMargin;
            } else {
                // hour left point
                mHourLeft = mLeftPaddingSize;
            }
 
            if (isShowHour) {
                // draw hour text
                canvas.drawText(formatNum(mHour), mHourLeft + mTimeTextWidth
                        / 2, mTimeTextBaseline, mTimeTextPaint);
                if (mSuffixHourTextWidth > 0) {
                    // draw hour suffix
                    canvas.drawText(mSuffixHour, mHourLeft + mTimeTextWidth
                                    + mSuffixHourLeftMargin, mSuffixHourTextBaseline,
                            mSuffixTextPaint);
                }
 
                // minute left point
                mMinuteLeft = mHourLeft + mTimeTextWidth + mSuffixHourTextWidth
                        + mSuffixHourLeftMargin + mSuffixHourRightMargin;
            } else {
                // minute left point
                mMinuteLeft = mHourLeft;
            }
 
            if (isShowMinute) {
                // draw minute text
                canvas.drawText(formatNum(mMinute), mMinuteLeft
                        + mTimeTextWidth / 2, mTimeTextBaseline, mTimeTextPaint);
                if (mSuffixMinuteTextWidth > 0) {
                    // draw minute suffix
                    canvas.drawText(mSuffixMinute, mMinuteLeft + mTimeTextWidth
                                    + mSuffixMinuteLeftMargin,
                            mSuffixMinuteTextBaseline, mSuffixTextPaint);
                }
 
                // second left point
                mSecondLeft = mMinuteLeft + mTimeTextWidth
                        + mSuffixMinuteTextWidth + mSuffixMinuteLeftMargin
                        + mSuffixMinuteRightMargin;
            } else {
                // second left point
                mSecondLeft = mMinuteLeft;
            }
 
        } else {
            // show background
 
            if (isShowDay) {
                // draw day background
                canvas.drawRoundRect(mDayBgRectF, mTimeBgRadius, mTimeBgRadius,
                        mTimeTextBgPaint);
                if (isShowTimeBgDivisionLine) {
                    // draw day background division line
                    canvas.drawLine(mLeftPaddingSize, mTimeBgDivisionLineYPos,
                            mLeftPaddingSize + mDayTimeBgWidth,
                            mTimeBgDivisionLineYPos,
                            mTimeTextBgDivisionLinePaint);
                }
                // draw day text
                canvas.drawText(formatNum(mDay), mDayBgRectF.centerX(),
                        mTimeTextBaseY, mTimeTextPaint);
                if (mSuffixDayTextWidth > 0) {
                    // draw day suffix
                    canvas.drawText(mSuffixDay, mLeftPaddingSize
                                    + mDayTimeBgWidth + mSuffixDayLeftMargin,
                            mSuffixDayTextBaseline, mSuffixTextPaint);
                }
 
                // hour left point
                mHourLeft = mLeftPaddingSize + mDayTimeBgWidth
                        + mSuffixDayTextWidth + mSuffixDayLeftMargin
                        + mSuffixDayRightMargin;
            } else {
                // hour left point
                mHourLeft = mLeftPaddingSize;
            }
 
            if (isShowHour) {
                // draw hour background
                canvas.drawRoundRect(mHourBgRectF, mTimeBgRadius,
                        mTimeBgRadius, mTimeTextBgPaint);
                if (isShowTimeBgDivisionLine) {
                    // draw hour background division line
                    canvas.drawLine(mHourLeft, mTimeBgDivisionLineYPos,
                            mTimeBgSize + mHourLeft, mTimeBgDivisionLineYPos,
                            mTimeTextBgDivisionLinePaint);
                }
                // draw hour text
                canvas.drawText(formatNum(mHour), mHourBgRectF.centerX(),
                        mTimeTextBaseY, mTimeTextPaint);
                if (mSuffixHourTextWidth > 0) {
                    // draw hour suffix
                    canvas.drawText(mSuffixHour, mHourLeft + mTimeBgSize
                                    + mSuffixHourLeftMargin, mSuffixHourTextBaseline,
                            mSuffixTextPaint);
                }
 
                // minute left point
                mMinuteLeft = mHourLeft + mTimeBgSize + mSuffixHourTextWidth
                        + mSuffixHourLeftMargin + mSuffixHourRightMargin;
            } else {
                // minute left point
                mMinuteLeft = mHourLeft;
            }
 
            if (isShowMinute) {
                // draw minute background
                canvas.drawRoundRect(mMinuteBgRectF, mTimeBgRadius,
                        mTimeBgRadius, mTimeTextBgPaint);
                if (isShowTimeBgDivisionLine) {
                    // draw minute background division line
                    canvas.drawLine(mMinuteLeft, mTimeBgDivisionLineYPos,
                            mTimeBgSize + mMinuteLeft, mTimeBgDivisionLineYPos,
                            mTimeTextBgDivisionLinePaint);
                }
                // draw minute text
                canvas.drawText(formatNum(mMinute), mMinuteBgRectF.centerX(),
                        mTimeTextBaseY, mTimeTextPaint);
                if (mSuffixMinuteTextWidth > 0) {
                    // draw minute suffix
                    canvas.drawText(mSuffixMinute, mMinuteLeft + mTimeBgSize
                                    + mSuffixMinuteLeftMargin,
                            mSuffixMinuteTextBaseline, mSuffixTextPaint);
                }
 
                // second left point
                mSecondLeft = mMinuteLeft + mTimeBgSize
                        + mSuffixMinuteTextWidth + mSuffixMinuteLeftMargin
                        + mSuffixMinuteRightMargin;
            } else {
                // second left point
                mSecondLeft = mMinuteLeft;
            }
 
        }
    }
 
    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        stop();
    }
 
    /**
     * start countdown
     *
     * @param millisecond millisecond
     */
    public void start(long millisecond) {
        if (millisecond <= 0) {
            setVisibility(GONE);
            return;
        }
 
        if (null != mCustomCountDownTimer) {
            mCustomCountDownTimer.stop();
            mCustomCountDownTimer = null;
        }
 
        long countDownInterval;
        countDownInterval = 1000;
 
        mCustomCountDownTimer = new CustomCountDownTimer(millisecond,
                countDownInterval) {
            @Override
            public void onTick(long millisUntilFinished) {
                updateShow(millisUntilFinished);
            }
 
            @Override
            public void onFinish() {
                // countdown end
                allShowZero();
                // callback
                if (null != mOnCountdownEndListener) {
                    mOnCountdownEndListener.onEnd(CountdownView.this);
                }
            }
        };
        mCustomCountDownTimer.start();
    }
 
    /**
     * stop countdown
     */
    public void stop() {
        if (null != mCustomCountDownTimer)
            mCustomCountDownTimer.stop();
    }
 
    /**
     * pause countdown
     */
    public void pause() {
        if (null != mCustomCountDownTimer)
            mCustomCountDownTimer.pause();
    }
 
    /**
     * pause countdown
     */
    public void restart() {
        if (null != mCustomCountDownTimer)
            mCustomCountDownTimer.restart();
    }
 
    /**
     * custom time show
     *
     * @param isShowDay         isShowDay
     * @param isShowHour        isShowHour
     * @param isShowMinute      isShowMinute
     * @param isShowSecond      isShowSecond
     * @param isShowMillisecond isShowMillisecond
     */
    public void customTimeShow(boolean isShowDay, boolean isShowHour,
                               boolean isShowMinute, boolean isShowSecond,
                               boolean isShowMillisecond) {
        mHasSetIsShowDay = true;
        mHasSetIsShowHour = true;
 
        // regular time data
        // pick one of two (minute and second)
        if (!isShowMinute && !isShowSecond) {
            isShowSecond = true;
        }
 
        refTimeShow(isShowDay, isShowHour, isShowMinute);
    }
 
    /**
     * set all time zero
     */
    public void allShowZero() {
        mHour = 0;
        mMinute = 0;
        invalidate();
        setVisibility(GONE);
        if (null != mCallback) {
            mCallback.onEndTime();
        }
    }
 
    private CallBack mCallback;
 
    public CallBack getCallback() {
        return mCallback;
    }
 
    public void setCallback(CallBack callback) {
        this.mCallback = callback;
    }
 
    public interface CallBack {
        void onEndTime();
    }
 
    /**
     * set countdown end callback listener
     *
     * @param onCountdownEndListener OnCountdownEndListener
     */
    public void setOnCountdownEndListener(
            OnCountdownEndListener onCountdownEndListener) {
        this.mOnCountdownEndListener = onCountdownEndListener;
    }
 
    /**
     * set interval callback listener
     *
     * @param interval                    interval time
     * @param onCountdownIntervalListener OnCountdownIntervalListener
     */
    public void setOnCountdownIntervalListener(long interval,
                                               OnCountdownIntervalListener onCountdownIntervalListener) {
        this.mInterval = interval;
        this.mOnCountdownIntervalListener = onCountdownIntervalListener;
    }
 
    /**
     * get day
     *
     * @return current day
     */
    public int getDay() {
        return mDay;
    }
 
    /**
     * get hour
     *
     * @return current hour
     */
    public int getHour() {
        return mHour;
    }
 
    /**
     * get minute
     *
     * @return current minute
     */
    public int getMinute() {
        return mMinute;
    }
 
    /**
     * get remain time
     *
     * @return remain time ( millisecond )
     */
    public long getRemainTime() {
        return mRemainTime;
    }
 
    public void updateShow(long ms) {
        this.mRemainTime = ms;
 
        mDay = (int) (ms / (1000 * 60 * 60 * 24));
        mHour = (int) ((ms % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
        mMinute = (int) ((ms % (1000 * 60 * 60)) / (1000 * 60));
 
        // interval callback
        if (mInterval > 0 && null != mOnCountdownIntervalListener) {
            if (mPreviouIntervalCallbackTime == 0) {
                mPreviouIntervalCallbackTime = ms;
            } else if (ms + mInterval <= mPreviouIntervalCallbackTime) {
                mPreviouIntervalCallbackTime = ms;
                mOnCountdownIntervalListener.onInterval(this, mRemainTime);
            }
        }
 
        handlerAutoShowTimeAndDayLargeNinetyNine();
 
        invalidate();
    }
 
    private void handlerAutoShowTimeAndDayLargeNinetyNine() {
        if (!mHasSetIsShowDay) {
            if (!isShowDay && mDay > 0) {
                // auto show day
                // judge auto show hour
                if (!mHasSetIsShowHour) {
                    refTimeShow(true, true, isShowMinute);
                } else {
                    refTimeShow(true, isShowHour, isShowMinute);
                }
            } else if (isShowDay && mDay == 0) {
                // auto hide day
                refTimeShow(false, isShowHour, isShowMinute);
            } else {
                if (!mHasSetIsShowHour) {
                    if (!isShowHour && (mDay > 0 || mHour > 0)) {
                        // auto show hour
                        refTimeShow(isShowDay, true, isShowMinute);
                    } else if (isShowHour && mDay == 0 && mHour == 0) {
                        // auto hide hour
                        refTimeShow(false, false, isShowMinute);
                    }
                }
            }
        } else {
            if (!mHasSetIsShowHour) {
                if (!isShowHour && (mDay > 0 || mHour > 0)) {
                    // auto show hour
                    refTimeShow(isShowDay, true, isShowMinute);
                } else if (isShowHour && mDay == 0 && mHour == 0) {
                    // auto hide hour
                    refTimeShow(isShowDay, false, isShowMinute);
                }
            }
        }
 
        if (isShowDay) {
            // handler large ninety nine
            if (!isDayLargeNinetyNine && mDay > 99) {
                isDayLargeNinetyNine = true;
                requestLayout();
            } else if (isDayLargeNinetyNine && mDay <= 99) {
                isDayLargeNinetyNine = false;
                requestLayout();
            }
        }
    }
 
    private String formatNum(int time) {
        return time < 10 ? "0" + time : String.valueOf(time);
    }
 
    public interface OnCountdownEndListener {
        void onEnd(CountdownView cv);
    }
 
    public interface OnCountdownIntervalListener {
        void onInterval(CountdownView cv, long remainTime);
    }
 
    private int dp2px(float dpValue) {
        final float scale = mContext.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
 
    private float sp2px(float spValue) {
        final float scale = mContext.getResources().getDisplayMetrics().scaledDensity;
        return spValue * scale;
    }
 
}