|
| 1 | +using System; |
| 2 | +using Android.App; |
| 3 | +using Android.Content; |
| 4 | +using Android.Graphics; |
| 5 | +using Android.OS; |
| 6 | +using Android.Runtime; |
| 7 | +using Android.Util; |
| 8 | +using Android.Views; |
| 9 | +using Android.Widget; |
| 10 | + |
| 11 | +namespace AnalogClock.Android |
| 12 | +{ |
| 13 | + public class ClockView : View |
| 14 | + { |
| 15 | + Path tickMarks; |
| 16 | + Path hourHand; |
| 17 | + Path minuteHand; |
| 18 | + Path secondHand; |
| 19 | + |
| 20 | + Paint paint = new Paint(); |
| 21 | + DashPathEffect minuteTickDashEffect = |
| 22 | + new DashPathEffect (new float[] { 0.1f, 3 * (float)Math.PI - 0.1f}, 0); |
| 23 | + |
| 24 | + DashPathEffect hourTickDashEffect = |
| 25 | + new DashPathEffect (new float[] { 0.1f, 15 * (float)Math.PI - 0.1f}, 0); |
| 26 | + |
| 27 | + float hourAngle, minuteAngle, secondAngle; |
| 28 | + |
| 29 | + public ClockView (Context context) : |
| 30 | + base (context) |
| 31 | + { |
| 32 | + Initialize (); |
| 33 | + } |
| 34 | + |
| 35 | + public ClockView (Context context, IAttributeSet attrs) : |
| 36 | + base (context, attrs) |
| 37 | + { |
| 38 | + Initialize (); |
| 39 | + } |
| 40 | + |
| 41 | + public ClockView (Context context, IAttributeSet attrs, int defStyle) : |
| 42 | + base (context, attrs, defStyle) |
| 43 | + { |
| 44 | + Initialize (); |
| 45 | + } |
| 46 | + |
| 47 | + void Initialize () |
| 48 | + { |
| 49 | + // Create Paint for all drawing |
| 50 | + paint = new Paint (); |
| 51 | + |
| 52 | + // All paths are based on 100-unit clock radius |
| 53 | + // centered at (0, 0). |
| 54 | + |
| 55 | + // Define circle for tick marks. |
| 56 | + tickMarks = new Path (); |
| 57 | + tickMarks.AddCircle (0, 0, 90, Path.Direction.Cw); |
| 58 | + |
| 59 | + // Hour, minute, second hands defined to point straight up. |
| 60 | + |
| 61 | + // Define hour hand. |
| 62 | + hourHand = new Path (); |
| 63 | + hourHand.MoveTo (0, -60); |
| 64 | + hourHand.CubicTo (0, -30, 20, -30, 5, -20); |
| 65 | + hourHand.LineTo (5, 0); |
| 66 | + hourHand.CubicTo (5, 7.5f, -5, 7.5f, -5, 0); |
| 67 | + hourHand.LineTo (-5, -20); |
| 68 | + hourHand.CubicTo (-20, -30, 0, -30, 0, -60); |
| 69 | + hourHand.Close (); |
| 70 | + |
| 71 | + // Define minute hand. |
| 72 | + minuteHand = new Path (); |
| 73 | + minuteHand.MoveTo (0, -80); |
| 74 | + minuteHand.CubicTo (0, -75, 0, -70, 2.5f, -60); |
| 75 | + minuteHand.LineTo (2.5f, 0); |
| 76 | + minuteHand.CubicTo (2.5f, 5, -2.5f, 5, -2.5f, 0); |
| 77 | + minuteHand.LineTo (-2.5f, -60); |
| 78 | + minuteHand.CubicTo (0, -70, 0, -75, 0, -80); |
| 79 | + minuteHand.Close (); |
| 80 | + |
| 81 | + // Define second hand. |
| 82 | + secondHand = new Path (); |
| 83 | + secondHand.MoveTo (0, 10); |
| 84 | + secondHand.LineTo (0, -80); |
| 85 | + } |
| 86 | + |
| 87 | + // Called from MainActivity. |
| 88 | + public void SetClockHandAngles(float hourAngle, float minuteAngle, float secondAngle) |
| 89 | + { |
| 90 | + this.hourAngle = hourAngle; |
| 91 | + this.minuteAngle = minuteAngle; |
| 92 | + this.secondAngle = secondAngle; |
| 93 | + |
| 94 | + this.Invalidate (); |
| 95 | + } |
| 96 | + |
| 97 | + protected override void OnDraw (Canvas canvas) |
| 98 | + { |
| 99 | + base.OnDraw (canvas); |
| 100 | + |
| 101 | + // Clear screen to pink. |
| 102 | + paint.Color = new Color (255, 204, 204); |
| 103 | + canvas.DrawPaint (paint); |
| 104 | + |
| 105 | + // Overall transforms to shift (0, 0) to center and scale. |
| 106 | + canvas.Translate (this.Width / 2, this.Height / 2); |
| 107 | + float scale = Math.Min (this.Width, this.Height) / 2.0f / 100; |
| 108 | + canvas.Scale (scale, scale); |
| 109 | + |
| 110 | + // Attributes for tick marks. |
| 111 | + paint.Color = Color.Black; |
| 112 | + paint.StrokeCap = Paint.Cap.Round; |
| 113 | + paint.SetStyle (Paint.Style.Stroke); |
| 114 | + |
| 115 | + // Set line dash to draw tick marks for every minute. |
| 116 | + paint.StrokeWidth = 3; |
| 117 | + paint.SetPathEffect (minuteTickDashEffect); |
| 118 | + canvas.DrawPath (tickMarks, paint); |
| 119 | + |
| 120 | + // Set line dash to draw tick marks for every hour. |
| 121 | + paint.StrokeWidth = 6; |
| 122 | + paint.SetPathEffect (hourTickDashEffect); |
| 123 | + canvas.DrawPath (tickMarks, paint); |
| 124 | + |
| 125 | + // Set attributes common to all clock hands. |
| 126 | + Color strokeColor = Color.Black; |
| 127 | + Color fillColor = Color.Blue; |
| 128 | + paint.StrokeWidth = 2; |
| 129 | + paint.SetPathEffect (null); |
| 130 | + |
| 131 | + // Draw hour hand. |
| 132 | + canvas.Save (); |
| 133 | + canvas.Rotate (this.hourAngle); |
| 134 | + paint.Color = fillColor; |
| 135 | + paint.SetStyle (Paint.Style.Fill); |
| 136 | + canvas.DrawPath (hourHand, paint); |
| 137 | + paint.Color = strokeColor; |
| 138 | + paint.SetStyle (Paint.Style.Stroke); |
| 139 | + canvas.DrawPath (hourHand, paint); |
| 140 | + canvas.Restore (); |
| 141 | + |
| 142 | + // Draw minute hand. |
| 143 | + canvas.Save (); |
| 144 | + canvas.Rotate (this.minuteAngle); |
| 145 | + paint.Color = fillColor; |
| 146 | + paint.SetStyle (Paint.Style.Fill); |
| 147 | + canvas.DrawPath (minuteHand, paint); |
| 148 | + paint.Color = strokeColor; |
| 149 | + paint.SetStyle (Paint.Style.Stroke); |
| 150 | + canvas.DrawPath (minuteHand, paint); |
| 151 | + canvas.Restore (); |
| 152 | + |
| 153 | + // Draw second hand. |
| 154 | + canvas.Save (); |
| 155 | + canvas.Rotate (this.secondAngle); |
| 156 | + paint.Color = strokeColor; |
| 157 | + paint.SetStyle (Paint.Style.Stroke); |
| 158 | + canvas.DrawPath (secondHand, paint); |
| 159 | + canvas.Restore (); |
| 160 | + } |
| 161 | + } |
| 162 | +} |
| 163 | + |
0 commit comments