diff --git a/README.md b/README.md index 04af984..cf4145a 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ It's based on [android.widget.NumberPicker](https://android.googlesource.com/pla ## Features -- Customizable fonts(color, size, typeface) +- Customizable fonts(color, size, strikethrough, underline, typeface) - Customizable dividers(color, distance, thickness) - Horizontal and Vertical mode are both supported - Ascending and Descending order are both supported @@ -119,7 +119,7 @@ add `xmlns:app="http://schemas.android.com/apk/res-auto"` ### Attributes |attribute name|attribute description| -|:-:|:-:| +|:---:|:---:| |np_width|The width of this widget.| |np_height|The height of this widget.| |np_dividerColor|The color of the selection divider.| @@ -133,10 +133,16 @@ add `xmlns:app="http://schemas.android.com/apk/res-auto"` |np_order|The order of this widget. Default is ascending.| |np_orientation|The orientation of this widget. Default is vertical.| |np_scrollerEnabled|Flag whether the scroller should enabled.| +|np_selectedTextAlign|The text align of the selected number. Default is center.| |np_selectedTextColor|The text color of the selected number.| |np_selectedTextSize|The text size of the selected number.| +|np_selectedTextStrikeThru|Flag whether the selected text should strikethroughed.| +|np_selectedTextUnderline|Flag whether the selected text should underlined.| +|np_textAlign|The text align of the numbers. Default is center.| |np_textColor|The text color of the numbers.| |np_textSize|The text size of the numbers.| +|np_textStrikeThru|Flag whether the text should strikethroughed.| +|np_textUnderline|Flag whether the text should underlined.| |np_typeface|The typeface of the numbers.| |np_value|The current value of this widget.| |np_wheelItemCount|The number of items show in the selector wheel.| diff --git a/library/src/main/java/com/shawnlin/numberpicker/NumberPicker.java b/library/src/main/java/com/shawnlin/numberpicker/NumberPicker.java index 63700dc..af715da 100644 --- a/library/src/main/java/com/shawnlin/numberpicker/NumberPicker.java +++ b/library/src/main/java/com/shawnlin/numberpicker/NumberPicker.java @@ -5,7 +5,6 @@ import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Paint; -import android.graphics.Paint.Align; import android.graphics.Typeface; import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.Drawable; @@ -62,6 +61,14 @@ public class NumberPicker extends LinearLayout { 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; + /** * The default update interval during long press. */ @@ -132,6 +139,11 @@ public class NumberPicker extends LinearLayout { */ 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. */ @@ -238,11 +250,36 @@ public static final Formatter getTwoDigitFormatter() { */ 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 align of the text. + */ + private int mTextAlign = DEFAULT_TEXT_ALIGN; + /** * The color of the text. */ @@ -254,9 +291,14 @@ public static final Formatter getTwoDigitFormatter() { private float mTextSize = DEFAULT_TEXT_SIZE; /** - * The size of the selected text. + * Flag whether the text should strikethroughed. */ - private float mSelectedTextSize = DEFAULT_TEXT_SIZE; + private boolean mTextStrikeThru; + + /** + * Flag whether the text should underlined. + */ + private boolean mTextUnderline; /** * The typeface of the text. @@ -662,13 +704,24 @@ public NumberPicker(Context context, AttributeSet attrs, int defStyle) { 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); + 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)); @@ -701,7 +754,7 @@ public NumberPicker(Context context, AttributeSet attrs, int defStyle) { // create the selector wheel paint Paint paint = new Paint(); paint.setAntiAlias(true); - paint.setTextAlign(Align.CENTER); + paint.setTextAlign(Paint.Align.CENTER); mSelectorWheelPaint = paint; setSelectedTextColor(mSelectedTextColor); @@ -1565,11 +1618,17 @@ protected void onDraw(Canvas canvas) { int[] selectorIndices = getSelectorIndices(); for (int i = 0; i < selectorIndices.length; i++) { if (i == mWheelMiddleItemIndex) { + mSelectorWheelPaint.setTextAlign(Paint.Align.values()[mSelectedTextAlign]); mSelectorWheelPaint.setTextSize(mSelectedTextSize); mSelectorWheelPaint.setColor(mSelectedTextColor); + mSelectorWheelPaint.setStrikeThruText(mSelectedTextStrikeThru); + mSelectorWheelPaint.setUnderlineText(mSelectedTextUnderline); } else { + mSelectorWheelPaint.setTextAlign(Paint.Align.values()[mTextAlign]); mSelectorWheelPaint.setTextSize(mTextSize); mSelectorWheelPaint.setColor(mTextColor); + mSelectorWheelPaint.setStrikeThruText(mTextStrikeThru); + mSelectorWheelPaint.setUnderlineText(mTextUnderline); } int selectorIndex = selectorIndices[isAscendingOrder() @@ -2341,6 +2400,10 @@ public void setScrollerEnabled(boolean scrollerEnabled) { mScrollerEnabled = scrollerEnabled; } + public void setSelectedTextAlign(@Align int align) { + mSelectedTextAlign = align; + } + public void setSelectedTextColor(@ColorInt int color) { mSelectedTextColor = color; mSelectedText.setTextColor(mSelectedTextColor); @@ -2359,6 +2422,18 @@ public void setSelectedTextSize(@DimenRes int dimenId) { setSelectedTextSize(getResources().getDimension(dimenId)); } + public void setSelectedTextStrikeThru(boolean strikeThruText) { + mSelectedTextStrikeThru = strikeThruText; + } + + public void setSelectedTextUnderline(boolean underlineText) { + mSelectedTextUnderline = underlineText; + } + + public void setTextAlign(@Align int align) { + mTextAlign = align; + } + public void setTextColor(@ColorInt int color) { mTextColor = color; mSelectorWheelPaint.setColor(mTextColor); @@ -2377,6 +2452,14 @@ public void setTextSize(@DimenRes int dimenId) { setTextSize(getResources().getDimension(dimenId)); } + public void setTextStrikeThru(boolean strikeThruText) { + mTextStrikeThru = strikeThruText; + } + + public void setTextUnderline(boolean underlineText) { + mTextUnderline = underlineText; + } + public void setTypeface(Typeface typeface) { mTypeface = typeface; if (mTypeface != null) { @@ -2455,6 +2538,10 @@ public boolean isScrollerEnabled() { return mScrollerEnabled; } + public int getSelectedTextAlign() { + return mSelectedTextAlign; + } + public int getSelectedTextColor() { return mSelectedTextColor; } @@ -2463,6 +2550,18 @@ public float getSelectedTextSize() { return mSelectedTextSize; } + public boolean getSelectedTextStrikeThru() { + return mSelectedTextStrikeThru; + } + + public boolean getSelectedTextUnderline() { + return mSelectedTextUnderline; + } + + public int getTextAlign() { + return mTextAlign; + } + public int getTextColor() { return mTextColor; } @@ -2471,6 +2570,14 @@ public float getTextSize() { return spToPx(mTextSize); } + public boolean getTextStrikeThru() { + return mTextStrikeThru; + } + + public boolean getTextUnderline() { + return mTextUnderline; + } + public Typeface getTypeface() { return mTypeface; } diff --git a/library/src/main/res/values/attrs.xml b/library/src/main/res/values/attrs.xml index caaf70b..36bced0 100644 --- a/library/src/main/res/values/attrs.xml +++ b/library/src/main/res/values/attrs.xml @@ -23,10 +23,24 @@ + + + + + + + + + + + + + +