1.今天就来模仿一下这个小闹钟的
2.思路:
- 先画闹钟的圆盘 ,在通过Path来画指针
- 两个耳朵其实就是用两个圆被一个大圆截取后留下的,并旋转一定度数后生成
3.直接上代码:
1 public class AlarmClockView extends View { 2 3 private static final PorterDuff.Mode MODE = PorterDuff.Mode.DST_OUT; 4 /** 5 * 屏幕适应性参数 6 */ 7 private static final float BIG_RADIU = 1 / 6F; 8 private static final float SMALL_RADIU = 1 / 12F; 9 private static final float CLOCKWISE_LENGTH = 1 / 10F; 10 private static final float MINUTE_LENGTH = 1 / 12f; 11 private static final float MASK_RADIU = 1 / 4F; 12 13 // 大圆盘圈 14 private Paint mPaint; 15 // 指针Paint 16 private Paint zhenPaint; 17 // 小圆Paint 18 private Paint smallCirclePaint; 19 private Path path; 20 private int screenW, screenH; 21 /** 22 * 大圆半径 23 * 24 * @param context 25 */ 26 private float radius_big, small_radiu, clockwise_length, minute_length, 27 mask_radiu; 28 29 public AlarmClockView(Context context) { 30 super(context); 31 } 32 33 public AlarmClockView(Context context, AttributeSet attrs) { 34 super(context, attrs); 35 36 mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 37 mPaint.setStyle(Paint.Style.STROKE); 38 mPaint.setColor(Color.WHITE); 39 mPaint.setStrokeWidth(40); 40 41 zhenPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 42 zhenPaint.setStyle(Paint.Style.STROKE); 43 zhenPaint.setColor(Color.WHITE); 44 zhenPaint.setStrokeWidth(20); 45 46 smallCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG); 47 smallCirclePaint.setStyle(Paint.Style.FILL); 48 smallCirclePaint.setColor(Color.WHITE); 49 50 screenW = new MeasureUtil().getWindowWidth(context); 51 screenH = new MeasureUtil().getWindowHeigh(context); 52 53 int size = Math.min(screenW, screenH); 54 radius_big = size * BIG_RADIU; 55 small_radiu = size * SMALL_RADIU; 56 clockwise_length = size * CLOCKWISE_LENGTH; 57 minute_length = size * MINUTE_LENGTH; 58 mask_radiu = size * MASK_RADIU; 59 // 设置指针 60 path = new Path(); 61 path.moveTo(screenW / 2, screenH / 2 + 10); 62 path.lineTo(screenW / 2, screenH / 2 - clockwise_length + 10); 63 path.moveTo(screenW / 2 - 10, screenH / 2 + 10); 64 path.lineTo(screenW / 2 + minute_length, screenH / 2 + 10); 65 } 66 67 @Override 68 protected void onDraw(Canvas canvas) { 69 super.onDraw(canvas); 70 canvas.drawColor(Color.argb(255, 255, 128, 103)); 71 72 canvas.drawCircle(screenW / 2, screenH / 2, radius_big, mPaint); 73 canvas.drawPath(path, zhenPaint); 74 75 // 画耳朵圆 76 drawEarCircle(canvas, 30); 77 drawEarCircle(canvas, -30); 78 79 } 80 81 private void drawEarCircle(Canvas canvas, int degree) { 82 int layerID = canvas.saveLayer(0, 0, screenW, screenH, null, 83 Canvas.ALL_SAVE_FLAG); 84 canvas.rotate(degree, screenW / 2, screenH / 2); 85 canvas.drawCircle(screenW / 2, screenH / 2 - radius_big - small_radiu, 86 small_radiu, smallCirclePaint); 87 // 用一个大圆去遮罩 88 PorterDuffXfermode xfermode = new PorterDuffXfermode(MODE); 89 smallCirclePaint.setXfermode(xfermode); 90 canvas.drawCircle(screenW / 2, screenH / 2, mask_radiu, 91 smallCirclePaint); 92 smallCirclePaint.setXfermode(null); 93 canvas.restoreToCount(layerID); 94 } 95 96 }
4.运行后的效果是:
时间: 2024-10-21 15:08:55