-
-
Notifications
You must be signed in to change notification settings - Fork 241
Expand file tree
/
Copy pathNumberPicker.java
More file actions
3041 lines (2668 loc) · 103 KB
/
Copy pathNumberPicker.java
File metadata and controls
3041 lines (2668 loc) · 103 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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package com.shawnlin.numberpicker;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.text.InputType;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.method.NumberKeyListener;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.SparseArray;
import android.util.TypedValue;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.LayoutInflater.Filter;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.accessibility.AccessibilityEvent;
import android.view.animation.DecelerateInterpolator;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.LinearLayout;
import androidx.annotation.CallSuper;
import androidx.annotation.ColorInt;
import androidx.annotation.ColorRes;
import androidx.annotation.DimenRes;
import androidx.annotation.IntDef;
import androidx.annotation.StringRes;
import androidx.core.content.ContextCompat;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.util.Locale;
import static java.lang.annotation.RetentionPolicy.SOURCE;
/**
* A widget that enables the user to select a number from a predefined range.
*/
public class NumberPicker extends LinearLayout {
@Retention(SOURCE)
@IntDef({VERTICAL, HORIZONTAL})
public @interface Orientation {
}
public static final int VERTICAL = LinearLayout.VERTICAL;
public static final int HORIZONTAL = LinearLayout.HORIZONTAL;
@Retention(SOURCE)
@IntDef({ASCENDING, DESCENDING})
public @interface Order {
}
public static final int ASCENDING = 0;
public static final int DESCENDING = 1;
@Retention(SOURCE)
@IntDef({LEFT, CENTER, RIGHT})
public @interface Align {
}
public static final int RIGHT = 0;
public static final int CENTER = 1;
public static final int LEFT = 2;
@Retention(SOURCE)
@IntDef({SIDE_LINES, UNDERLINE, DRAWABLE_AS_DIVIDER})
public @interface DividerType {
}
public static final int SIDE_LINES = 0;
public static final int UNDERLINE = 1;
public static final int DRAWABLE_AS_DIVIDER = 2;
/**
* The default drawable resource ID for drawable divider.
*/
public static final int DEFAULT_DRAWABLE_DIVIDER_RES = 0;
/**
* The default update interval during long press.
*/
private static final long DEFAULT_LONG_PRESS_UPDATE_INTERVAL = 300;
/**
* The default coefficient to adjust (divide) the max fling velocity.
*/
private static final int DEFAULT_MAX_FLING_VELOCITY_COEFFICIENT = 8;
/**
* The the duration for adjusting the selector wheel.
*/
private static final int SELECTOR_ADJUSTMENT_DURATION_MILLIS = 800;
/**
* The duration of scrolling while snapping to a given position.
*/
private static final int SNAP_SCROLL_DURATION = 300;
/**
* The default strength of fading edge while drawing the selector.
*/
private static final float DEFAULT_FADING_EDGE_STRENGTH = 0.9f;
/**
* The default unscaled height of the divider.
*/
private static final int UNSCALED_DEFAULT_DIVIDER_THICKNESS = 2;
/**
* The default unscaled distance between the dividers.
*/
private static final int UNSCALED_DEFAULT_DIVIDER_DISTANCE = 48;
/**
* Constant for unspecified size.
*/
private static final int SIZE_UNSPECIFIED = -1;
/**
* The default color of divider.
*/
private static final int DEFAULT_DIVIDER_COLOR = 0xFF000000;
/**
* The default alpha value for the drawable divider.
*/
private static final int DEFAULT_DIVIDER_DRAWABLE_ALPHA_VALUE = 100;
/**
* The default max value of this widget.
*/
private static final int DEFAULT_MAX_VALUE = 100;
/**
* The default min value of this widget.
*/
private static final int DEFAULT_MIN_VALUE = 1;
/**
* The default wheel item count of this widget.
*/
private static final int DEFAULT_WHEEL_ITEM_COUNT = 3;
/**
* The default max height of this widget.
*/
private static final int DEFAULT_MAX_HEIGHT = 180;
/**
* The default min width of this widget.
*/
private static final int DEFAULT_MIN_WIDTH = 64;
/**
* The default align of text.
*/
private static final int DEFAULT_TEXT_ALIGN = CENTER;
/**
* The default color of text.
*/
private static final int DEFAULT_TEXT_COLOR = 0xFF000000;
/**
* The default size of text.
*/
private static final float DEFAULT_TEXT_SIZE = 25f;
/**
* The default line spacing multiplier of text.
*/
private static final float DEFAULT_LINE_SPACING_MULTIPLIER = 1f;
/**
* Use a custom NumberPicker formatting callback to use two-digit minutes
* strings like "01". Keeping a static formatter etc. is the most efficient
* way to do this; it avoids creating temporary objects on every call to
* format().
*/
private static class TwoDigitFormatter implements Formatter {
final StringBuilder mBuilder = new StringBuilder();
char mZeroDigit;
java.util.Formatter mFmt;
final Object[] mArgs = new Object[1];
TwoDigitFormatter() {
final Locale locale = Locale.getDefault();
init(locale);
}
private void init(Locale locale) {
mFmt = createFormatter(locale);
mZeroDigit = getZeroDigit(locale);
}
public String format(int value) {
final Locale currentLocale = Locale.getDefault();
if (mZeroDigit != getZeroDigit(currentLocale)) {
init(currentLocale);
}
mArgs[0] = value;
mBuilder.delete(0, mBuilder.length());
mFmt.format("%02d", mArgs);
return mFmt.toString();
}
private static char getZeroDigit(Locale locale) {
// return LocaleData.get(locale).zeroDigit;
return new DecimalFormatSymbols(locale).getZeroDigit();
}
private java.util.Formatter createFormatter(Locale locale) {
return new java.util.Formatter(mBuilder, locale);
}
}
private static final TwoDigitFormatter sTwoDigitFormatter = new TwoDigitFormatter();
public static Formatter getTwoDigitFormatter() {
return sTwoDigitFormatter;
}
/**
* The text for showing the current value.
*/
private final EditText mSelectedText;
/**
* The center X position of the selected text.
*/
private float mSelectedTextCenterX;
/**
* The center Y position of the selected text.
*/
private float mSelectedTextCenterY;
/**
* The min height of this widget.
*/
private int mMinHeight;
/**
* The max height of this widget.
*/
private int mMaxHeight;
/**
* The max width of this widget.
*/
private int mMinWidth;
/**
* The max width of this widget.
*/
private int mMaxWidth;
/**
* Flag whether to compute the max width.
*/
private final boolean mComputeMaxWidth;
/**
* The align of the selected text.
*/
private int mSelectedTextAlign = DEFAULT_TEXT_ALIGN;
/**
* The color of the selected text.
*/
private int mSelectedTextColor = DEFAULT_TEXT_COLOR;
/**
* The size of the selected text.
*/
private float mSelectedTextSize = DEFAULT_TEXT_SIZE;
/**
* Flag whether the selected text should strikethroughed.
*/
private boolean mSelectedTextStrikeThru;
/**
* Flag whether the selected text should underlined.
*/
private boolean mSelectedTextUnderline;
/**
* The typeface of the selected text.
*/
private Typeface mSelectedTypeface;
/**
* The align of the text.
*/
private int mTextAlign = DEFAULT_TEXT_ALIGN;
/**
* The color of the text.
*/
private int mTextColor = DEFAULT_TEXT_COLOR;
/**
* The drawable divider resource.
*/
private int mDividerResource = DEFAULT_DRAWABLE_DIVIDER_RES;
/**
* The alpha value for the drawable divider
*/
private int mDividerDrawableAlphaValue = DEFAULT_DIVIDER_DRAWABLE_ALPHA_VALUE;
/**
* The size of the text.
*/
private float mTextSize = DEFAULT_TEXT_SIZE;
/**
* Flag whether the text should strikethroughed.
*/
private boolean mTextStrikeThru;
/**
* Flag whether the text should underlined.
*/
private boolean mTextUnderline;
/**
* The typeface of the text.
*/
private Typeface mTypeface;
/**
* The width of the gap between text elements if the selector wheel.
*/
private int mSelectorTextGapWidth;
/**
* The height of the gap between text elements if the selector wheel.
*/
private int mSelectorTextGapHeight;
/**
* The values to be displayed instead the indices.
*/
private String[] mDisplayedValues;
/**
* Lower value of the range of numbers allowed for the NumberPicker
*/
private int mMinValue = DEFAULT_MIN_VALUE;
/**
* Upper value of the range of numbers allowed for the NumberPicker
*/
private int mMaxValue = DEFAULT_MAX_VALUE;
/**
* Current value of this NumberPicker
*/
private int mValue;
/**
* Listener to be notified upon current value click.
*/
private OnClickListener mOnClickListener;
/**
* Listener to be notified upon current value change.
*/
private OnValueChangeListener mOnValueChangeListener;
/**
* Listener to be notified upon scroll state change.
*/
private OnScrollListener mOnScrollListener;
/**
* Formatter for for displaying the current value.
*/
private Formatter mFormatter;
/**
* The speed for updating the value form long press.
*/
private long mLongPressUpdateInterval = DEFAULT_LONG_PRESS_UPDATE_INTERVAL;
/**
* Cache for the string representation of selector indices.
*/
private final SparseArray<String> mSelectorIndexToStringCache = new SparseArray<>();
/**
* The number of items show in the selector wheel.
*/
private int mWheelItemCount = DEFAULT_WHEEL_ITEM_COUNT;
/**
* The real number of items show in the selector wheel.
*/
private int mRealWheelItemCount = DEFAULT_WHEEL_ITEM_COUNT;
/**
* The index of the middle selector item.
*/
private int mWheelMiddleItemIndex = mWheelItemCount / 2;
/**
* The selector indices whose value are show by the selector.
*/
private int[] mSelectorIndices = new int[mWheelItemCount];
/**
* The {@link Paint} for drawing the selector.
*/
private final Paint mSelectorWheelPaint;
/**
* The size of a selector element (text + gap).
*/
private int mSelectorElementSize;
/**
* The initial offset of the scroll selector.
*/
private int mInitialScrollOffset = Integer.MIN_VALUE;
/**
* The current offset of the scroll selector.
*/
private int mCurrentScrollOffset;
/**
* The {@link Scroller} responsible for flinging the selector.
*/
private final Scroller mFlingScroller;
/**
* The {@link Scroller} responsible for adjusting the selector.
*/
private final Scroller mAdjustScroller;
/**
* The previous X coordinate while scrolling the selector.
*/
private int mPreviousScrollerX;
/**
* The previous Y coordinate while scrolling the selector.
*/
private int mPreviousScrollerY;
/**
* Handle to the reusable command for setting the input text selection.
*/
private SetSelectionCommand mSetSelectionCommand;
/**
* Handle to the reusable command for changing the current value from long press by one.
*/
private ChangeCurrentByOneFromLongPressCommand mChangeCurrentByOneFromLongPressCommand;
/**
* The X position of the last down event.
*/
private float mLastDownEventX;
/**
* The Y position of the last down event.
*/
private float mLastDownEventY;
/**
* The X position of the last down or move event.
*/
private float mLastDownOrMoveEventX;
/**
* The Y position of the last down or move event.
*/
private float mLastDownOrMoveEventY;
/**
* Determines speed during touch scrolling.
*/
private VelocityTracker mVelocityTracker;
/**
* @see ViewConfiguration#getScaledTouchSlop()
*/
private int mTouchSlop;
/**
* @see ViewConfiguration#getScaledMinimumFlingVelocity()
*/
private int mMinimumFlingVelocity;
/**
* @see ViewConfiguration#getScaledMaximumFlingVelocity()
*/
private int mMaximumFlingVelocity;
/**
* Flag whether the selector should wrap around.
*/
private boolean mWrapSelectorWheel;
/**
* User choice on whether the selector wheel should be wrapped.
*/
private boolean mWrapSelectorWheelPreferred = true;
/**
* Divider for showing item to be selected while scrolling
*/
private Drawable mDividerDrawable;
/**
* The color of the divider.
*/
private int mDividerColor = DEFAULT_DIVIDER_COLOR;
/**
* The distance between the two dividers.
*/
private int mDividerDistance;
/**
* The thickness of the divider.
*/
private int mDividerLength;
/**
* The thickness of the divider.
*/
private int mDividerThickness;
/**
* The top of the top divider.
*/
private int mTopDividerTop;
/**
* The bottom of the bottom divider.
*/
private int mBottomDividerBottom;
/**
* The left of the top divider.
*/
private int mLeftDividerLeft;
/**
* The right of the right divider.
*/
private int mRightDividerRight;
/**
* The type of the divider.
*/
private int mDividerType;
/**
* The current scroll state of the number picker.
*/
private int mScrollState = OnScrollListener.SCROLL_STATE_IDLE;
/**
* The keycode of the last handled DPAD down event.
*/
private int mLastHandledDownDpadKeyCode = -1;
/**
* Flag whether the selector wheel should hidden until the picker has focus.
*/
private boolean mHideWheelUntilFocused;
/**
* The orientation of this widget.
*/
private int mOrientation;
/**
* The order of this widget.
*/
private int mOrder;
/**
* Flag whether the fading edge should enabled.
*/
private boolean mFadingEdgeEnabled = true;
/**
* The strength of fading edge while drawing the selector.
*/
private float mFadingEdgeStrength = DEFAULT_FADING_EDGE_STRENGTH;
/**
* Flag whether the scroller should enabled.
*/
private boolean mScrollerEnabled = true;
/**
* The line spacing multiplier of the text.
*/
private float mLineSpacingMultiplier = DEFAULT_LINE_SPACING_MULTIPLIER;
/**
* The coefficient to adjust (divide) the max fling velocity.
*/
private int mMaxFlingVelocityCoefficient = DEFAULT_MAX_FLING_VELOCITY_COEFFICIENT;
/**
* Flag whether the accessibility description enabled.
*/
private boolean mAccessibilityDescriptionEnabled = true;
/**
* The context of this widget.
*/
private Context mContext;
/**
* The number formatter for current locale.
*/
private NumberFormat mNumberFormatter;
/**
* The view configuration of this widget.
*/
private ViewConfiguration mViewConfiguration;
/**
* Interface to listen for changes of the current value.
*/
public interface OnValueChangeListener {
/**
* Called upon a change of the current value.
*
* @param picker The NumberPicker associated with this listener.
* @param oldVal The previous value.
* @param newVal The new value.
*/
void onValueChange(NumberPicker picker, int oldVal, int newVal);
}
/**
* The amount of space between items.
*/
private int mItemSpacing = 0;
/**
* Interface to listen for the picker scroll state.
*/
public interface OnScrollListener {
@IntDef({SCROLL_STATE_IDLE, SCROLL_STATE_TOUCH_SCROLL, SCROLL_STATE_FLING})
@Retention(RetentionPolicy.SOURCE)
public @interface ScrollState {
}
/**
* The view is not scrolling.
*/
public static int SCROLL_STATE_IDLE = 0;
/**
* The user is scrolling using touch, and his finger is still on the screen.
*/
public static int SCROLL_STATE_TOUCH_SCROLL = 1;
/**
* The user had previously been scrolling using touch and performed a fling.
*/
public static int SCROLL_STATE_FLING = 2;
/**
* Callback invoked while the number picker scroll state has changed.
*
* @param view The view whose scroll state is being reported.
* @param scrollState The current scroll state. One of
* {@link #SCROLL_STATE_IDLE},
* {@link #SCROLL_STATE_TOUCH_SCROLL} or
* {@link #SCROLL_STATE_IDLE}.
*/
public void onScrollStateChange(NumberPicker view, @ScrollState int scrollState);
}
/**
* Interface used to format current value into a string for presentation.
*/
public interface Formatter {
/**
* Formats a string representation of the current value.
*
* @param value The currently selected value.
* @return A formatted string representation.
*/
public String format(int value);
}
/**
* Create a new number picker.
*
* @param context The application environment.
*/
public NumberPicker(Context context) {
this(context, null);
}
/**
* Create a new number picker.
*
* @param context The application environment.
* @param attrs A collection of attributes.
*/
public NumberPicker(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
/**
* Create a new number picker
*
* @param context the application environment.
* @param attrs a collection of attributes.
* @param defStyle The default style to apply to this view.
*/
public NumberPicker(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs);
mContext = context;
mNumberFormatter = NumberFormat.getInstance();
final TypedArray attributes = context.obtainStyledAttributes(attrs,
R.styleable.NumberPicker, defStyle, 0);
final Drawable selectionDivider = attributes.getDrawable(
R.styleable.NumberPicker_np_divider);
if (selectionDivider != null) {
selectionDivider.setCallback(this);
if (selectionDivider.isStateful()) {
selectionDivider.setState(getDrawableState());
}
mDividerDrawable = selectionDivider;
} else {
mDividerColor = attributes.getColor(R.styleable.NumberPicker_np_dividerColor,
mDividerColor);
setDividerColor(mDividerColor);
}
final DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
final int defDividerDistance = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
UNSCALED_DEFAULT_DIVIDER_DISTANCE, displayMetrics);
final int defDividerThickness = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
UNSCALED_DEFAULT_DIVIDER_THICKNESS, displayMetrics);
mDividerDistance = attributes.getDimensionPixelSize(
R.styleable.NumberPicker_np_dividerDistance, defDividerDistance);
mDividerLength = attributes.getDimensionPixelSize(
R.styleable.NumberPicker_np_dividerLength, 0);
mDividerThickness = attributes.getDimensionPixelSize(
R.styleable.NumberPicker_np_dividerThickness, defDividerThickness);
mDividerType = attributes.getInt(R.styleable.NumberPicker_np_dividerType, SIDE_LINES);
mOrder = attributes.getInt(R.styleable.NumberPicker_np_order, ASCENDING);
mOrientation = attributes.getInt(R.styleable.NumberPicker_np_orientation, VERTICAL);
final float width = attributes.getDimensionPixelSize(R.styleable.NumberPicker_np_width,
SIZE_UNSPECIFIED);
final float height = attributes.getDimensionPixelSize(R.styleable.NumberPicker_np_height,
SIZE_UNSPECIFIED);
setWidthAndHeight();
mComputeMaxWidth = true;
mValue = attributes.getInt(R.styleable.NumberPicker_np_value, mValue);
mMaxValue = attributes.getInt(R.styleable.NumberPicker_np_max, mMaxValue);
mMinValue = attributes.getInt(R.styleable.NumberPicker_np_min, mMinValue);
mSelectedTextAlign = attributes.getInt(R.styleable.NumberPicker_np_selectedTextAlign,
mSelectedTextAlign);
mSelectedTextColor = attributes.getColor(R.styleable.NumberPicker_np_selectedTextColor,
mSelectedTextColor);
mSelectedTextSize = attributes.getDimension(R.styleable.NumberPicker_np_selectedTextSize,
spToPx(mSelectedTextSize));
mSelectedTextStrikeThru = attributes.getBoolean(
R.styleable.NumberPicker_np_selectedTextStrikeThru, mSelectedTextStrikeThru);
mSelectedTextUnderline = attributes.getBoolean(
R.styleable.NumberPicker_np_selectedTextUnderline, mSelectedTextUnderline);
mSelectedTypeface = Typeface.create(attributes.getString(
R.styleable.NumberPicker_np_selectedTypeface), Typeface.NORMAL);
mTextAlign = attributes.getInt(R.styleable.NumberPicker_np_textAlign, mTextAlign);
mTextColor = attributes.getColor(R.styleable.NumberPicker_np_textColor, mTextColor);
mTextSize = attributes.getDimension(R.styleable.NumberPicker_np_textSize,
spToPx(mTextSize));
mTextStrikeThru = attributes.getBoolean(
R.styleable.NumberPicker_np_textStrikeThru, mTextStrikeThru);
mTextUnderline = attributes.getBoolean(
R.styleable.NumberPicker_np_textUnderline, mTextUnderline);
mTypeface = Typeface.create(attributes.getString(R.styleable.NumberPicker_np_typeface),
Typeface.NORMAL);
mFormatter = stringToFormatter(attributes.getString(R.styleable.NumberPicker_np_formatter));
mFadingEdgeEnabled = attributes.getBoolean(R.styleable.NumberPicker_np_fadingEdgeEnabled,
mFadingEdgeEnabled);
mFadingEdgeStrength = attributes.getFloat(R.styleable.NumberPicker_np_fadingEdgeStrength,
mFadingEdgeStrength);
mScrollerEnabled = attributes.getBoolean(R.styleable.NumberPicker_np_scrollerEnabled,
mScrollerEnabled);
mWheelItemCount = attributes.getInt(R.styleable.NumberPicker_np_wheelItemCount,
mWheelItemCount);
mLineSpacingMultiplier = attributes.getFloat(
R.styleable.NumberPicker_np_lineSpacingMultiplier, mLineSpacingMultiplier);
mMaxFlingVelocityCoefficient = attributes.getInt(
R.styleable.NumberPicker_np_maxFlingVelocityCoefficient,
mMaxFlingVelocityCoefficient);
mHideWheelUntilFocused = attributes.getBoolean(
R.styleable.NumberPicker_np_hideWheelUntilFocused, false);
mAccessibilityDescriptionEnabled = attributes.getBoolean(
R.styleable.NumberPicker_np_accessibilityDescriptionEnabled, true);
mItemSpacing = attributes.getDimensionPixelSize(
R.styleable.NumberPicker_np_itemSpacing, 0);
// By default LinearLayout that we extend is not drawn. This is
// its draw() method is not called but dispatchDraw() is called
// directly (see ViewGroup.drawChild()). However, this class uses
// the fading edge effect implemented by View and we need our
// draw() method to be called. Therefore, we declare we will draw.
setWillNotDraw(false);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.number_picker_material, this, true);
// input text
mSelectedText = findViewById(R.id.np__numberpicker_input);
mSelectedText.setEnabled(false);
mSelectedText.setFocusable(false);
mSelectedText.setImeOptions(EditorInfo.IME_ACTION_NONE);
// create the selector wheel paint
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setTextAlign(Paint.Align.CENTER);
mSelectorWheelPaint = paint;
setSelectedTextColor(mSelectedTextColor);
setTextColor(mTextColor);
setTextSize(mTextSize);
setSelectedTextSize(mSelectedTextSize);
setTypeface(mTypeface);
setSelectedTypeface(mSelectedTypeface);
setFormatter(mFormatter);
updateInputTextView();
setValue(mValue);
setMaxValue(mMaxValue);
setMinValue(mMinValue);
setWheelItemCount(mWheelItemCount);
mWrapSelectorWheel = attributes.getBoolean(R.styleable.NumberPicker_np_wrapSelectorWheel,
mWrapSelectorWheel);
setWrapSelectorWheel(mWrapSelectorWheel);
if (width != SIZE_UNSPECIFIED && height != SIZE_UNSPECIFIED) {
setScaleX(width / mMinWidth);
setScaleY(height / mMaxHeight);
} else if (width != SIZE_UNSPECIFIED) {
final float scale = width / mMinWidth;
setScaleX(scale);
setScaleY(scale);
} else if (height != SIZE_UNSPECIFIED) {
final float scale = height / mMaxHeight;
setScaleX(scale);
setScaleY(scale);
}
// initialize constants
mViewConfiguration = ViewConfiguration.get(context);
mTouchSlop = mViewConfiguration.getScaledTouchSlop();
mMinimumFlingVelocity = mViewConfiguration.getScaledMinimumFlingVelocity();
mMaximumFlingVelocity = mViewConfiguration.getScaledMaximumFlingVelocity()
/ mMaxFlingVelocityCoefficient;
// create the fling and adjust scrollers
mFlingScroller = new Scroller(context, null, true);
mAdjustScroller = new Scroller(context, new DecelerateInterpolator(2.5f));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
// If not explicitly specified this view is important for accessibility.
if (getImportantForAccessibility() == IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
}
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Should be focusable by default, as the text view whose visibility changes is focusable
if (getFocusable() == View.FOCUSABLE_AUTO) {
setFocusable(View.FOCUSABLE);
setFocusableInTouchMode(true);
}
}
attributes.recycle();
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
final int msrdWdth = getMeasuredWidth();
final int msrdHght = getMeasuredHeight();
// Input text centered horizontally.
final int inptTxtMsrdWdth = mSelectedText.getMeasuredWidth();
final int inptTxtMsrdHght = mSelectedText.getMeasuredHeight();
final int inptTxtLeft = (msrdWdth - inptTxtMsrdWdth) / 2;
final int inptTxtTop = (msrdHght - inptTxtMsrdHght) / 2;
final int inptTxtRight = inptTxtLeft + inptTxtMsrdWdth;
final int inptTxtBottom = inptTxtTop + inptTxtMsrdHght;
mSelectedText.layout(inptTxtLeft, inptTxtTop, inptTxtRight, inptTxtBottom);
mSelectedTextCenterX = mSelectedText.getX() + mSelectedText.getMeasuredWidth() / 2f - 2f;
mSelectedTextCenterY = mSelectedText.getY() + mSelectedText.getMeasuredHeight() / 2f - 5f;
if (changed) {
// need to do all this when we know our size
initializeSelectorWheel();
initializeFadingEdges();
final int dividerDistance = 2 * mDividerThickness + mDividerDistance;
if (isHorizontalMode()) {
mLeftDividerLeft = (getWidth() - mDividerDistance) / 2 - mDividerThickness;
mRightDividerRight = mLeftDividerLeft + dividerDistance;
mBottomDividerBottom = getHeight();
} else {
mTopDividerTop = (getHeight() - mDividerDistance) / 2 - mDividerThickness;
mBottomDividerBottom = mTopDividerTop + dividerDistance;
}
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// Try greedily to fit the max width and height.
final int newWidthMeasureSpec = makeMeasureSpec(widthMeasureSpec, mMaxWidth);
final int newHeightMeasureSpec = makeMeasureSpec(heightMeasureSpec, mMaxHeight);
super.onMeasure(newWidthMeasureSpec, newHeightMeasureSpec);
// Flag if we are measured with width or height less than the respective min.
final int widthSize = resolveSizeAndStateRespectingMinSize(mMinWidth, getMeasuredWidth(),
widthMeasureSpec);
final int heightSize = resolveSizeAndStateRespectingMinSize(mMinHeight, getMeasuredHeight(),
heightMeasureSpec);
setMeasuredDimension(widthSize, heightSize);
}
/**
* Move to the final position of a scroller. Ensures to force finish the scroller
* and if it is not at its final position a scroll of the selector wheel is
* performed to fast forward to the final position.
*
* @param scroller The scroller to whose final position to get.
* @return True of the a move was performed, i.e. the scroller was not in final position.
*/
private boolean moveToFinalScrollerPosition(Scroller scroller) {
scroller.forceFinished(true);
if (isHorizontalMode()) {
int amountToScroll = scroller.getFinalX() - scroller.getCurrX();
int futureScrollOffset = (mCurrentScrollOffset + amountToScroll) % mSelectorElementSize;
int overshootAdjustment = mInitialScrollOffset - futureScrollOffset;
if (overshootAdjustment != 0) {
if (Math.abs(overshootAdjustment) > mSelectorElementSize / 2) {
if (overshootAdjustment > 0) {
overshootAdjustment -= mSelectorElementSize;
} else {
overshootAdjustment += mSelectorElementSize;
}
}
amountToScroll += overshootAdjustment;
scrollBy(amountToScroll, 0);
return true;
}
} else {
int amountToScroll = scroller.getFinalY() - scroller.getCurrY();
int futureScrollOffset = (mCurrentScrollOffset + amountToScroll) % mSelectorElementSize;
int overshootAdjustment = mInitialScrollOffset - futureScrollOffset;
if (overshootAdjustment != 0) {