自定义倒计时以及不同字体大小的实现

自定义倒计时以及不同字体大小的实现

public class TimeView extends
        AppCompatTextView {

    private static final String TAG = "TimeView";

    private long hours;
    private long minutes;
    private long seconds;
    private long diff;
    private long days;

    public TimeView(Context context) {
        this(context, null);

    }

    public TimeView(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TimeView);
        diff = a.getInteger(R.styleable.TimeView_time, 0) * 1000;

        onCreate();

    }


    /**
     * 根据 attrs 设置时间开始
     */
    private void onCreate() {
        //start();
    }

    //开始计时
    private void start() {
        handler.removeMessages(1);

        getTime();

        Message message = handler.obtainMessage(1);
        handler.sendMessageDelayed(message, 1000);
    }

    final Handler handler = new Handler(Looper.getMainLooper()) {

        @Override
        public void handleMessage(Message msg) { // handle message
            switch (msg.what) {
                case 1:
                    setVisibility(View.VISIBLE);
                    diff = diff + 1000;
                    getShowTime();
                    if (diff > 0) {
                        Message message = handler.obtainMessage(1);
                        handler.sendMessageDelayed(message, 1000);
                    } else {
                        setVisibility(View.GONE);
                    }
                    break;
                default:
                    break;
            }
            super.handleMessage(msg);
        }
    };

    /**
     * 得到时间差
     */
    private void getTime() {
        try {
            days = diff / (1000 * 60 * 60 * 24);
            hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
            minutes = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);
            seconds = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60) - minutes * (1000 * 60)) / (1000);
            String tme = (days < 10 ? " " + days : days) + "天"
                    + (hours < 10 ? "0" + hours : hours) + "时"
                    + (minutes < 10 ? "0" + minutes : minutes) + "分"
                    + (seconds < 10 ? "0" + seconds : seconds) + "秒";


            SpannableString spanStr = new SpannableString(tme);
            spanStr.setSpan(new RelativeSizeSpan(0.6f), 2, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE );
            spanStr.setSpan(new RelativeSizeSpan(0.6f), 5, 6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            spanStr.setSpan(new RelativeSizeSpan(0.6f), 8, 9, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            spanStr.setSpan(new RelativeSizeSpan(0.6f), 11, 12, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            setText(spanStr);
        } catch (Exception e) {

        }
    }

    /**
     * 获得要显示的时间
     */
    private void getShowTime() {

        days = diff / (1000 * 60 * 60 * 24);
        hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
        minutes = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);
        seconds = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60) - minutes * (1000 * 60)) / (1000);
        String tme = (days < 10 ? " " + days : days) + "天"
                + (hours < 10 ? "0" + hours : hours) + "时"
                + (minutes < 10 ? "0" + minutes : minutes) + "分"
                + (seconds < 10 ? "0" + seconds : seconds) + "秒";


        SpannableString spanStr = new SpannableString(tme);
        spanStr.setSpan(new RelativeSizeSpan(0.6f), 2, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE );
        spanStr.setSpan(new RelativeSizeSpan(0.6f), 5, 6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spanStr.setSpan(new RelativeSizeSpan(0.6f), 8, 9, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spanStr.setSpan(new RelativeSizeSpan(0.6f), 11, 12, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        setText(spanStr);
    }
    public void setTime(long startTime) {
        long localL = System.currentTimeMillis();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
        String date1 = formatter.format(localL);
        String date2 = formatter.format(startTime);
        // 获取服务器返回的时间戳 转换成"yyyy-MM-dd HH:mm:ss"
        // 计算的时间差
        DateFormat df = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
        try {
            Date d1 = df.parse(date1);
            Date d2 = df.parse(date2);
            // 这样得到的差值是微秒级别
            this.diff = d1.getTime() - d2.getTime();
        } catch (ParseException pE) {
         
        }

        start();
    }
}

效果

效果图.png

你可能感兴趣的:(自定义倒计时以及不同字体大小的实现)