@@ -51,9 +51,15 @@ public class NumberPicker extends LinearLayout {
5151 public @interface Orientation {}
5252
5353 public static final int VERTICAL = LinearLayout .VERTICAL ;
54-
5554 public static final int HORIZONTAL = LinearLayout .HORIZONTAL ;
5655
56+ @ Retention (SOURCE )
57+ @ IntDef ({ASCENDING , DESCENDING })
58+ public @interface Order {}
59+
60+ public static final int ASCENDING = 0 ;
61+ public static final int DESCENDING = 1 ;
62+
5763 /**
5864 * The default update interval during long press.
5965 */
@@ -94,6 +100,11 @@ public class NumberPicker extends LinearLayout {
94100 */
95101 private static final int SIZE_UNSPECIFIED = -1 ;
96102
103+ /**
104+ * The default color of divider.
105+ */
106+ private static final int DEFAULT_DIVIDER_COLOR = 0xFF000000 ;
107+
97108 /**
98109 * The default max value of this widget.
99110 */
@@ -403,7 +414,7 @@ public static final Formatter getTwoDigitFormatter() {
403414 /**
404415 * The color of the selection divider.
405416 */
406- private int mSelectionDividerColor ;
417+ private int mSelectionDividerColor = DEFAULT_DIVIDER_COLOR ;
407418
408419 /**
409420 * The distance between the two selection dividers.
@@ -460,6 +471,11 @@ public static final Formatter getTwoDigitFormatter() {
460471 */
461472 private int mOrientation ;
462473
474+ /**
475+ * The order of this widget.
476+ */
477+ private int mOrder ;
478+
463479 /**
464480 * The context of this widget.
465481 */
@@ -574,6 +590,7 @@ public NumberPicker(Context context, AttributeSet attrs, int defStyle) {
574590 mSelectionDividerThickness = attributesArray .getDimensionPixelSize (
575591 R .styleable .NumberPicker_np_dividerThickness , defSelectionDividerThickness );
576592
593+ mOrder = attributesArray .getInt (R .styleable .NumberPicker_np_order , ASCENDING );
577594 mOrientation = attributesArray .getInt (R .styleable .NumberPicker_np_orientation , VERTICAL );
578595
579596 mWidth = attributesArray .getDimensionPixelSize (R .styleable .NumberPicker_np_width , SIZE_UNSPECIFIED );
@@ -588,9 +605,9 @@ public NumberPicker(Context context, AttributeSet attrs, int defStyle) {
588605 mMinValue = attributesArray .getInt (R .styleable .NumberPicker_np_min , mMinValue );
589606
590607 mSelectedTextColor = attributesArray .getColor (R .styleable .NumberPicker_np_selectedTextColor , mSelectedTextColor );
608+ mSelectedTextSize = attributesArray .getDimension (R .styleable .NumberPicker_np_selectedTextSize , spToPx (mSelectedTextSize ));
591609 mTextColor = attributesArray .getColor (R .styleable .NumberPicker_np_textColor , mTextColor );
592610 mTextSize = attributesArray .getDimension (R .styleable .NumberPicker_np_textSize , spToPx (mTextSize ));
593- mSelectedTextSize = attributesArray .getDimension (R .styleable .NumberPicker_np_selectedTextSize , spToPx (mSelectedTextSize ));
594611 mTypeface = Typeface .create (attributesArray .getString (R .styleable .NumberPicker_np_typeface ), Typeface .NORMAL );
595612 mFormatter = stringToFormatter (attributesArray .getString (R .styleable .NumberPicker_np_formatter ));
596613 mWheelItemCount = attributesArray .getInt (R .styleable .NumberPicker_np_wheelItemCount , mWheelItemCount );
@@ -1003,32 +1020,58 @@ public void setEnabled(boolean enabled) {
10031020
10041021 @ Override
10051022 public void scrollBy (int x , int y ) {
1006- int [] selectorIndices = mSelectorIndices ;
1023+ int [] selectorIndices = getSelectorIndices () ;
10071024 int gap ;
10081025 if (isHorizontalMode ()) {
1009- if (!mWrapSelectorWheel && x > 0
1010- && selectorIndices [mWheelMiddleItemIndex ] <= mMinValue ) {
1011- mCurrentScrollOffset = mInitialScrollOffset ;
1012- return ;
1013- }
1014- if (!mWrapSelectorWheel && x < 0
1015- && selectorIndices [mWheelMiddleItemIndex ] >= mMaxValue ) {
1016- mCurrentScrollOffset = mInitialScrollOffset ;
1017- return ;
1026+ if (isAscendingOrder ()) {
1027+ if (!mWrapSelectorWheel && x > 0
1028+ && selectorIndices [mWheelMiddleItemIndex ] <= mMinValue ) {
1029+ mCurrentScrollOffset = mInitialScrollOffset ;
1030+ return ;
1031+ }
1032+ if (!mWrapSelectorWheel && x < 0
1033+ && selectorIndices [mWheelMiddleItemIndex ] >= mMaxValue ) {
1034+ mCurrentScrollOffset = mInitialScrollOffset ;
1035+ return ;
1036+ }
1037+ } else {
1038+ if (!mWrapSelectorWheel && x > 0
1039+ && selectorIndices [mWheelMiddleItemIndex ] >= mMaxValue ) {
1040+ mCurrentScrollOffset = mInitialScrollOffset ;
1041+ return ;
1042+ }
1043+ if (!mWrapSelectorWheel && x < 0
1044+ && selectorIndices [mWheelMiddleItemIndex ] <= mMinValue ) {
1045+ mCurrentScrollOffset = mInitialScrollOffset ;
1046+ return ;
1047+ }
10181048 }
10191049
10201050 mCurrentScrollOffset += x ;
10211051 gap = mSelectorTextGapWidth ;
10221052 } else {
1023- if (!mWrapSelectorWheel && y > 0
1024- && selectorIndices [mWheelMiddleItemIndex ] <= mMinValue ) {
1025- mCurrentScrollOffset = mInitialScrollOffset ;
1026- return ;
1027- }
1028- if (!mWrapSelectorWheel && y < 0
1029- && selectorIndices [mWheelMiddleItemIndex ] >= mMaxValue ) {
1030- mCurrentScrollOffset = mInitialScrollOffset ;
1031- return ;
1053+ if (isAscendingOrder ()) {
1054+ if (!mWrapSelectorWheel && y > 0
1055+ && selectorIndices [mWheelMiddleItemIndex ] <= mMinValue ) {
1056+ mCurrentScrollOffset = mInitialScrollOffset ;
1057+ return ;
1058+ }
1059+ if (!mWrapSelectorWheel && y < 0
1060+ && selectorIndices [mWheelMiddleItemIndex ] >= mMaxValue ) {
1061+ mCurrentScrollOffset = mInitialScrollOffset ;
1062+ return ;
1063+ }
1064+ } else {
1065+ if (!mWrapSelectorWheel && y > 0
1066+ && selectorIndices [mWheelMiddleItemIndex ] >= mMaxValue ) {
1067+ mCurrentScrollOffset = mInitialScrollOffset ;
1068+ return ;
1069+ }
1070+ if (!mWrapSelectorWheel && y < 0
1071+ && selectorIndices [mWheelMiddleItemIndex ] <= mMinValue ) {
1072+ mCurrentScrollOffset = mInitialScrollOffset ;
1073+ return ;
1074+ }
10321075 }
10331076
10341077 mCurrentScrollOffset += y ;
@@ -1037,15 +1080,23 @@ public void scrollBy(int x, int y) {
10371080
10381081 while (mCurrentScrollOffset - mInitialScrollOffset > gap ) {
10391082 mCurrentScrollOffset -= mSelectorElementSize ;
1040- decrementSelectorIndices (selectorIndices );
1083+ if (isAscendingOrder ()) {
1084+ decrementSelectorIndices (selectorIndices );
1085+ } else {
1086+ incrementSelectorIndices (selectorIndices );
1087+ }
10411088 setValueInternal (selectorIndices [mWheelMiddleItemIndex ], true );
10421089 if (!mWrapSelectorWheel && selectorIndices [mWheelMiddleItemIndex ] < mMinValue ) {
10431090 mCurrentScrollOffset = mInitialScrollOffset ;
10441091 }
10451092 }
10461093 while (mCurrentScrollOffset - mInitialScrollOffset < -gap ) {
10471094 mCurrentScrollOffset += mSelectorElementSize ;
1048- incrementSelectorIndices (selectorIndices );
1095+ if (isAscendingOrder ()) {
1096+ incrementSelectorIndices (selectorIndices );
1097+ } else {
1098+ decrementSelectorIndices (selectorIndices );
1099+ }
10491100 setValueInternal (selectorIndices [mWheelMiddleItemIndex ], true );
10501101 if (!mWrapSelectorWheel && selectorIndices [mWheelMiddleItemIndex ] > mMaxValue ) {
10511102 mCurrentScrollOffset = mInitialScrollOffset ;
@@ -1369,7 +1420,7 @@ protected void onDraw(Canvas canvas) {
13691420 }
13701421
13711422 // draw the selector wheel
1372- int [] selectorIndices = mSelectorIndices ;
1423+ int [] selectorIndices = getSelectorIndices () ;
13731424 for (int i = 0 ; i < selectorIndices .length ; i ++) {
13741425 if (i == mWheelMiddleItemIndex ) {
13751426 mSelectorWheelPaint .setTextSize (mSelectedTextSize );
@@ -1379,7 +1430,7 @@ protected void onDraw(Canvas canvas) {
13791430 mSelectorWheelPaint .setColor (mTextColor );
13801431 }
13811432
1382- int selectorIndex = selectorIndices [i ];
1433+ int selectorIndex = selectorIndices [isAscendingOrder () ? i : selectorIndices . length - i - 1 ];
13831434 String scrollSelectorValue = mSelectorIndexToStringCache .get (selectorIndex );
13841435 // Do not draw the middle item if input is visible since the input
13851436 // is shown only if the wheel is static and it covers the middle
@@ -1528,7 +1579,7 @@ public static int resolveSizeAndState(int size, int measureSpec, int childMeasur
15281579 */
15291580 private void initializeSelectorWheelIndices () {
15301581 mSelectorIndexToStringCache .clear ();
1531- int [] selectorIndices = mSelectorIndices ;
1582+ int [] selectorIndices = getSelectorIndices () ;
15321583 int current = getValue ();
15331584 for (int i = 0 ; i < mSelectorIndices .length ; i ++) {
15341585 int selectorIndex = current + (i - mWheelMiddleItemIndex );
@@ -1599,7 +1650,7 @@ private void changeValueByOne(boolean increment) {
15991650
16001651 private void initializeSelectorWheel () {
16011652 initializeSelectorWheelIndices ();
1602- int [] selectorIndices = mSelectorIndices ;
1653+ int [] selectorIndices = getSelectorIndices () ;
16031654 int totalTextSize = selectorIndices .length * (int ) mTextSize ;
16041655 float textGapCount = selectorIndices .length ;
16051656 int editTextTextPosition ;
@@ -1693,6 +1744,10 @@ private int getWrappedSelectorIndex(int selectorIndex) {
16931744 return selectorIndex ;
16941745 }
16951746
1747+ private int [] getSelectorIndices () {
1748+ return mSelectorIndices ;
1749+ }
1750+
16961751 /**
16971752 * Increments the <code>selectorIndices</code> whose string representations
16981753 * will be displayed in the selector.
@@ -2038,6 +2093,15 @@ public void setDividerThickness(int thickness) {
20382093 mSelectionDividerThickness = (int ) dpToPx (thickness );
20392094 }
20402095
2096+ /**
2097+ * Should sort numbers in ascending or descending order.
2098+ * @param order Pass {@link #ASCENDING} or {@link #ASCENDING}.
2099+ * Default value is {@link #DESCENDING}.
2100+ */
2101+ public void setOrder (@ Order int order ) {
2102+ mOrder = order ;
2103+ }
2104+
20412105 public void setOrientation (@ Orientation int orientation ) {
20422106 mOrientation = orientation ;
20432107 setWidthAndHeight ();
@@ -2070,6 +2134,15 @@ public void setSelectedTextColorResource(@ColorRes int colorId) {
20702134 setSelectedTextColor (ContextCompat .getColor (mContext , colorId ));
20712135 }
20722136
2137+ public void setSelectedTextSize (float textSize ) {
2138+ mSelectedTextSize = textSize ;
2139+ mSelectedText .setTextSize (pxToSp (mSelectedTextSize ));
2140+ }
2141+
2142+ public void setSelectedTextSize (@ DimenRes int dimenId ) {
2143+ setSelectedTextSize (getResources ().getDimension (dimenId ));
2144+ }
2145+
20732146 public void setTextColor (@ ColorInt int color ) {
20742147 mTextColor = color ;
20752148 mSelectorWheelPaint .setColor (mTextColor );
@@ -2087,14 +2160,7 @@ public void setTextSize(float textSize) {
20872160 public void setTextSize (@ DimenRes int dimenId ) {
20882161 setTextSize (getResources ().getDimension (dimenId ));
20892162 }
2090- public void setSelectedTextSize (float textSize ) {
2091- mSelectedTextSize = textSize ;
2092- mSelectedText .setTextSize (pxToSp (mSelectedTextSize ));
2093- }
20942163
2095- public void setSelectedTextSize (@ DimenRes int dimenId ) {
2096- setSelectedTextSize (getResources ().getDimension (dimenId ));
2097- }
20982164 public void setTypeface (Typeface typeface ) {
20992165 mTypeface = typeface ;
21002166 if (mTypeface != null ) {
@@ -2155,7 +2221,11 @@ private float pxToSp(float px) {
21552221 }
21562222
21572223 public boolean isHorizontalMode () {
2158- return mOrientation == HORIZONTAL ;
2224+ return getOrientation () == HORIZONTAL ;
2225+ }
2226+
2227+ public boolean isAscendingOrder () {
2228+ return getOrder () == ASCENDING ;
21592229 }
21602230
21612231 public int getDividerColor () {
@@ -2170,6 +2240,10 @@ public float getDividerThickness() {
21702240 return pxToDp (mSelectionDividerThickness );
21712241 }
21722242
2243+ public int getOrder () {
2244+ return mOrder ;
2245+ }
2246+
21732247 public int getOrientation () {
21742248 return mOrientation ;
21752249 }
@@ -2186,6 +2260,10 @@ public int getSelectedTextColor() {
21862260 return mSelectedTextColor ;
21872261 }
21882262
2263+ public float getSelectedTextSize () {
2264+ return mSelectedTextSize ;
2265+ }
2266+
21892267 public int getTextColor () {
21902268 return mTextColor ;
21912269 }
@@ -2198,7 +2276,4 @@ public Typeface getTypeface() {
21982276 return mTypeface ;
21992277 }
22002278
2201- public float getmSelectedTextSize () {
2202- return mSelectedTextSize ;
2203- }
22042279}
0 commit comments