1. Try to imitate this little alarm clock today.
2. Ideas:
- Draw the alarm disc first, and draw the pointer through path.
- Two ears are actually used two circles by a large circle to leave behind, and rotate a certain degree after the generation
3. Directly on the code:
1 Public classAlarmclockviewextendsView {2 3 Private Static FinalPorterduff.mode Mode =PorterDuff.Mode.DST_OUT;4 /**5 * Screen Adaptability parameters6 */7 Private Static Final floatBig_radiu = 1/6F;8 Private Static Final floatSmall_radiu = 1/12F;9 Private Static Final floatClockwise_length = 1/10F;Ten Private Static Final floatMinute_length = 1/12f; One Private Static Final floatMask_radiu = 1/4F; A - // Large Circle Coil - PrivatePaint Mpaint; the //pointer Paint - PrivatePaint Zhenpaint; - //Small round paint - PrivatePaint Smallcirclepaint; + Privatepath Path; - Private intScreenw, screenh; + /** A * Circle Radius at * - * @paramContext - */ - Private floatRadius_big, Small_radiu, Clockwise_length, Minute_length, - Mask_radiu; - in PublicAlarmclockview (Context context) { - Super(context); to } + - PublicAlarmclockview (Context context, AttributeSet attrs) { the Super(context, attrs); * $Mpaint =NewPaint (paint.anti_alias_flag);Panax Notoginseng Mpaint.setstyle (Paint.Style.STROKE); - Mpaint.setcolor (color.white); theMpaint.setstrokewidth (40); + AZhenpaint =NewPaint (paint.anti_alias_flag); the Zhenpaint.setstyle (Paint.Style.STROKE); + Zhenpaint.setcolor (color.white); -Zhenpaint.setstrokewidth (20); $ $Smallcirclepaint =NewPaint (paint.anti_alias_flag); - Smallcirclepaint.setstyle (Paint.Style.FILL); - Smallcirclepaint.setcolor (color.white); the -Screenw =Newmeasureutil (). Getwindowwidth (context);WuyiScreenh =Newmeasureutil (). Getwindowheigh (context); the - intSize =math.min (Screenw, screenh); WuRadius_big = Size *Big_radiu; -Small_radiu = Size *Small_radiu; Aboutclockwise_length = Size *clockwise_length; $minute_length = Size *minute_length; -Mask_radiu = Size *Mask_radiu; - //Set Pointer -Path =NewPath (); APath.moveto (SCREENW/2, SCREENH/2 + 10); +Path.lineto (SCREENW/2, SCREENH/2-clockwise_length + 10); thePath.moveto (SCREENW/2-Ten, SCREENH/2 + 10); -Path.lineto (SCREENW/2 + minute_length, SCREENH/2 + 10); $ } the the @Override the protected voidOnDraw (canvas canvas) { the Super. OnDraw (canvas); -Canvas.drawcolor (Color.argb (255, 255, 128, 103)); in theCanvas.drawcircle (SCREENW/2, SCREENH/2, Radius_big, mpaint); the Canvas.drawpath (path, zhenpaint); About the //Draw ears Round theDrawearcircle (Canvas, 30); theDrawearcircle (Canvas,-30); + - } the Bayi Private voidDrawearcircle (Canvas canvas,intdegree) { the intLayerid = Canvas.savelayer (0, 0, Screenw, Screenh,NULL, the canvas.all_save_flag); -Canvas.rotate (degree, SCREENW/2, SCREENH/2); -Canvas.drawcircle (SCREENW/2, SCREENH/2-Radius_big-Small_radiu, the Small_radiu, smallcirclepaint); the //use a big circle to mask thePorterduffxfermode Xfermode =NewPorterduffxfermode (MODE); the Smallcirclepaint.setxfermode (xfermode); -Canvas.drawcircle (SCREENW/2, SCREENH/2, Mask_radiu, the smallcirclepaint); theSmallcirclepaint.setxfermode (NULL); the Canvas.restoretocount (Layerid);94 } the the}
4. The effect after operation is:
Android Picture Alarm