package com.example.huguomin.myclock.view; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Typeface; import android.util.AttributeSet; import android.view.View; /** * Created by huguomin on 2017/7/26. */ public class PuyaClock extends View{ private Paint mBigClockCircle; private Paint mCenterCircle; private Paint mDegreePaint; private Paint mTextPaint; private Paint mSecondPaint; private Paint mMinutePaint; private Paint mHourPaint; public PuyaClock(Context context) { super(context); initView(); } public PuyaClock(Context context, AttributeSet attrs) { super(context, attrs); initView(); } public PuyaClock(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initView(); } private void initView() { //初始化画笔,工具空间 //画大表盘的画笔 mBigClockCircle = new Paint(); mBigClockCircle.setAntiAlias(true); mBigClockCircle.setColor(Color.RED); mBigClockCircle.setStrokeWidth(25); mBigClockCircle.setStyle(Paint.Style.STROKE); //画中心小表盘的画笔 mCenterCircle = new Paint(); mCenterCircle.setAntiAlias(true); mCenterCircle.setColor(Color.BLUE); mCenterCircle.setStyle(Paint.Style.FILL); //画表盘刻度的画笔 mDegreePaint = new Paint(); mDegreePaint.setAntiAlias(true); mDegreePaint.setColor(Color.RED); mDegreePaint.setStrokeWidth(15); mDegreePaint.setTypeface(Typeface.DEFAULT_BOLD); mDegreePaint.setStyle(Paint.Style.STROKE); //画表盘数字的比 mTextPaint = new Paint(); mTextPaint.setAntiAlias(true); mTextPaint.setColor(Color.BLACK); mTextPaint.setTextSize(40); mTextPaint.setTextAlign(Paint.Align.CENTER); //居中 mTextPaint.setStyle(Paint.Style.FILL_AND_STROKE); mHourPaint = new Paint(); mHourPaint.setColor(Color.GRAY); mHourPaint.setAntiAlias(true); mHourPaint.setStrokeWidth(15); mHourPaint.setStyle(Paint.Style.STROKE); mMinutePaint = new Paint(); mMinutePaint.setColor(Color.LTGRAY); mMinutePaint.setAntiAlias(true); mMinutePaint.setStrokeWidth(10); mMinutePaint.setStyle(Paint.Style.STROKE); mSecondPaint = new Paint(); mSecondPaint.setColor(Color.RED); mSecondPaint.setAntiAlias(true); mSecondPaint.setStrokeWidth(10); mSecondPaint.setStyle(Paint.Style.STROKE); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); int width = getWidth(); //获取宽度,布局中的宽度、 int height = getHeight(); //绘制大的表盘, int radius = width / 2 - 50; canvas.drawCircle(width/2,height/2, radius,mBigClockCircle); //回执中心校表盘 canvas.drawCircle(width/2,height/2,30,mCenterCircle); //画表盘刻度 for (int i = 1; i <13; i++) { canvas.save(); canvas.rotate(i*30,width/2,height/2); //旋转的角度 canvas.drawLine(width/2,height/2-radius,width/2,height/2-radius+50,mDegreePaint); canvas.restore(); } int x =width/2; int y = height / 2 - radius + 50+40; for (int i = 1; i < 13; i++) { canvas.save(); canvas.rotate(i * 30, width / 2, height / 2); //旋转的角度,沿着中心圆的位置旋转一次画布 canvas.rotate(-i * 30, x, y); // 沿着子的中心点再旋转一次画布,让字竖直显示 canvas.drawText(i + "", x, y, mTextPaint); canvas.rotate(i * 30, x, y); //一定要记得转回来 canvas.restore(); canvas.save(); canvas.rotate(120, width / 2, height / 2);//旋转画布准备画时针 canvas.drawLine(width / 2, height / 2 - 100, width / 2, height / 2, mHourPaint);//画时针 canvas.restore();//重置画布 canvas.save(); canvas.rotate(135, width / 2, height / 2); canvas.drawLine(width / 2, height / 2 - 200, width / 2, height / 2, mMinutePaint); canvas.restore(); canvas.save(); canvas.rotate(10, width / 2, height / 2);//这里是顺时针旋转 canvas.drawLine(width / 2, height / 2 - 250, width / 2, height / 2, mSecondPaint); canvas.restore(); } } }
自定义view----画了个时钟
最新推荐文章于 2021-05-27 11:26:11 发布