-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClockView.java
327 lines (290 loc) · 13.6 KB
/
ClockView.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package study.amadey.customview;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import java.time.Clock;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.EnumSet;
/**
* Created by Amadey on 10/18/2018.
*/
public class ClockView extends View {
private static final int NUMBER_OF_MINUTES = 60;
private static final int NUMBER_OF_HOURS = 12;
private static final int DEGREES_IN_CIRCLE = 360;
private static final int EVERY_NTH_BIG_MINUTE = 5;
private static final int STARTING_ANGLE_FOR_CIRCLE_DRAWING = -90;
private boolean isZoomNeeded = false;
private ClockHandDrawingType chosenClockHandDrawType = ClockHandDrawingType.ARC;
private float[] alphas = new float[2];
private RectF rect = new RectF();
private RectF innerRect = new RectF();
private Paint minuteHandPaint = ClockViewPaintFactory.produceMinuteHandPaint();
private Paint watchFacePaint = ClockViewPaintFactory.produceWatchFacePaint();
private Paint timeLabelPaint = ClockViewPaintFactory.produceTimeLabelPaint();
private Paint hourHandPaint = ClockViewPaintFactory.produceHourHandPaint();
private Paint textPaint = ClockViewPaintFactory.produceTextPaintForZoom(getContext().getResources().getDisplayMetrics().density);
private Point canvasCenter = new Point();
private Point zoomInPoint = new Point();
private Point startingPointOfDrawingRotation = new Point();
private Point pointOfTouch = new Point();
private Radius radius = new Radius();
private ClockElement currentlyChosenClockElement = ClockElement.HOUR;
private String zoomInText;
private ArrayList<ClockElement> clockElements = new ArrayList<>(EnumSet.allOf(ClockElement.class));
private ArrayList<Paint> clockHandsPaintList = new ArrayList<>(Arrays.asList(minuteHandPaint, hourHandPaint));
public ClockView(Context context) {
super(context);
}
public ClockView(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
}
public ClockView(Context context, AttributeSet attributeSet, int defStyle) {
super(context, attributeSet, defStyle);
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
rect = null;
innerRect = null;
minuteHandPaint = null;
watchFacePaint = null;
timeLabelPaint = null;
hourHandPaint = null;
textPaint = null;
canvasCenter = null;
zoomInPoint = null;
startingPointOfDrawingRotation = null;
pointOfTouch = null;
radius = null;
clockElements.clear();
clockElements = null;
clockHandsPaintList.clear();
clockHandsPaintList = null;
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
canvasCenter.setX(getWidth() / 2);
canvasCenter.setY(getHeight() / 2);
setAppropriateRectForCircle(0, 0, getWidth(), getHeight());
radius.setClockFrame(rect);
}
private void setAppropriateRectForInnerCircle(Point center, int radius) {
int newLeft = center.getX() - radius;
int newTop = center.getY() - radius;
int newRight = center.getX() + radius;
int newBottom = center.getY() + radius;
innerRect.set(newLeft, newTop, newRight, newBottom);
}
private void setAppropriateRectForCircle(int left, int top, int right, int bottom) {
int circleDiameter = getCircleDiameter(left, top, right, bottom);
int newLeft = left + getAppropriateOffset(left, right, circleDiameter);
int newTop = top + getAppropriateOffset(top, bottom, circleDiameter);
int newRight = newLeft + circleDiameter;
int newBottom = newTop + circleDiameter;
rect.set(newLeft, newTop, newRight, newBottom);
}
private int getAppropriateOffset(int start, int end, int bestFitLength) {
return (end - start - bestFitLength) / 2;
}
private int getCircleDiameter(int left, int top, int right, int bottom) {
int differenceHeight = bottom - top;
int differenceWidth = right - left;
return differenceHeight > differenceWidth ? differenceWidth : differenceHeight;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
drawClock(canvas, alphas);
drawTimeLabelsOfWatchFace(canvas, clockElements);
if (isZoomNeeded) {
zoomInOnElement(canvas);
}
}
private void drawTimeLabelsOfWatchFace(Canvas canvas, ArrayList<ClockElement> ClockElement) {
for (ClockElement clockElement : ClockElement) {
drawTimeLabel(canvas, clockElement);
}
}
private void drawTimeLabel(Canvas canvas, ClockElement clockElement) {
int amountOfElements = getAmountOfClockElement(clockElement);
float angle = DEGREES_IN_CIRCLE / amountOfElements;
int timeLabelRadius;
setStartingPointOfDrawingRotation(clockElement, radius, startingPointOfDrawingRotation);
canvas.save();
for (int i = 1; i <= amountOfElements; i++) {
canvas.rotate(angle, canvasCenter.getX(), canvasCenter.getY());
timeLabelRadius = radius.getTimeLabelRadius(clockElement, isBigMinuteCircleNeeded(i));
canvas.drawCircle(startingPointOfDrawingRotation.getX(), startingPointOfDrawingRotation.getY(), timeLabelRadius, timeLabelPaint);
}
canvas.restore();
}
private boolean isBigMinuteCircleNeeded(int i) {
return i % EVERY_NTH_BIG_MINUTE == 0;
}
private void setStartingPointOfDrawingRotation(ClockElement clockElement, Radius radius, Point point) {
int x = canvasCenter.getX();
int y = canvasCenter.getY() - radius.getRadiusOfSpinning(clockElement);
point.setXY(x, y);
}
private void drawClock(Canvas canvas, float clockHandRotationAngles[]) {
canvas.drawArc(rect, STARTING_ANGLE_FOR_CIRCLE_DRAWING, DEGREES_IN_CIRCLE, true, watchFacePaint);
for (int i = 0; i < clockHandRotationAngles.length; i++) {
drawClockHand(canvas, clockHandRotationAngles[i], radius.getRadiusOfSpinning(clockElements.get(i)), clockHandsPaintList.get(i));
}
}
private void drawClockHand(Canvas canvas, float clockHandRotationAngle, int clockHandLength, Paint clockHandPaint) {
switch (chosenClockHandDrawType) {
case ARC:
drawArc(canvas, clockHandRotationAngle, clockHandLength, clockHandPaint);
break;
case LINE:
drawLine(canvas, clockHandRotationAngle, clockHandLength, clockHandPaint);
break;
}
}
private void drawArc(Canvas canvas, float angle, int radius, Paint paint) {
setAppropriateRectForInnerCircle(canvasCenter, radius);
canvas.drawArc(innerRect, STARTING_ANGLE_FOR_CIRCLE_DRAWING, angle, true, paint);
}
private void drawLine(Canvas canvas, float angle, int length, Paint paint) {
canvas.drawLine(canvasCenter.getX(), canvasCenter.getY(),
calculateCoordinateOfRotation(Coordinate.X, angle, length),
calculateCoordinateOfRotation(Coordinate.Y, angle, length), paint);
}
private void zoomInOnElement(Canvas canvas) {
canvas.drawCircle(zoomInPoint.getX(), zoomInPoint.getY(), radius.getZoomingRadius(), timeLabelPaint);
int x = zoomInPoint.getX();
int y = zoomInPoint.getY() + (ClockViewPaintFactory.getHeightOfTextPaint(textPaint, zoomInText) / 2);
canvas.drawText(zoomInText, x, y, textPaint);
}
private int getAmountOfClockElement(ClockElement clockElement) {
switch (clockElement) {
case HOUR:
return NUMBER_OF_HOURS;
case MINUTE:
return NUMBER_OF_MINUTES;
default:
return 0;
}
}
private float getAngleAroundClockCenter(int x, int y) {
double lengthAlongX = x - canvasCenter.getX();
double heightAlongY = y - canvasCenter.getY();
double tangent = 0.0;
float angleOffset = 0.0f;
if (lengthAlongX >= 0 && heightAlongY < 0) {
tangent = lengthAlongX / heightAlongY;
} else if (lengthAlongX >= 0 && heightAlongY >= 0) {
tangent = heightAlongY / lengthAlongX;
angleOffset = 90.0f;
} else if (lengthAlongX < 0 && heightAlongY < 0) {
tangent = heightAlongY / lengthAlongX;
angleOffset = 270.0f;
} else if (lengthAlongX < 0 && heightAlongY >= 0) {
tangent = lengthAlongX / heightAlongY;
angleOffset = 180.0f;
}
return Math.abs((float) Math.toDegrees(Math.atan(tangent))) + angleOffset;
}
private float roundAngleToNearestClockElement(float currentAngle, ClockElement clockElement) {
float angleBetweenContiguousElements = DEGREES_IN_CIRCLE / getAmountOfClockElement(clockElement);
float quotient = currentAngle / angleBetweenContiguousElements;
int nearestValue = Math.round(quotient);
return nearestValue * angleBetweenContiguousElements;
}
private double calculateTrigonometricValueOfAngle(Coordinate coordinate, float angle) {
double angleInRads = Math.toRadians(angle);
switch (coordinate) {
case X:
return Math.sin(angleInRads);
case Y:
return Math.cos(angleInRads);
default:
return 0;
}
}
private int getAngleCoordinateSign(Coordinate coordinate) {
switch (coordinate) {
case X:
return 1;
case Y:
return -1;
default:
return 0;
}
}
private int calculateCoordinateOfRotation(Coordinate coordinate, float angle, int radius) {
double trigonometricValueOfAngle = calculateTrigonometricValueOfAngle(coordinate, angle);
int coordinateAroundCenter = (int) (trigonometricValueOfAngle * radius);
return canvasCenter.getCoordinate(coordinate) + (getAngleCoordinateSign(coordinate) * coordinateAroundCenter);
}
private int normalizeCurrentClockElement(ClockElement clockElement, int currentClockElement) {
switch (clockElement) {
case HOUR:
return currentClockElement == NUMBER_OF_HOURS ? 0 : currentClockElement;
case MINUTE:
return currentClockElement == NUMBER_OF_MINUTES ? 0 : currentClockElement;
default:
return 0;
}
}
private int getCurrentClockElementByAngle(float angle, ClockElement clockElement) {
int amountOfElements = getAmountOfClockElement(clockElement);
float angleBetweenTwoContiguousClockElement = DEGREES_IN_CIRCLE / amountOfElements;
int currentClockElement = (int) (angle / angleBetweenTwoContiguousClockElement);
return normalizeCurrentClockElement(clockElement, currentClockElement);
}
private ClockElement clockElementChosen(Point pointOfTouch, Radius radius) {
int normalizedX = pointOfTouch.getX() - canvasCenter.getX();
int normalizedY = pointOfTouch.getY() - canvasCenter.getY();
double length = Math.sqrt(normalizedX * normalizedX + normalizedY * normalizedY);
if (radius.getRadiusOfSpinning(ClockElement.HOUR) >= length) {
return ClockElement.HOUR;
} else {
return ClockElement.MINUTE;
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
pointOfTouch.setXY((int) event.getX(), (int) event.getY());
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
isZoomNeeded = true;
currentlyChosenClockElement = clockElementChosen(pointOfTouch, radius);
case MotionEvent.ACTION_MOVE:
alphas[clockElements.indexOf(currentlyChosenClockElement)] = roundAngleToNearestClockElement(getAngleAroundClockCenter(pointOfTouch.getX(), pointOfTouch.getY()), currentlyChosenClockElement);
zoomInPoint.setXY(
calculateCoordinateOfRotation(Coordinate.X, alphas[clockElements.indexOf(currentlyChosenClockElement)],
radius.getRadiusOfSpinning(currentlyChosenClockElement)),
calculateCoordinateOfRotation(Coordinate.Y, alphas[clockElements.indexOf(currentlyChosenClockElement)],
radius.getRadiusOfSpinning(currentlyChosenClockElement)));
zoomInText = String.valueOf(getCurrentClockElementByAngle(alphas[clockElements.indexOf(currentlyChosenClockElement)], currentlyChosenClockElement));
break;
case MotionEvent.ACTION_UP:
isZoomNeeded = false;
currentlyChosenClockElement = ClockElement.MINUTE;
break;
}
invalidate();
return true;
}
public void setLineOrArc(boolean isLineDrawn) {
if (isLineDrawn) {
chosenClockHandDrawType = ClockHandDrawingType.LINE;
}
invalidate();
}
public void setTime(int currentHour, int currentMinute) {
alphas[clockElements.indexOf(ClockElement.HOUR)] = currentHour * (DEGREES_IN_CIRCLE / getAmountOfClockElement(ClockElement.HOUR));
alphas[clockElements.indexOf(ClockElement.MINUTE)] = currentMinute * (DEGREES_IN_CIRCLE / getAmountOfClockElement(ClockElement.MINUTE));
}
}