您好,登錄后才能下訂單哦!
本文小編為大家詳細介紹“Android如何自定義模擬時鐘控件”,內容詳細,步驟清晰,細節處理妥當,希望這篇“Android如何自定義模擬時鐘控件”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學習新知識吧。
思路:重寫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>
讀到這里,這篇“Android如何自定義模擬時鐘控件”文章已經介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領會,如果想了解更多相關內容的文章,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。