android 自定义view实现时钟

package com.domain.zjl.androidhero;

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

import java.util.Calendar;

/**
 * Created by xiaoji94shen on 2016/9/1.
 */
public class MyClockView extends View {

    private Handler mHandler;
    private Runnable mRunnable;
    private Paint mPaint;

    public MyClockView(Context context) {
        super(context);
        init();

    }

    public MyClockView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        mHandler = new Handler();
        initPaint();
        mRunnable = new Runnable() {
            @Override
            public void run() {
                postInvalidate();
                mHandler.postDelayed(this, 1000);
            }
        };
        mHandler.postDelayed(mRunnable, 1000);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawBigCircle(canvas);
        drawLinesAndNumbers(canvas);
        drawPoint(canvas);
        drawLines(canvas);
    }

    private void initPaint() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.BLACK);
    }

    private void drawLines(Canvas canvas) {
        Calendar calendar = Calendar.getInstance();
        float hour = calendar.get(Calendar.HOUR);
        float min = calendar.get(Calendar.MINUTE);
        float secode = calendar.get(Calendar.SECOND);

        mPaint.setStyle(Paint.Style.FILL);

        float startX = getWidth() / 2;
        float endX = startX;

        float startY = getHeight() / 2;
        float endY = startY - 80;

        canvas.save();

        float hourDegree = (hour + min / 60) * (360 / 12);
        canvas.rotate(hourDegree, getWidth() / 2, getHeight() / 2);
        canvas.drawLine(startX, startY, endX, endY, mPaint);
        canvas.restore();

        canvas.save();
        float minDegree = (min + secode / 60) * 360 / 60;
        canvas.rotate(minDegree, getWidth() / 2, getHeight() / 2);
        canvas.drawLine(startX, startY, endX, endY - 20, mPaint);
        canvas.restore();

        canvas.save();
        float secondeDegree = secode * 360 / 60;
        canvas.rotate(secondeDegree, getWidth() / 2, getHeight() / 2);
        canvas.drawLine(startX, startY, endX, endY - 40, mPaint);
        canvas.restore();
    }

    private void drawPoint(Canvas canvas) {

        mPaint.setStyle(Paint.Style.FILL);
        canvas.drawCircle(getWidth() / 2, getHeight() / 2, 6, mPaint);

    }

    private void drawBigCircle(Canvas canvas) {
        mPaint.setStyle(Paint.Style.STROKE);
        canvas.drawCircle(getWidth() / 2, getHeight() / 2, getWidth() / 2 - 4, mPaint);
    }


    private void drawLinesAndNumbers(Canvas canvas) {
        canvas.save();
        int logHeight = 16;
        int shortHeight = 8;
        int startX = getWidth() / 2;
        int stopX = startX;
        int radius = getWidth() / 2 - 4;
        int startY = getHeight() / 2 - radius;
        int stopY = 0;
        int degree = 360 / 60;

        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setTextSize(25);

        String[] numbers = new String[]{"12", "1", "2", "3", "4", "5",
                "6", "7", "8", "9", "10", "11"};
        int j = 0;
        for (int i = 0; i < 60; i++) {

            if (i % 5 == 0) {
                stopY = startY + logHeight;
                j++;
            } else {
                stopY = startY + shortHeight;
            }
            canvas.drawLine(startX, startY, stopX, stopY, mPaint);

            if (i % 5 == 0) {
                String num = numbers[j - 1];
                Rect textBounds = new Rect();
                mPaint.getTextBounds(num, 0, num.length(), textBounds);
                int textStartX = (int) (startX - mPaint.measureText(num) / 2);
                int textStartY = (int) (stopY + textBounds.height()) + 2;
                canvas.drawText(numbers[j - 1], textStartX, textStartY, mPaint);
            }
            canvas.rotate(degree, getWidth() / 2, getHeight() / 2);
        }
        canvas.restore();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值