Android clock

Android clock


package com.li.zhobiao;

import java.util.Calendar;

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

public class MyView extends View{
    private int width;
    private int heigth;
    private Paint mPaintLine;
    private Paint mPaintCircle;
    private Paint mPaintText;
    private Paint mPaintPoint;
    private Calendar mCalendar;
    public static final int NEED_REFRESH=0x23;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case NEED_REFRESH:
                    mCalendar=Calendar.getInstance();
                    invalidate();//Remind the UI thread to repaint
                    handler.sendEmptyMessageDelayed(NEED_REFRESH,1000);//It is equivalent to iteration, sending an empty message every 1s to tell the UI thread to redraw
                    break;
            }
        }
    };
// View has 4 constructors
    public MyView(Context context) {
        super(context);
    }

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mCalendar=Calendar.getInstance();//Initialize Calendar to get the current time

        mPaintLine=new Paint();
        mPaintLine.setColor(Color.BLACK);//Set the color
        mPaintLine.setAntiAlias(true);//Set anti-aliasing
        mPaintLine.setStrokeWidth(10);//Set the line width

        mPaintCircle=new Paint();
        mPaintCircle.setStrokeWidth(10);//Set the line width
        mPaintCircle.setColor(Color.BLACK);//Set the color
        mPaintCircle.setAntiAlias(true);//Set anti-aliasing
        mPaintCircle.setStyle(Paint.Style.STROKE);//Set to hollow

        mPaintText=new Paint();
        mPaintText.setColor(Color.BLUE);//Set the color
        mPaintText.setTextAlign(Paint.Align.CENTER);//Set the alignment
        mPaintText.setAntiAlias(true);//Set anti-aliasing
        mPaintText.setTextSize(30);//Set the font size

        mPaintPoint=new Paint();
        mPaintPoint.setAntiAlias(true);//Set anti-aliasing
        mPaintPoint.setStyle(Paint.Style.FILL);//Set to FILL
        mPaintPoint.setStrokeWidth(10);//Set the width
        mPaintPoint.setColor(Color.BLACK);//Set the color

        handler.sendEmptyMessage(NEED_REFRESH);//Send an empty message to Handler
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width=getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);//Get the default width of the system
        height=getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);//Get the system default height
        setMeasuredDimension(width,heigth);//Set the default width and height
    }
    //onDraw is called by the UI thread, no other processing is required
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//        canvas.drawLine(0,600,600,600,mPaintLine);
//        canvas.drawCircle(300,300,100,mPaintCircle);
        canvas.drawCircle(width/2,heigth/2,300,mPaintCircle);
        canvas.drawCircle(width/2,heigth/2,10,mPaintPoint);
        //Use the for loop to draw the time information of the clock
        for (int i=1;i<=12;i++){
            // canvas.save(); and canvas.restore(); need to be used together
            //Save first, then rotate, then draw a line, and finally go to the initial position,
            canvas.save();//Save the current canvas state
            canvas.rotate(360/12*i,width/2,heigth/2);//The first parameter represents the rotation angle, and the second and third parameters represent the rotation center
            canvas.drawLine(width/2,heigth/2-300,width/2,heigth/2-280,mPaintLine);//The first two indicate the starting position, the third and fourth indicate the end position, and the last one indicates brush
            canvas.drawText(""+i,width/2,heigth/2-250,mPaintText);//The first is the text content, the second and the third indicate the display text position
            canvas.restore();//Restore to the saved canvas state
        }

            int minutes = mCalendar.get(Calendar.MINUTE);//Get the minutes of the current time
            int hours = mCalendar.get(Calendar.HOUR);//Get the hours of the current time
            int second=mCalendar.get(Calendar.SECOND);//Get the seconds of the current time

            canvas.save();
            Float minutesDegree = minutes / 60f * 360;//Get the angle occupied by the current minutes
            canvas.rotate(minutesDegree, width / 2, heigth / 2);
            canvas.drawLine(width / 2, heigth / 2 - 200, width / 2, heigth / 2 + 20, mPaintLine);
            canvas.restore();

            canvas.save();
            Float hoursDegree = (hours * 60 + minutes) / 12f / 60 * 360;//Get the angle occupied by the current hours
            canvas.rotate(hoursDegree, width / 2, heigth / 2);
            canvas.drawLine(width / 2, heigth / 2 - 100, width / 2, heigth / 2 + 30, mPaintLine);
            canvas.restore();

            canvas.save();
            Float secondDegree = second/60f*360;//Get the angle occupied by the current hour
            canvas.rotate(secondDegree, width / 2, heigth / 2);
            canvas.drawLine(width / 2, heigth / 2 - 240, width / 2, heigth / 2 + 40, mPaintLine);
            canvas.restore();

    }
}
<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >

    <com.li.zhobiao.MyView
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout >


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325767068&siteId=291194637