package com.domain.zjl.androidhero;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import java.util.Calendar;
/**
* Created by xiaoji94shen on 2016/9/1.
*/
public class MyClockView extends View {
private Handler mHandler;
private Runnable mRunnable;
private Paint mPaint;
public MyClockView(Context context) {
super(context);
init();
}
public MyClockView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
mHandler = new Handler();
initPaint();
mRunnable = new Runnable() {
@Override
public void run() {
postInvalidate();
mHandler.postDelayed(this, 1000);
}
};
mHandler.postDelayed(mRunnable, 1000);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
drawBigCircle(canvas);
drawLinesAndNumbers(canvas);
drawPoint(canvas);
drawLines(canvas);
}
private void initPaint() {
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setColor(Color.BLACK);
}
private void drawLines(Canvas canvas) {
Calendar calendar = Calendar.getInstance();
float hour = calendar.get(Calendar.HOUR);
float min = calendar.get(Calendar.MINUTE);
float secode = calendar.get(Calendar.SECOND);
mPaint.setStyle(Paint.Style.FILL);
float startX = getWidth() / 2;
float endX = startX;
float startY = getHeight() / 2;
float endY = startY - 80;
canvas.save();
float hourDegree = (hour + min / 60) * (360 / 12);
canvas.rotate(hourDegree, getWidth() / 2, getHeight() / 2);
canvas.drawLine(startX, startY, endX, endY, mPaint);
canvas.restore();
canvas.save();
float minDegree = (min + secode / 60) * 360 / 60;
canvas.rotate(minDegree, getWidth() / 2, getHeight() / 2);
canvas.drawLine(startX, startY, endX, endY - 20, mPaint);
canvas.restore();
canvas.save();
float secondeDegree = secode * 360 / 60;
canvas.rotate(secondeDegree, getWidth() / 2, getHeight() / 2);
canvas.drawLine(startX, startY, endX, endY - 40, mPaint);
canvas.restore();
}
private void drawPoint(Canvas canvas) {
mPaint.setStyle(Paint.Style.FILL);
canvas.drawCircle(getWidth() / 2, getHeight() / 2, 6, mPaint);
}
private void drawBigCircle(Canvas canvas) {
mPaint.setStyle(Paint.Style.STROKE);
canvas.drawCircle(getWidth() / 2, getHeight() / 2, getWidth() / 2 - 4, mPaint);
}
private void drawLinesAndNumbers(Canvas canvas) {
canvas.save();
int logHeight = 16;
int shortHeight = 8;
int startX = getWidth() / 2;
int stopX = startX;
int radius = getWidth() / 2 - 4;
int startY = getHeight() / 2 - radius;
int stopY = 0;
int degree = 360 / 60;
mPaint.setStyle(Paint.Style.FILL);
mPaint.setTextSize(25);
String[] numbers = new String[]{"12", "1", "2", "3", "4", "5",
"6", "7", "8", "9", "10", "11"};
int j = 0;
for (int i = 0; i < 60; i++) {
if (i % 5 == 0) {
stopY = startY + logHeight;
j++;
} else {
stopY = startY + shortHeight;
}
canvas.drawLine(startX, startY, stopX, stopY, mPaint);
if (i % 5 == 0) {
String num = numbers[j - 1];
Rect textBounds = new Rect();
mPaint.getTextBounds(num, 0, num.length(), textBounds);
int textStartX = (int) (startX - mPaint.measureText(num) / 2);
int textStartY = (int) (stopY + textBounds.height()) + 2;
canvas.drawText(numbers[j - 1], textStartX, textStartY, mPaint);
}
canvas.rotate(degree, getWidth() / 2, getHeight() / 2);
}
canvas.restore();
}
}
android 自定义view实现时钟
最新推荐文章于 2023-12-20 10:51:41 发布