Android自定義View實現(xiàn)時鐘效果
本文實例為大家分享了Android自定義View實現(xiàn)時鐘效果的具體代碼,供大家參考,具體內(nèi)容如下
自定義時鐘
初學(xué)自定義View,先畫一個不太成熟的時鐘(甚至只有秒針)
時鐘效果

@SuppressLint("DrawAllocation")
public class ClockView extends View {
? ? private final Context mContext;
? ? private Canvas mCanvas;// 畫布
? ? private Paint clockPaint;// 表盤畫筆
? ? private Paint textPaint;// 文字畫筆
? ? private Paint secondPaint;// 秒針畫筆
? ? private int x,y;// 表中心坐標(biāo)
? ? private Thread refreshThread;
? ? private boolean isStop = false;
? ? // 用于獲取當(dāng)前秒數(shù)
? ? private ?Date currentDate;
? ? private SimpleDateFormat sp = new SimpleDateFormat("ss");
? ? @SuppressLint("HandlerLeak")
? ? private final Handler mHandler = new Handler(){
? ? ? ? @Override
? ? ? ? public void handleMessage(@NonNull Message msg) {
? ? ? ? ? ? super.handleMessage(msg);
? ? ? ? ? ? if(msg.what == 0){
? ? ? ? ? ? ? ? invalidate();//每隔一秒刷新一次
? ? ? ? ? ? }
? ? ? ? }
? ? };
? ? public ClockView(Context context) {
? ? ? ? this(context,null);
? ? }
? ? public ClockView(Context context, @Nullable AttributeSet attrs) {
? ? ? ? this(context,null,0);
? ? }
? ? public ClockView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
? ? ? ? this(context,null,0,0);
? ? }
? ? public ClockView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
? ? ? ? super(context, attrs, defStyleAttr, defStyleRes);
? ? ? ? this.mContext = context;
? ? }
? ? @Override
? ? protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
? ? ? ? int widthMeasure;
? ? ? ? // 設(shè)置寬高一致
? ? ? ? if(widthMeasureSpec > heightMeasureSpec){
? ? ? ? ? ? super.onMeasure(heightMeasureSpec, heightMeasureSpec);
? ? ? ? ? ? widthMeasure ?= getMeasuredHeight();
? ? ? ? }else{
? ? ? ? ? ? super.onMeasure(widthMeasureSpec, widthMeasureSpec);
? ? ? ? ? ? widthMeasure = getMeasuredWidth();
? ? ? ? }
? ? ? ? initPaint(widthMeasure);
? ? }
? ? private void initPaint(int width){
? ? ? ? // 表盤畫筆
? ? ? ? clockPaint = new Paint();
? ? ? ? clockPaint.setColor(mContext.getColor(R.color.black));// 顏色
? ? ? ? clockPaint.setAntiAlias(true);// 抗鋸齒
? ? ? ? clockPaint.setStyle(Paint.Style.STROKE);// 樣式
? ? ? ? clockPaint.setStrokeWidth(width/80f);// 寬度
? ? ? ? //字體畫筆
? ? ? ? textPaint = new Paint();
? ? ? ? textPaint.setColor(mContext.getColor(R.color.black));
? ? ? ? textPaint.setAntiAlias(true);
? ? ? ? textPaint.setStyle(Paint.Style.FILL);
? ? ? ? textPaint.setTextSize(width/16f);
? ? ? ? textPaint.setTextAlign(Paint.Align.CENTER);
? ? ? ? // 秒針畫筆
? ? ? ? secondPaint = new Paint();
? ? ? ? secondPaint.setColor(mContext.getColor(R.color.red));
? ? ? ? secondPaint.setAntiAlias(true);
? ? ? ? secondPaint.setStyle(Paint.Style.FILL);
? ? ? ? secondPaint.setStrokeWidth(5f);
? ? }
? ? @Override
? ? protected void onDraw(Canvas canvas) {
? ? ? ? super.onDraw(canvas);
? ? ? ? mCanvas = canvas;
? ? ? ? // 獲取畫布中心坐標(biāo)
? ? ? ? x = getWidth() / 2;
? ? ? ? y = getHeight() / 2;
? ? ? ? // 繪制表盤
? ? ? ? mCanvas.drawCircle(x, y,getWidth()/40f, clockPaint);// 表盤中心
? ? ? ? mCanvas.drawCircle(x, y, x -getWidth()/40f, clockPaint);// 表盤邊框
? ? ? ? // 繪制刻度
? ? ? ? for(int i = 1;i <= 60;i++){
? ? ? ? ? ? mCanvas.rotate(6, x, y);
? ? ? ? ? ? if(i%5 == 0){
? ? ? ? ? ? ? ? // 繪制大刻度
? ? ? ? ? ? ? ? mCanvas.drawLine(x, getWidth()*3/80f, x, getWidth()*5/80f, clockPaint);
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? // 繪制小刻度
? ? ? ? ? ? ? ? mCanvas.drawLine(x, getWidth()*3/80f, x, getWidth()/20f, clockPaint);
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? // 繪制 1-12小時 字體
? ? ? ? for(int i = 1;i <= 60;i++){
? ? ? ? ? ? if(i%5 == 0){
? ? ? ? ? ? ? ? float x1 = (float) Math.sin(Math.toRadians(6 * i)) * (y * 3 / 4f) + x;
? ? ? ? ? ? ? ? float y1 = y - (float) Math.cos(Math.toRadians(6 * i)) * (y * 3 / 4f) + getWidth()/40f;
? ? ? ? ? ? ? ? mCanvas.drawText(String.valueOf(i/5), x1, y1, textPaint);
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? // 繪制秒針
? ? ? ? currentDate = new Date(System.currentTimeMillis());
? ? ? ? int ss = Integer.parseInt(sp.format(currentDate));// 獲取當(dāng)前秒數(shù)
? ? ? ? // 根據(jù)當(dāng)前秒數(shù) 計算出秒針的 start 及 end 坐標(biāo)
? ? ? ? float sin = (float) Math.sin(Math.toRadians(6 * ss));
? ? ? ? float cos = (float) Math.cos(Math.toRadians(6 * ss));
? ? ? ? float x0 = x - sin * (y / 10f);
? ? ? ? float y0 = y + cos * (y / 10f);
? ? ? ? float x1 = x + sin * (y * 13 / 20f);
? ? ? ? float y1 = y - cos * (y * 13 / 20f);
? ? ? ? mCanvas.drawLine(x0, y0, x1, y1, secondPaint);
? ? ? ? mCanvas.drawCircle(x, y,getWidth()/80f, secondPaint);
? ? }
? ? @Override
? ? protected void onAttachedToWindow() {
? ? ? ? super.onAttachedToWindow();
? ? ? ? refreshThread = new Thread(){
? ? ? ? ? ? @Override
? ? ? ? ? ? public void run() {
? ? ? ? ? ? ? ? super.run();
? ? ? ? ? ? ? ? while (!isStop){
? ? ? ? ? ? ? ? ? ? SystemClock.sleep(1000);
? ? ? ? ? ? ? ? ? ? mHandler.sendEmptyMessage(0);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? };
? ? ? ? refreshThread.start();
? ? }
? ? @Override
? ? protected void onDetachedFromWindow() {
? ? ? ? super.onDetachedFromWindow();
? ? ? ? mHandler.removeCallbacksAndMessages(null);
? ? ? ? isStop = true;
? ? }以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android獲取設(shè)備CPU核數(shù)、時鐘頻率以及內(nèi)存大小的方法
- Android多功能時鐘開發(fā)案例(實戰(zhàn)篇)
- android實現(xiàn)widget時鐘示例分享
- Android 仿日歷翻頁、仿htc時鐘翻頁、數(shù)字翻頁切換效果
- Android多功能時鐘開發(fā)案例(基礎(chǔ)篇)
- android高仿小米時鐘(使用Camera和Matrix實現(xiàn)3D效果)
- Android實現(xiàn)簡單時鐘View的方法
- Android自定義動態(tài)壁紙開發(fā)(時鐘)
- Android編程基于自定義控件實現(xiàn)時鐘功能的方法
- Android仿小米時鐘效果
相關(guān)文章
android文件操作——讀取assets和raw文件下的內(nèi)容
本篇文章主要介紹了android文件操作——讀取assets和raw文件下的內(nèi)容,并附簡單實例代碼,需要的朋友可以參考下。2016-10-10
android ScrollView實現(xiàn)水平滑動回彈
這篇文章主要為大家詳細(xì)介紹了android ScrollView實現(xiàn)水平滑動回彈,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-04-04
Android開發(fā)教程之如何屏蔽View的重復(fù)點擊
這篇文章主要給大家介紹了關(guān)于Android開發(fā)教程之如何屏蔽View的重復(fù)點擊的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-09-09
詳解Android中Fragment的兩種創(chuàng)建方式
本篇文章主要介紹了Android中Fragment的兩種創(chuàng)建方式,具有一定的參考價值,有興趣的可以了解一下。2016-12-12
Android自定義控件實現(xiàn)icon+文字的多種效果
這篇文章主要為大家詳細(xì)介紹了Android自定義控件實現(xiàn)icon+文字的多種效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-01-01
Android中ViewPager的PagerTabStrip與PagerTitleStrip用法實例
這篇文章主要介紹了Android中ViewPager的PagerTabStrip與PagerTitleStrip用法實例,這兩個子控件一般被用作添加標(biāo)題,在實際效果上并不是那么好控制,使用的時候需要謹(jǐn)慎,需要的朋友可以參考下2016-06-06
解決Android加殼過程中mprotect調(diào)用失敗的原因分析
本文探討的主要內(nèi)容是mprotect調(diào)用失敗的根本原因,以及在加殼實現(xiàn)中的解決方案,通過本文的闡述,一方面能夠幫助遇到同類問題的小伙伴解決心中的疑惑,另一方面能夠給大家提供可落地的實現(xiàn)方案,需要的朋友可以參考下2022-01-01
Android開發(fā)自學(xué)筆記(五):使用代碼控制界面
這篇文章主要介紹了Android開發(fā)自學(xué)筆記(五):使用代碼控制界面,本文講解了添加第二個layout、添加MyActivity的code、setup函數(shù)、getResult函數(shù)等內(nèi)容,需要的朋友可以參考下2015-04-04
淺析Android手機(jī)衛(wèi)士之手機(jī)實現(xiàn)短信指令獲取位置
這篇文章主要介紹了淺析Android手機(jī)衛(wèi)士之手機(jī)實現(xiàn)短信指令獲取位置的相關(guān)資料,需要的朋友可以參考下2016-04-04

