Android clock

Android clock


import java.util.Calendar;

import android.content.Context;
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(){
        public void handleMessage(Message msg) {
            switch (msg.what){
                case NEED_REFRESH:
                    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
// View has 4 constructors
    public MyView(Context 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

    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
    protected void onDraw(Canvas canvas) {
//        canvas.drawLine(0,600,600,600,mPaintLine);
//        canvas.drawCircle(300,300,100,mPaintCircle);
        //Use the for loop to draw the time information of the clock
        for (int i=1;i<=12;i++){
            //; and canvas.restore(); need to be used together
            //Save first, then rotate, then draw a line, and finally go to the initial position,
  ;//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

            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);

            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);

            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);

<LinearLayout  xmlns:android=""
    tools:context="${relativePackage}.${activityClass}" >


</LinearLayout >

Guess you like