您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關怎么在Android中通過自定義控件實現時鐘功能,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
自定義控件的代碼(ClockView.java):
package com.example.clock; import java.util.Calendar; import java.util.Date; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.os.Handler; import android.os.Looper; import android.os.Message; import android.util.AttributeSet; import android.view.View; public class ClockView extends View { private Paint circlePaint, dialPaint, numberPaint; // view 的寬高 private float mWidth, mHeight; // 圓的半徑 private float circleRadius; // 圓心X,Y坐標 private float circleX, circleY; private int second, minute; private double hour; private Handler handler = new Handler(Looper.getMainLooper()) { @Override public void handleMessage(Message msg) { super.handleMessage(msg); if (msg.what == 0) { invalidate(); } } }; public ClockView(Context context, AttributeSet attrs) { super(context, attrs); initPaint(); } private void initPaint() { // 刻盤圓,小時刻度,時針和分針的畫筆 circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG); circlePaint.setColor(Color.BLACK); circlePaint.setStyle(Paint.Style.STROKE); circlePaint.setStrokeWidth(10); // 分鐘刻度的畫筆 dialPaint = new Paint(Paint.ANTI_ALIAS_FLAG); dialPaint.setColor(Color.BLACK); dialPaint.setStrokeWidth(5); // 數字的畫筆 numberPaint = new Paint(Paint.ANTI_ALIAS_FLAG); numberPaint.setColor(Color.BLACK); numberPaint.setStrokeWidth(5); numberPaint.setTextSize(30); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); mWidth = getMeasuredWidth(); mHeight = getMeasuredHeight(); if (mWidth < mHeight) { // 圓的半徑為view的寬度的一半再減9,防止貼邊 circleRadius = mWidth / 2 - 9; circleX = mWidth / 2; circleY = mHeight / 2; } else { circleRadius = mHeight / 2 - 9; circleX = mWidth / 2; circleY = mHeight / 2; } } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); setTimes(); drawCirclePoint(canvas); drawCircle(canvas); drawDial(canvas); drawPointer(canvas); } /** * 圓心 * * @param canvas */ private void drawCirclePoint(Canvas canvas) { canvas.drawCircle(circleX, circleY, 5, circlePaint); } private void drawCircle(Canvas canvas) { canvas.drawCircle(circleX, circleY, circleRadius, circlePaint); } /** * 畫刻度及時間 * * @param canvas */ private void drawDial(Canvas canvas) { // 時鐘用長一點的刻度,畫筆用畫圓的畫筆 Point hourStartPoint = new Point(circleX, circleY - circleRadius); Point hourEndPoint = new Point(circleX, circleY - circleRadius + 40); // 分鐘的刻度要稍微短一些,畫筆用畫圓的畫筆 Point startPoint2 = new Point(circleX, circleY - circleRadius); Point endPoint2 = new Point(circleX, circleY - circleRadius + 10); // 開始畫刻度和數字,總共60個刻度,12個時鐘刻度,被5整除畫一個時鐘刻度,被其余的為分針刻度 String clockNumber; for (int i = 0; i < 60; i++) { if (i % 5 == 0) { if (i == 0) { clockNumber = "12"; } else { clockNumber = String.valueOf(i / 5); } // 時針刻度 canvas.drawLine(hourStartPoint.getX(), hourStartPoint.getY(), hourEndPoint.getX(), hourEndPoint.getY(), circlePaint); // 畫數字,需在時針刻度末端加30 canvas.drawText(clockNumber, circleX - numberPaint.measureText(clockNumber) / 2, hourEndPoint.getY() + 30, numberPaint); } else { // 畫分針刻度 canvas.drawLine(startPoint2.getX(), startPoint2.getY(), endPoint2.getX(), endPoint2.getY(), circlePaint); } // 畫布旋轉6度 canvas.rotate(360 / 60, circleX, circleY); } } /** * 畫指針 X點坐標 cos(弧度)*r Y點坐標 sin(弧度)*r toRadians將角度轉成弧度 * 安卓坐標系與數學坐標系不同的地方是X軸是相反的,所以為了調整方向,需要將角度+270度 * * @param canvas */ private void drawPointer(Canvas canvas) { canvas.translate(circleX, circleY); float hourX = (float) Math.cos(Math.toRadians(hour * 30 + 270)) * circleRadius * 0.5f; float hourY = (float) Math.sin(Math.toRadians(hour * 30 + 270)) * circleRadius * 0.5f; float minuteX = (float) Math.cos(Math.toRadians(minute * 6 + 270)) * circleRadius * 0.8f; float minuteY = (float) Math.sin(Math.toRadians(minute * 6 + 270)) * circleRadius * 0.8f; float secondX = (float) Math.cos(Math.toRadians(second * 6 + 270)) * circleRadius * 0.8f; float secondY = (float) Math.sin(Math.toRadians(second * 6 + 270)) * circleRadius * 0.8f; canvas.drawLine(0, 0, hourX, hourY, circlePaint); canvas.drawLine(0, 0, minuteX, minuteY, circlePaint); canvas.drawLine(0, 0, secondX, secondY, dialPaint); // 一秒重繪一次 handler.sendEmptyMessageDelayed(0, 1000); } public void startClock() { setTimes(); invalidate(); } private void setTimes() { Date date = new Date(); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); second = getTimes(date, Calendar.SECOND); minute = getTimes(date, Calendar.MINUTE); hour = getTimes(date, Calendar.HOUR) + minute / 12 * 0.2; } private int getTimes(Date date, int calendarField) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); return calendar.get(calendarField); } public void stopClock() { handler.removeMessages(0); } }
Point.java:
package com.example.clock; public class Point { private float x; private float y; public Point(float x, float y) { this.x = x; this.y = y; } public float getX() { return x; } public void setX(float x) { this.x = x; } public float getY() { return y; } public void setY(float y) { this.y = y; } }
Acitivity(ClockActivity.java):
package com.example.clock; import android.app.Activity; import android.os.Bundle; public class ClockActivity extends Activity { /** Called when the activity is first created. */ private ClockView clockView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); clockView = (ClockView) findViewById(R.id.clock); } @Override protected void onResume() { super.onResume(); clockView.startClock(); } @Override protected void onStop() { super.onStop(); clockView.stopClock(); } }
xml布局(main.xml):
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:gravity="center" android:layout_height="match_parent"> <com.example.customview.view.ClockView android:layout_width="match_parent" android:id="@+id/clock" android:layout_height="match_parent" /> </LinearLayout>
Android是一種基于Linux內核的自由及開放源代碼的操作系統,主要使用于移動設備,如智能手機和平板電腦,由美國Google公司和開放手機聯盟領導及開發。
關于怎么在Android中通過自定義控件實現時鐘功能就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。