Android自定義模擬時鐘控件

本文實例為大傢分享瞭Android自定義模擬時鐘控件的具體代碼,供大傢參考,具體內容如下

自定義view—透明模擬時鐘顯示

項目中要用到模擬時鐘的顯示,查瞭一些資料根據自己的需要進行瞭自定義view

思路:重寫view,1.根據控件的寬高進行獲取模擬時鐘的半徑大小。2.重寫onDraw方法,將畫佈進行不同角度的旋轉進行繪制表盤 圓心 刻度 指針

這裡就直接上代碼瞭

自定義的TimeClockView:

package com.eq.viewdemo;

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.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;

import java.util.Calendar;

/**
 * Created by pc on 2017/3/29.
 */
public class TimeClockView extends View {
    private int width;
    private int height;
    private Paint mPaintLine;
    private Paint mPaintCircle;
    private Paint mPaintHour;
    private Paint mPaintMinute;
    private Paint mPaintSec;
    private TextPaint mPaintText;
    private Calendar mCalendar;
    public static final int START_ONDRAW = 0X23;

    //每隔一秒,在handler中調用一次重新繪制方法
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {

            switch (msg.what) {
                case START_ONDRAW:
                    mCalendar = Calendar.getInstance();
                    invalidate();//告訴UI主線程重新繪制
                    handler.sendEmptyMessageDelayed(START_ONDRAW, 1000);
                    break;
                default:
                    break;
            }
        }
    };

    public TimeClockView(Context context) {
        super(context);
    }

    public TimeClockView(Context context, AttributeSet attrs) {
        super(context, attrs);

        mCalendar = Calendar.getInstance();

        mPaintLine = new Paint();
        mPaintLine.setColor(Color.GREEN);
        mPaintLine.setStrokeWidth(2);
        mPaintLine.setAntiAlias(true);//設置是否抗鋸齒
        mPaintLine.setStyle(Paint.Style.STROKE);//設置繪制風格

        mPaintCircle = new Paint();
        mPaintCircle.setColor(Color.RED);//設置顏色
        mPaintCircle.setStrokeWidth(2);//設置線寬
        mPaintCircle.setAntiAlias(true);//設置是否抗鋸齒
        mPaintCircle.setStyle(Paint.Style.FILL);//設置繪制風格

        mPaintText = new TextPaint();
        mPaintText.setColor(Color.BLUE);
        mPaintText.setStrokeWidth(5);
        mPaintText.setTextAlign(Paint.Align.CENTER);
        mPaintText.setTextSize(30);

        mPaintHour = new Paint();
        mPaintHour.setStrokeWidth(6);
        mPaintHour.setColor(Color.BLUE);
        mPaintHour.setAntiAlias(true);

        mPaintMinute = new Paint();
        mPaintMinute.setStrokeWidth(4);
        mPaintMinute.setColor(Color.BLUE);
        mPaintMinute.setAntiAlias(true);

        mPaintSec = new Paint();
        mPaintSec.setStrokeWidth(2);
        mPaintSec.setColor(Color.BLUE);
        mPaintSec.setAntiAlias(true);

        handler.sendEmptyMessage(START_ONDRAW);//向handler發送一個消息,讓它開啟重繪
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
        height = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);
        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int circleRadius ; //模擬時鐘的圓半徑大小
        if (width > height) {
            circleRadius = height / 2 -10;
        } else {
            circleRadius = width / 2 -10;
        }
        //畫出圓中心
        canvas.drawCircle(width / 2, height / 2, 5, mPaintCircle);
        //依次旋轉畫佈,畫出每個刻度和對應數字
        for (int i = 1; i <= 60; i++) {
            canvas.save();//保存當前畫佈
            if (i % 5 == 0) {
                //將畫佈進行以圓心以固定的角度旋轉進行旋轉
                canvas.rotate(360 / 60 * i, width / 2, height / 2);
                //設置字體大小,這裡是以圓半徑的十分之一大小
                mPaintText.setTextSize(circleRadius / 10);
                //如果繪制對應的數字時隻進行一次旋轉是不能達到目標的,需要再次以書寫文字的地方在進行反向旋轉這樣寫出來的就是正向的
                canvas.rotate(-360 / 60 * i, width / 2, height / 2 - circleRadius+5);
                canvas.drawText("" + i / 5, width / 2, height / 2 - circleRadius+circleRadius / 20 , mPaintText);
            } else {
                canvas.rotate(360 / 60 * i, width / 2, height / 2);
                //左起:起始位置x坐標,起始位置y坐標,終止位置x坐標,終止位置y坐標,畫筆(一個Paint對象)
                canvas.drawCircle(width/2,height/2-circleRadius,2,mPaintCircle);
            }
            canvas.restore();
        }

        int minute = mCalendar.get(Calendar.MINUTE);//得到當前分鐘數
        int hour = mCalendar.get(Calendar.HOUR);//得到當前小時數
        int sec = mCalendar.get(Calendar.SECOND);//得到當前秒數
        String time;
        if (sec < 10 && hour < 10 && minute < 10) { //都小於10
            time = "0" + hour + ":0" + minute + ":0" + sec; //02:02:02
        } else if (sec < 10 && hour < 10 && minute > 9) {//分鐘大於9
            time = "0" + hour + ":" + minute + ":0" + sec; //02:12:02
        } else if (sec > 9 && hour < 10 && minute < 10) {//秒大於9
            time = "0" + hour + ":0" + minute + ":" + sec; //02:02:12
        } else if (sec < 10 && hour > 9 && minute < 10) {//時大於9
            time = hour + ":0" + minute + ":0" + sec; //12:02:02
        } else if (sec < 10 && hour > 9 && minute > 9) {//時分於9
            time = hour + ":" + minute + ":0" + sec; //12:12:02
        } else if (sec > 9 && hour > 9 && minute < 10) {//時秒大於9
            time = hour + ":0" + minute + ":" + sec; //12:02:12
        } else if (sec > 9 && hour < 10 && minute > 9) {//分秒大於9
            time = "0" + hour + ":" + minute + ":" + sec; //02:12:12
        } else {
            time = hour + ":" + minute + ":" + sec; //12:12:12
        }
        //繪制中心下方的時間顯示
        mPaintText.setTextSize(circleRadius / 10 * 2);
        canvas.save();
        canvas.drawText(time, width / 2, height / 2 + circleRadius / 10 * 4, mPaintText);

        //繪制時分秒相應的指針
        float minuteDegree = minute / 60f * 360;//得到分針旋轉的角度
        canvas.save();
        canvas.rotate(minuteDegree, width / 2, height / 2);
        canvas.drawLine(width / 2, height / 2 - circleRadius + circleRadius / 3, width / 2, height / 2 + circleRadius / 6, mPaintMinute);
        canvas.restore();

        float hourDegree = (hour * 60 + minute) / 12f / 60 * 360;//得到時鐘旋轉的角度
        canvas.save();
        canvas.rotate(hourDegree, width / 2, height / 2);
        canvas.drawLine(width / 2, height / 2 - circleRadius + circleRadius / 2, width / 2, height / 2 + circleRadius / 9, mPaintHour);
        canvas.restore();

        float secDegree = sec / 60f * 360;//得到秒針旋轉的角度
        canvas.save();
        canvas.rotate(secDegree, width / 2, height / 2);
        canvas.drawLine(width / 2, height / 2 - circleRadius + 2, width / 2, height / 2 + circleRadius / 4, mPaintSec);
        canvas.restore();

    }
}

在佈局中進行調用

activity_main.xml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="com.eq.viewdemo.MainActivity">


   <com.eq.viewdemo.TimeClockView
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:background="#f99"/>

</RelativeLayout>

到此自定義的模擬時鐘就完成瞭,希望對有需要的朋友起到幫助。

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: