登录后台

页面导航

效果图

请输入图片描述

开源

Gitee:https://gitee.com/rabbitTang_admin/TimeClockView

赞助

爱发电:http://afdian.net/@naiyouhuameitang

代码(ClockView.class)

public class MyView extends View {
    Paint paint = new Paint();// 画笔
    int height = 0;// 高
    int width = 0;// 宽
    int radius = 0;// 圆心
    Canvas canvas;// 画布

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void onDraw(Canvas c) {
        // TODO Auto-generated method stub
        canvas = c;// 赋值,无所谓的操作,方便查看而已
        super.onDraw(canvas);
        width = canvas.getWidth() / 2;// 获取半径宽
        height = canvas.getHeight() / 2;// 获取半径高
        radius = height - 3;// 获取半径
        paint.setColor(Color.BLACK);// 设置画笔颜色
        paint.setStyle(Style.STROKE);// 设置画笔风格
        paint.setStrokeWidth(3);// 设置画笔粗细
        paint.setAntiAlias(true);// 设置抗锯齿
        // 绘制外圆
        canvas.drawCircle(width, height, radius, paint);
        // 循环绘制小刻度(分钟\秒)
        for (int i = 0; i < 60; i++) {
            canvas.save();
            canvas.rotate(i * 6, width, height);
            canvas.drawLine(width, height - radius, width,
                    height - radius + 10, paint);
            canvas.restore();
        }
        // 循环绘制大刻度(小时)
        for (int i = 0; i < 12; i++) {
            canvas.save();
            canvas.rotate(i * 30, width, height);
            canvas.drawLine(width, height - radius, width,
                    height - radius + 20, paint);
            canvas.restore();
        }
        // 循环绘制时间文本(1-12)
        for (int i = 1; i < 13; i++) {
            paint.setStrokeWidth(1);
            canvas.save();
            canvas.rotate(i * 30, width, height);
            canvas.drawText(String.valueOf(i), width - 5, height - radius + 40,
                    paint);
            canvas.restore();
        }
        // 获取系统时间
        Calendar calendar = Calendar.getInstance();
        int hour = calendar.get(Calendar.HOUR);// 小时
        int min = calendar.get(Calendar.MINUTE);// 分钟
        int sec = calendar.get(Calendar.SECOND);// 秒
        // 设置画笔粗细
        paint.setStrokeWidth(15);
        // 设置画笔风格
        paint.setStyle(Style.FILL);
        /**
         * 绘制指针 ↓
         */
        // 绘制时针
        canvas.save();// 保存画布
        canvas.rotate(hour * 30 + min / 60 * 30, width, height);
        canvas.drawLine(width, height, width, height - 40, paint);
        // 绘制分针
        paint.setStrokeWidth(10);// 设置画笔粗细
        canvas.rotate(min * 6, width, height);
        canvas.drawLine(width, height, width, height - 50, paint);
        // 绘制秒针
        paint.setStrokeWidth(5);// 设置画笔粗细
        canvas.rotate(sec * 6, width, height);
        canvas.drawLine(width, height, width, height - 60, paint);
        canvas.restore();// 重置画布
        /**
         * 绘制指针 ↑
         */
        // 绘制小圆心
        canvas.drawCircle(width, height, 15, paint);
        // 重载View
        invalidate();
    }
}