-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathDataPicker.java
More file actions
397 lines (345 loc) · 17.2 KB
/
DataPicker.java
File metadata and controls
397 lines (345 loc) · 17.2 KB
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package ru.slybeaver.datapicker;
import android.content.Context;
import android.graphics.*;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import java.util.ArrayList;
/**
* Created by dev on 27.03.2015.
*/
public class DataPicker extends View {
public Context dataPickercontext = null;
private OnChangeValueListener mListener = null;
public int nowTopPosition = 0; //Current scroll position
private int upMaxTopPosition = 0; //Max up scroll position
private int maxTopPosition = 0; //Max scroll position
private int maxValueHeight = 0; //Max of value height
private ArrayList<dpValuesSize> dpvalues = new ArrayList<dpValuesSize>(); //Values
private int canvasW = 0; //Current weight of canvas
private int canvasH = 0; //Current height of canvas
private int selectedvalueId = 0; //Selected value id
private boolean needAnimation = false; //animation
private int needPosition = 0; //need to scroll position
public int valpadding = 30; //vertical values padding
private int scrollspeed = 0; //Impulse speed of scroll
private boolean scrolltoup = false; //The direction of movement
private float dpDownY = 0; //Touch down coordinates
private float canvasDownY = 0; //canvas touch down coordinates
private long actdownTime = 0; //Touch time
public interface OnChangeValueListener {
public void onEvent(int valueId);
}
public void setOnChangeValueListener(OnChangeValueListener eventListener) {
mListener = eventListener;
}
public DataPicker(Context context) {
super(context);
dataPickercontext = context;
init();
}
public DataPicker(Context context, AttributeSet attrs) {
super(context, attrs);
dataPickercontext = context;
init();
}
public DataPicker(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
dataPickercontext = context;
init();
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
canvasW = w;
canvasH = h;
maxValueHeight = (canvasH - (valpadding * 2)) / 2;
nowTopPosition = 0;
super.onSizeChanged(w, h, oldw, oldh);
}
@Override
public boolean onTouchEvent(MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
canvasDownY = motionEvent.getY();
dpDownY = motionEvent.getY() - nowTopPosition;
needAnimation = false;
actdownTime = motionEvent.getEventTime();
}
if (motionEvent.getAction() == MotionEvent.ACTION_MOVE) {
if ((int) (motionEvent.getY() - dpDownY) > maxTopPosition) {
nowTopPosition = maxTopPosition;
return true;
}
if ((int) (motionEvent.getY() - dpDownY) < upMaxTopPosition) {
nowTopPosition = upMaxTopPosition;
return true;
}
nowTopPosition = (int) (motionEvent.getY() - dpDownY);
}
if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
if (canvasDownY > motionEvent.getY()) {
scrolltoup = false;
} else {
scrolltoup = true;
}
if ((motionEvent.getEventTime() - actdownTime < 200) && (Math.abs(dpDownY - motionEvent.getY()) > 100)) {
scrollspeed = (int) (1000 - (motionEvent.getEventTime() - actdownTime));
} else {
scrollspeed = 0;
roundingValue();
}
needAnimation = true;
}
return true;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
try {
if (dpvalues.size() == 0) {
return;
}
upMaxTopPosition = -(((dpvalues.size() - 1) * (maxValueHeight + valpadding)));
canvas.drawColor(Color.argb(0, 255, 255, 255));
if (needAnimation) {
if (scrollspeed > 0) {
scrollspeed -= 30;
if (scrolltoup) {
int currentPos = nowTopPosition + 30;
if ((currentPos) > maxTopPosition) {
nowTopPosition = maxTopPosition;
scrollspeed = 0;
roundingValue();
} else {
nowTopPosition = currentPos;
}
}
if (!scrolltoup) {
int currentPos = nowTopPosition - 30;
if ((currentPos) < upMaxTopPosition) {
nowTopPosition = upMaxTopPosition;
scrollspeed = 0;
roundingValue();
} else {
nowTopPosition = currentPos;
}
}
if (scrollspeed <= 0) {
roundingValue();
}
} else {
if (nowTopPosition > needPosition) {
nowTopPosition -= 20;
if (nowTopPosition < needPosition) {
nowTopPosition = needPosition;
}
}
if (nowTopPosition < needPosition) {
nowTopPosition += 20;
if (nowTopPosition > needPosition) {
nowTopPosition = needPosition;
}
}
if (nowTopPosition == needPosition) {
needAnimation = false;
}
}
}
//Paste values to canvas
for (int i = 0; i < dpvalues.size(); i++) {
try {
Paint paint = new Paint();
paint.setColor(dataPickercontext.getResources().getColor(R.color.datepickerText));
if (selectedvalueId == i) {
paint.setColor(dataPickercontext.getResources().getColor(R.color.datepickerSelectedValue));
Paint shadowText = new Paint();
shadowText.setColor(dataPickercontext.getResources().getColor(R.color.datepickerSelectedValueShadow));
shadowText.setTextSize(dpvalues.get(i).dpTextSize);
shadowText.setAntiAlias(true);
canvas.drawText(dpvalues.get(i).dpValue, (canvasW / 2) - (dpvalues.get(i).dpWidth / 2), ((maxValueHeight + valpadding) * i) + (valpadding + maxValueHeight) + (dpvalues.get(i).dpHeight / 2) + nowTopPosition + 2, shadowText);
}
paint.setTextSize(dpvalues.get(i).dpTextSize);
paint.setAntiAlias(true);
canvas.drawText(dpvalues.get(i).dpValue, (canvasW / 2) - (dpvalues.get(i).dpWidth / 2), ((maxValueHeight + valpadding) * i) + (valpadding + maxValueHeight) + (dpvalues.get(i).dpHeight / 2) + nowTopPosition, paint);
} catch (Exception e) {
e.printStackTrace();
}
}
//Draw borders
Paint lPBorders = new Paint();
lPBorders.setColor(dataPickercontext.getResources().getColor(R.color.datapickerBlackLines));
canvas.drawLine(0, 0, 0, canvasH, lPBorders);
canvas.drawLine(1, 0, 1, canvasH, lPBorders);
canvas.drawLine(canvasW - 1, 0, canvasW - 1, canvasH, lPBorders);
canvas.drawLine(canvasW - 2, 0, canvasW - 2, canvasH, lPBorders);
canvas.drawLine(canvasW, 0, canvasW, canvasH, lPBorders);
lPBorders = new Paint();
lPBorders.setColor(dataPickercontext.getResources().getColor(R.color.datapickerGrayLines));
canvas.drawRect(2, 0, 7, canvasH, lPBorders);
canvas.drawRect(canvasW - 7, 0, canvasW - 2, canvasH, lPBorders);
//Draw shadows
Paint framePaint = new Paint();
framePaint.setShader(new LinearGradient(0, 0, 0, getHeight() / 5, dataPickercontext.getResources().getColor(R.color.datapickerGradientStart), Color.TRANSPARENT, Shader.TileMode.CLAMP));
canvas.drawPaint(framePaint);
framePaint.setShader(new LinearGradient(0, getHeight(), 0, getHeight() - getHeight() / 5, dataPickercontext.getResources().getColor(R.color.datapickerGradientStart), Color.TRANSPARENT, Shader.TileMode.CLAMP));
canvas.drawPaint(framePaint);
//Draw glass
Path pathSelect = new Path();
pathSelect.moveTo(0, canvasH / 2 - maxValueHeight / 2 - valpadding / 2);
pathSelect.lineTo(canvasW, canvasH / 2 - maxValueHeight / 2 - valpadding / 2);
pathSelect.lineTo(canvasW, canvasH / 2);
pathSelect.lineTo(0, canvasH / 2);
pathSelect.lineTo(0, canvasH / 2 - maxValueHeight / 2);
Paint pathSelectPaint = new Paint();
pathSelectPaint.setShader(new LinearGradient(0, 0, 0, maxValueHeight / 2, dataPickercontext.getResources().getColor(R.color.datapickerSelectedValueeLineG1), dataPickercontext.getResources().getColor(R.color.datapickerSelectedValueeLineG2), Shader.TileMode.CLAMP));
canvas.drawPath(pathSelect, pathSelectPaint);
pathSelect = new Path();
pathSelect.moveTo(0, canvasH / 2);
pathSelect.lineTo(canvasW, canvasH / 2);
pathSelect.lineTo(canvasW, canvasH / 2 + maxValueHeight / 2 + valpadding / 2);
pathSelect.lineTo(0, canvasH / 2 + maxValueHeight / 2 + valpadding / 2);
pathSelect.lineTo(0, canvasH / 2);
pathSelectPaint = new Paint();
pathSelectPaint.setShader(new LinearGradient(0, 0, 0, maxValueHeight / 2, dataPickercontext.getResources().getColor(R.color.datapickerSelectedValueeLineG3), dataPickercontext.getResources().getColor(R.color.datapickerSelectedValueeLineG4), Shader.TileMode.CLAMP));
canvas.drawPath(pathSelect, pathSelectPaint);
//Draw glass shadow
Paint selValLightBorder = new Paint();
Paint selValTopBorder = new Paint();
Paint selValBottomBorder = new Paint();
selValLightBorder.setColor(dataPickercontext.getResources().getColor(R.color.datapicketSelectedValueBorder));
selValTopBorder.setColor(dataPickercontext.getResources().getColor(R.color.datapicketSelectedBorderTop));
selValBottomBorder.setColor(dataPickercontext.getResources().getColor(R.color.datapicketSelectedBorderBttom));
canvas.drawLine(0, canvasH / 2 - maxValueHeight / 2 - valpadding / 2, canvasW, canvasH / 2 - maxValueHeight / 2 - valpadding / 2, selValLightBorder);
canvas.drawLine(0, canvasH / 2 - maxValueHeight / 2 - valpadding / 2 + 1, canvasW, canvasH / 2 - maxValueHeight / 2 - valpadding / 2 + 1, selValTopBorder);
canvas.drawLine(0, canvasH / 2 + maxValueHeight / 2 + valpadding / 2, canvasW, canvasH / 2 + maxValueHeight / 2 + valpadding / 2, selValLightBorder);
canvas.drawLine(0, canvasH / 2 + maxValueHeight / 2 + valpadding / 2 - 1, canvasW, canvasH / 2 + maxValueHeight / 2 + valpadding / 2 - 1, selValBottomBorder);
canvas.drawLine(0, canvasH / 2 - maxValueHeight / 2 - valpadding / 2, 0, canvasH / 2 + maxValueHeight / 2 + valpadding / 2, selValLightBorder);
canvas.drawLine(1, canvasH / 2 - maxValueHeight / 2 - valpadding / 2, 1, canvasH / 2 + maxValueHeight / 2 + valpadding / 2, selValLightBorder);
canvas.drawLine(canvasW - 1, canvasH / 2 - maxValueHeight / 2 - valpadding / 2, canvasW - 1, canvasH / 2 + maxValueHeight / 2 + valpadding / 2, selValLightBorder);
canvas.drawLine(canvasW - 2, canvasH / 2 - maxValueHeight / 2 - valpadding / 2, canvasW - 2, canvasH / 2 + maxValueHeight / 2 + valpadding / 2, selValLightBorder);
canvas.drawLine(canvasW, canvasH / 2 - maxValueHeight / 2 - valpadding / 2, canvasW, canvasH / 2 + maxValueHeight / 2 + valpadding / 2, selValLightBorder);
//Draw selected value
Paint selectedTextPaint = new Paint();
selectedTextPaint.setColor(dataPickercontext.getResources().getColor(R.color.datepickerSelectedValue));
Paint shadowText = new Paint();
shadowText.setColor(dataPickercontext.getResources().getColor(R.color.datepickerSelectedValueShadow));
shadowText.setTextSize(dpvalues.get(selectedvalueId).dpTextSize);
shadowText.setAntiAlias(true);
canvas.drawText(dpvalues.get(selectedvalueId).dpValue, (canvasW / 2) - (dpvalues.get(selectedvalueId).dpWidth / 2), ((maxValueHeight + valpadding) * selectedvalueId) + (valpadding + maxValueHeight) + (dpvalues.get(selectedvalueId).dpHeight / 2) + nowTopPosition + 2, shadowText);
selectedTextPaint.setTextSize(dpvalues.get(selectedvalueId).dpTextSize);
selectedTextPaint.setAntiAlias(true);
canvas.drawText(dpvalues.get(selectedvalueId).dpValue, (canvasW / 2) - (dpvalues.get(selectedvalueId).dpWidth / 2), ((maxValueHeight + valpadding) * selectedvalueId) + (valpadding + maxValueHeight) + (dpvalues.get(selectedvalueId).dpHeight / 2) + nowTopPosition, selectedTextPaint);
} catch (Exception e) {
e.printStackTrace();
}
//FPS to canvas
this.postInvalidateDelayed(1000 / 50);
}
//Rounding values
private void roundingValue() {
Log.d("Rounding", "ROUNDING VALUE!");
needPosition = (((nowTopPosition - maxTopPosition - (maxValueHeight / 2)) / (maxValueHeight + valpadding))) * (maxValueHeight + valpadding) + maxTopPosition;
selectedvalueId = Math.abs(((needPosition - valpadding - (maxValueHeight / 2)) / (maxValueHeight + valpadding)));
try {
Log.e("SELECTED VALUE", dpvalues.get(selectedvalueId).dpValue);
} catch (Exception e) {
e.printStackTrace();
}
onSelected(selectedvalueId);
}
//Return value
public String getValue() {
try {
return dpvalues.get(selectedvalueId).dpValue;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
//Set values
private Handler dpChangeHandler = new Handler();
public void changetToValue(final int valueId) {
selectedvalueId = valueId;
dpChangeHandler.postDelayed(new Runnable() {
@Override
public void run() {
if (canvasW == 0 || canvasH == 0) {
dpChangeHandler.postDelayed(this, 100);
} else {
maxValueHeight = (canvasH - (valpadding * 2)) / 2;
needPosition = -((maxValueHeight + valpadding) * (valueId));
needAnimation = true;
}
}
}, 100);
onSelected(selectedvalueId);
}
//On value selected Event
protected void onSelected(int selectedId) {
if (mListener != null) {
mListener.onEvent(selectedId);
}
}
//Return the value id
public int getValueid() {
try {
return selectedvalueId;
} catch (Exception e) {
}
return -1;
}
private Handler dpHandler = new Handler();
//Set values
public void setValues(final String[] newvalues) {
if (canvasW == 0 || canvasH == 0) {
dpHandler.postDelayed(new Runnable() {
@Override
public void run() {
if (canvasW == 0 || canvasH == 0) {
dpHandler.postDelayed(this, 100);
} else {
dpvalues.clear();
for (int i = 0; i < newvalues.length; i++) {
dpvalues.add(new dpValuesSize(newvalues[i], canvasW, canvasH));
}
}
}
}, 100);
}
dpvalues.clear();
for (int i = 0; i < newvalues.length; i++) {
dpvalues.add(new dpValuesSize(newvalues[i], canvasW, canvasH));
}
}
}
class dpValuesSize {
public int dpWidth = 0;
public int dpHeight = 0;
public String dpValue = "";
public int dpTextSize = 0;
public int valpadding = 30;
public int valinnerLeftpadding = 20;
public dpValuesSize(String val, int canvasW, int canvasH) {
try {
int maxTextHeight = (canvasH - (valpadding * 2)) / 2;
boolean sizeOK = false;
dpValue = val;
while (!sizeOK) {
Rect textBounds = new Rect();
Paint textPaint = new Paint();
dpTextSize++;
textPaint.setTextSize(dpTextSize);
textPaint.getTextBounds(val, 0, val.length(), textBounds);
if (textBounds.width() <= canvasW - (valinnerLeftpadding * 2) && textBounds.height() <= maxTextHeight) {
dpWidth = textBounds.width();
dpHeight = textBounds.height();
} else {
sizeOK = true;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}