Android 自定义钟表

Android 自定义钟表

public class ClockView extends View {
    private Paint paint;
    private int clockColor;

    private void init() {
        paint = new Paint();
        this.setLayerType(View.LAYER_TYPE_SOFTWARE,paint);
        //设置抗锯齿
        paint.setAntiAlias(true);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int width = Math.min(getWidth(), getHeight());
        paint.setColor(clockColor);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(1);
        canvas.drawCircle(width / 2.0f, width / 2.0f, width / 2.0f, paint);
        canvas.drawCircle(width / 2.0f, width / 2.0f, width / 2.0f - 10, paint);
        paint.setColor(Color.BLACK);
        paint.setStrokeWidth(10);
        paint.setStyle(Paint.Style.FILL);
        canvas.drawCircle(width / 2.0f, width / 2.0f, 10, paint);
        paint.setStyle(Paint.Style.STROKE);
        for (int i = 0; i < 12; i++) {
            canvas.rotate(30, width / 2.0f, width / 2.0f);
            paint.setStrokeWidth(3);
            canvas.drawLine(width / 2.0f, 10, width / 2.0f, 50, paint);
            paint.setStrokeWidth(1);
            paint.setTextSize(20);
            paint.setTextAlign(Paint.Align.CENTER);
            canvas.drawText((i + 1) + "", width / 2.0f, 100, paint);
        }

        paint.setColor(Color.WHITE);
        //小时
        int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
        //分钟
        int minute = Calendar.getInstance().get(Calendar.MINUTE);
        //秒
        int second = Calendar.getInstance().get(Calendar.SECOND);
        canvas.save();
        paint.setStrokeWidth(8);
        canvas.rotate(hour * 360 / 12.0f + minute * 30 / 60.0f + second / 60.0f / 60.0f, width / 2.0f, width / 2.0f);
        canvas.drawLine(width / 2.0f, width / 4.0f, width / 2.0f, width * 7 / 12.0f, paint);
        canvas.restore();
        canvas.save();
        paint.setStrokeWidth(5);
        canvas.rotate(minute * 360 / 60f + second * 360 / 60f / 60.0f, width / 2.0f, width / 2.0f);
        canvas.drawLine(width / 2.0f, width / 5.0f, width / 2.0f, width * 8 / 12.0f, paint);
        canvas.restore();
        canvas.save();
        paint.setStrokeWidth(2);
        canvas.rotate(second * 360 / 60f, width / 2.0f, width / 2.0f);
        canvas.drawLine(width / 2.0f, width / 6.0f, width / 2.0f, width * 9 / 12.0f, paint);
        canvas.restore();
        setThread();
    }

    public ClockView(Context context) {
        super(context);
        init();
    }

    public ClockView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.ClockView);
        clockColor = array.getColor(R.styleable.ClockView_clock_color, 0);
        array.recycle();
        init();
    }

    private void setThread() {
        new Thread(() -> {
            try {
                Thread.sleep(1000);
                postInvalidate();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
    }

}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值