自定义时钟、钟表

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/dxf_cs/article/details/88843453
package com.unilife.fridge.foreign.home.ui.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;

import java.util.Calendar;

public class RetroClockView extends View {
    private int width;
    private int height;
    private Paint mPaintWhite;
    private Paint mPaintRed;
    public static final int NEED_INVALIDATE = 0X23;
    private IRetroClock mIRetroClock;

    //每隔一秒,在handler中调用一次重新绘制方法
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {

            switch (msg.what) {
                case NEED_INVALIDATE:
                    invalidate();//告诉UI主线程重新绘制
                    if (mIRetroClock != null) {
                        mIRetroClock.updateCurrentTime();
                    }
                    handler.sendEmptyMessageDelayed(NEED_INVALIDATE, 1000);
                    break;
                default:
                    break;
            }
        }
    };

    public void startTimer() {
        handler.sendEmptyMessageDelayed(NEED_INVALIDATE, 1000);
    }

    public RetroClockView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mPaintWhite = new Paint();
        mPaintWhite.setStrokeWidth(6);
        mPaintWhite.setColor(Color.WHITE);
        mPaintWhite.setAntiAlias(true);
        mPaintWhite.setStyle(Paint.Style.FILL);

        mPaintRed = new Paint();
        mPaintRed.setStrokeWidth(3);
        mPaintRed.setColor(Color.parseColor("#ff3d3d"));
        mPaintRed.setAntiAlias(true);
        mPaintRed.setStyle(Paint.Style.FILL);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
        height = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);
        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int minute = Calendar.getInstance().get(Calendar.MINUTE);//得到当前分钟数
        int hour = Calendar.getInstance().get(Calendar.HOUR);//得到当前小时数
        int second = Calendar.getInstance().get(Calendar.SECOND);//得到当前秒数

       /* //画出大圆心
        canvas.drawCircle(width / 2, height / 2, 20, mPaintWhite);
        //画出小圆心
        canvas.drawCircle(width / 2, height / 2, 10, mPaintBlue);*/
        float secDegree = second / 60f * 360;//得到秒针旋转的角度
        canvas.save();
        canvas.rotate(secDegree, width / 2, height / 2);
        canvas.drawLine(width / 2, height / 2 - 80, width / 2, height / 2 + 15, mPaintRed);
        canvas.restore();

        float minuteDegree = minute / 60f * 360;//得到分针旋转的角度
        canvas.save();
        canvas.rotate(minuteDegree - 90, width / 2, height / 2);

        RectF rect2 = new RectF(width / 2 - 10, height / 2 - 2.5f, width / 2 + 90, height / 2 + 2.5f);
        canvas.drawRoundRect(rect2, 2.5f, 2.5f, mPaintWhite);
        canvas.restore();
        float hourDegree = ((hour * 60 + minute) / 12f / 60 * 360);//得到时钟旋转的角度
        canvas.save();
        canvas.rotate(hourDegree - 90, width / 2, height / 2);

        RectF rect3 = new RectF(width / 2 - 10, height / 2 - 2.5f, width / 2 + 60, height / 2 + 2.5f);
        canvas.drawRoundRect(rect3, 2.5f, 2.5f, mPaintWhite);
        canvas.restore();
    }

    public interface IRetroClock {
        public void updateCurrentTime();
    }

    public void setmIRetroClock(IRetroClock mIRetroClock) {
        this.mIRetroClock = mIRetroClock;
    }
}

猜你喜欢

转载自blog.csdn.net/dxf_cs/article/details/88843453