自定义View(分秒毫秒倒计时)

来源:互联网 发布:网络打印机怎么用 编辑:程序博客网 时间:2024/06/01 07:29

记录下



代码如下:

package barry.defineview;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.os.Handler;import android.os.Message;import android.support.annotation.Nullable;import android.support.v7.widget.AppCompatTextView;import android.util.AttributeSet;import android.util.Log;import java.util.Date;/*** @author:BarryYang on 2017/11/1* @Email:barry.yang@gaopeng.com* @Description:*/public class CountDownView extends AppCompatTextView {private static final String TAG = CountDownView.class.getSimpleName();private float mTextSize;private int mColor;private Paint mPaint;/*** 服务器时间*/private long serverTime;/*** 揭晓时间*/private long announceTime;private Rect mRect;private Context context;public CountDownView(Context context, @Nullable AttributeSet attrs) {super(context, attrs);TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CountDownView);mTextSize = a.getDimension(R.styleable.CountDownView_text_size, 18);mColor = a.getColor(R.styleable.CountDownView_text_color, getResources().getColor(R.color.colorAccent));a.recycle();init();}private void init() {mPaint = new Paint();mPaint.setAntiAlias(true);mPaint.setColor(mColor);mPaint.setTextSize(mTextSize);mRect = new Rect();}@Overrideprotected void onDraw(Canvas canvas) {long textInput = announceTime - serverTime + (serverTime - getLocalTime());String countDownTime = long2Time(textInput);Log.d(TAG, textInput + "-->" + countDownTime);mPaint.getTextBounds(countDownTime, 0, countDownTime.length(), mRect);mPaint.setColor(Color.YELLOW);canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), mPaint);mPaint.setColor(Color.RED);canvas.drawText(countDownTime, getWidth() / 2 - mRect.width() / 2, getHeight() / 2 + mRect.height() / 2, mPaint);mHandler.sendEmptyMessageDelayed(1, 100);}Handler mHandler = new Handler() {@Overridepublic void handleMessage(Message msg) {super.handleMessage(msg);invalidate();}};/*** 将long处理成时分秒** @param textInput* @return*/private String long2Time(long textInput) {String minuteStr, secondStr, msecondStr;if (textInput <= 0) {mHandler.removeCallbacksAndMessages(null);return "00:00:00";}long mSecond = textInput % 1000;long second = textInput / 1000 % 60;long minute = textInput / 1000 / 60 % 60;if (minute < 10) {minuteStr = "0" + minute;} else {minuteStr = minute + "";}if (second < 10) {secondStr = "0" + second;} else {secondStr = second + "";}if (mSecond >= 100) {msecondStr = mSecond + "";} else if (mSecond >= 10 && mSecond < 100) {msecondStr = "0" + mSecond;} else {msecondStr = "00" + mSecond;}return minuteStr + ":" + secondStr + ":" + msecondStr.substring(0, 2);}/*** 接口传入** @param context* @param serverTime 服务器时间* @param announceTime 揭晓时间*/public void setData(Context context, long serverTime, long announceTime) {this.context = context;this.serverTime = serverTime;this.announceTime = announceTime;}/*** 获取当前的本地时间*/private long getLocalTime() {Date date = new Date(System.currentTimeMillis());return date.getTime();}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {int width = dip2px(context, 160);int height = dip2px(context, 80);super.onMeasure(widthMeasureSpec, heightMeasureSpec);int widthMode = MeasureSpec.getMode(widthMeasureSpec);int widthSize = MeasureSpec.getSize(widthMeasureSpec);int heightMode = MeasureSpec.getMode(heightMeasureSpec);int heightSize = MeasureSpec.getSize(heightMeasureSpec);if (widthMode == MeasureSpec.EXACTLY) {width = widthSize;}if (heightMode == MeasureSpec.EXACTLY) {height = heightSize;}setMeasuredDimension(width, height);}@Overrideprotected void onDetachedFromWindow() {super.onDetachedFromWindow();if (mHandler != null) {mHandler.removeCallbacksAndMessages(null);}}/*** 根据手机的分辨率从dp的单位转换为px像素** @param context* @param dpValue* @return*/private int dip2px(Context context, float dpValue) {float scale = context.getResources().getDisplayMetrics().density;Log.d(TAG, scale + "");return (int) (dpValue * scale + 0.5f);}}