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發(fā)送一個消息,讓它開啟重繪 ? ? } ? ? @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開發(fā)基于ViewPager+GridView實現(xiàn)仿大眾點評橫向滑動功能
這篇文章主要介紹了Android開發(fā)基于ViewPager+GridView實現(xiàn)仿大眾點評橫向滑動功能,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-09-09android仿新聞閱讀器菜單彈出效果實例(附源碼DEMO下載)
本篇文章介紹了android仿新聞閱讀器菜單彈出效果實例,現(xiàn)在很多閱讀器都有這個功能,需要的朋友可以看一下。2016-11-11Android 詳解沉浸式狀態(tài)欄的實現(xiàn)流程
沉浸式就是要給用戶提供完全沉浸的體驗,使用戶有一種置身于虛擬世界之中的感覺。沉浸式模式就是整個屏幕中顯示都是應用的內容,沒有狀態(tài)欄也沒有導航欄,用戶不會被一些系統(tǒng)的界面元素所打擾,讓我們來實現(xiàn)下網上傳的沸沸揚揚的安卓沉浸式狀態(tài)欄2021-11-11Android編程之SMS讀取短信并保存到SQLite的方法
這篇文章主要介紹了Android編程之SMS讀取短信并保存到SQLite的方法,涉及Android針對SMS短信及SQLite數據庫的相關操作技巧,需要的朋友可以參考下2015-11-11Android中自定義的dialog中的EditText無法彈出輸入法解決方案
這篇文章主要介紹了Android中自定義的dialog中的EditText無法彈出輸入法解決方案,需要的朋友可以參考下2017-04-04