diff --git a/library/src/main/java/com/shawnlin/numberpicker/NumberPicker.java b/library/src/main/java/com/shawnlin/numberpicker/NumberPicker.java index c320fea..217f466 100644 --- a/library/src/main/java/com/shawnlin/numberpicker/NumberPicker.java +++ b/library/src/main/java/com/shawnlin/numberpicker/NumberPicker.java @@ -77,12 +77,18 @@ public class NumberPicker extends LinearLayout { public static final int LEFT = 2; @Retention(SOURCE) - @IntDef({SIDE_LINES, UNDERLINE}) + @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. @@ -129,6 +135,11 @@ public class NumberPicker extends LinearLayout { */ 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. */ @@ -305,6 +316,16 @@ public static Formatter getTwoDigitFormatter() { */ 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. */ @@ -1896,6 +1917,17 @@ private void drawHorizontalDividers(Canvas canvas) { ); mDividerDrawable.draw(canvas); break; + case DRAWABLE_AS_DIVIDER: + final int drawableLeft = (getWidth() - mDividerLength)/ 2; + final int drawableTop = (getHeight() - mDividerThickness)/2; + final int drawableRight = (getWidth() + mDividerLength)/ 2; + final int drawableBottom = (getHeight() + mDividerThickness)/2; + + mDividerDrawable = getResources().getDrawable(mDividerResource); + mDividerDrawable.setBounds(drawableLeft, drawableTop, drawableRight, drawableBottom); + mDividerDrawable.setAlpha(mDividerDrawableAlphaValue); + mDividerDrawable.draw(canvas); + break; } } @@ -1937,6 +1969,16 @@ private void drawVerticalDividers(Canvas canvas) { ); mDividerDrawable.draw(canvas); break; + case DRAWABLE_AS_DIVIDER: + final int drawableLeft = (getWidth() - mDividerLength) / 2; + final int drawableTop = (getHeight() - mDividerThickness) / 2; + final int drawableRight = (getWidth() + mDividerLength) / 2; + final int drawableBottom = (getHeight() + mDividerThickness) / 2; + mDividerDrawable = getResources().getDrawable(mDividerResource); + mDividerDrawable.setBounds(drawableLeft, drawableTop, drawableRight, drawableBottom); + mDividerDrawable.setAlpha(mDividerDrawableAlphaValue); + mDividerDrawable.draw(canvas); + break; } } @@ -2679,6 +2721,11 @@ public void setDividerColor(@ColorInt int color) { mDividerDrawable = new ColorDrawable(color); } + public void setDividerDrawableResource(int drawableResourceId) + { + mDividerResource = drawableResourceId; + } + public void setDividerColorResource(@ColorRes int colorId) { setDividerColor(ContextCompat.getColor(mContext, colorId)); } diff --git a/library/src/main/res/values/attrs.xml b/library/src/main/res/values/attrs.xml index 48396fa..9303cc8 100644 --- a/library/src/main/res/values/attrs.xml +++ b/library/src/main/res/values/attrs.xml @@ -11,6 +11,7 @@ + diff --git a/sample/src/main/java/com/shawnlin/numberpicker/sample/MainActivity.java b/sample/src/main/java/com/shawnlin/numberpicker/sample/MainActivity.java index 8dded0c..f9f1421 100644 --- a/sample/src/main/java/com/shawnlin/numberpicker/sample/MainActivity.java +++ b/sample/src/main/java/com/shawnlin/numberpicker/sample/MainActivity.java @@ -25,6 +25,9 @@ protected void onCreate(Bundle savedInstanceState) { setSupportActionBar(toolbar); final NumberPicker numberPicker = findViewById(R.id.number_picker); + final NumberPicker numberPickerDrawableDividerVertical = findViewById(R.id.number_picker_drawable_divider_vertical); + final NumberPicker numberPickerDrawableDividerHorizontal = findViewById(R.id.number_picker_drawable_divider_horizontal); + // Set divider color numberPicker.setDividerColor(ContextCompat.getColor(this, R.color.colorPrimary)); @@ -112,6 +115,10 @@ public void onScrollStateChange(NumberPicker picker, int scrollState) { } } }); + + // Set drawable as divider + numberPickerDrawableDividerVertical.setDividerDrawableResource(R.drawable.blue_rectangle); + numberPickerDrawableDividerHorizontal.setDividerDrawableResource(R.drawable.accent_rectangle); } } diff --git a/sample/src/main/res/drawable/accent_rectangle.xml b/sample/src/main/res/drawable/accent_rectangle.xml new file mode 100644 index 0000000..5307798 --- /dev/null +++ b/sample/src/main/res/drawable/accent_rectangle.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/sample/src/main/res/drawable/blue_rectangle.xml b/sample/src/main/res/drawable/blue_rectangle.xml new file mode 100644 index 0000000..b538bd3 --- /dev/null +++ b/sample/src/main/res/drawable/blue_rectangle.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/sample/src/main/res/layout/content_main.xml b/sample/src/main/res/layout/content_main.xml index 51fa40d..1c83cbd 100644 --- a/sample/src/main/res/layout/content_main.xml +++ b/sample/src/main/res/layout/content_main.xml @@ -1,5 +1,6 @@ - @@ -17,17 +20,44 @@ + android:layout_height="200dp" + android:layout_marginStart="50dp" + android:layout_marginLeft="50dp" + app:layout_constraintTop_toTopOf="parent" + app:layout_constraintStart_toStartOf="parent"/> + + + app:np_width="180dp" + app:np_wrapSelectorWheel="true" /> + + + - +