欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android自定義模擬時鐘控件

 更新時間:2022年01月13日 08:24:31   作者:鳳洋  
這篇文章主要為大家詳細介紹了Android自定義模擬時鐘控件,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下<BR>

本文實例為大家分享了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>

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

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

相關文章

最新評論