網站首頁 編程語言 正文
本文實例為大家分享了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>
到此自定義的模擬時鐘就完成了,希望對有需要的朋友起到幫助。
原文鏈接:https://blog.csdn.net/MySuperGirl/article/details/73107192
相關推薦
- 2022-12-04 python亂序字符串排序的實現方式_python
- 2022-09-18 python實現修改xml文件內容_python
- 2023-05-15 shell參數換行與shell輸出換行的方法實例_linux shell
- 2023-11-26 在 el-table 中嵌入 el-checkbox el-input el-upload 多組件,
- 2022-11-06 react中關于Context/Provider/Consumer傳參的使用_React
- 2022-09-19 Tomcat配置HTTPS訪問的實現步驟_Tomcat
- 2022-08-04 Django框架之路由用法_python
- 2022-03-09 C++之const限定符詳解_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支