Skip to content

Commit

Permalink
Merge pull request #63 from ShawnLin013/feature/Order-setting
Browse files Browse the repository at this point in the history
Feature/order setting
  • Loading branch information
ShawnLin013 authored Jun 14, 2017
2 parents b0562d3 + 98526bb commit c619afc
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 50 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ It's based on [android.widget.NumberPicker](https://android.googlesource.com/pla
- Customizable fonts(color, size, typeface)
- Customizable dividers(color, distance, thickness)
- Horizontal and Vertical mode are both supported
- Ascending and Descending order are both supported
- Also supports the negative values

## Usage
Expand Down Expand Up @@ -70,9 +71,9 @@ add `xmlns:app="http://schemas.android.com/apk/res-auto"`
app:np_max="59"
app:np_min="0"
app:np_selectedTextColor="@color/colorPrimary"
app:np_selectedTextSize="@dimen/selected_text_size"
app:np_textColor="@color/colorPrimary"
app:np_textSize="@dimen/text_size"
app:np_selectedTextSize="@dimen/selected_text_size"
app:np_typeface="@string/roboto_light"
app:np_value="3" />
```
Expand All @@ -89,11 +90,12 @@ add `xmlns:app="http://schemas.android.com/apk/res-auto"`
|np_formatter|The formatter of the numbers.|
|np_max|The max value of this widget.|
|np_min|The min value of this widget.|
|np_order|The order of this widget. Default is ascending.|
|np_orientation|The orientation of this widget. Default is vertical.|
|np_selectedTextColor|The text color of the selected number.|
|np_selectedTextSize|The text size of the selected number.|
|np_textColor|The text color of the numbers.|
|np_textSize|The text size of the numbers.|
|np_selectedTextSize|The text size of the selected number.|
|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.|
Expand All @@ -111,7 +113,7 @@ buildscript {
}
dependencies {
compile 'com.shawnlin:number-picker:2.4.3'
compile 'com.shawnlin:number-picker:2.4.4'
}
```

Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
VERSION_CODE=11
VERSION_NAME=2.4.3
VERSION_CODE=12
VERSION_NAME=2.4.4

GROUP=com.shawnlin
ARTIFACT_ID=number-picker
Expand Down
153 changes: 114 additions & 39 deletions library/src/main/java/com/shawnlin/numberpicker/NumberPicker.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,15 @@ public class NumberPicker extends LinearLayout {
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;

/**
* The default update interval during long press.
*/
Expand Down Expand Up @@ -94,6 +100,11 @@ public class NumberPicker extends LinearLayout {
*/
private static final int SIZE_UNSPECIFIED = -1;

/**
* The default color of divider.
*/
private static final int DEFAULT_DIVIDER_COLOR = 0xFF000000;

/**
* The default max value of this widget.
*/
Expand Down Expand Up @@ -403,7 +414,7 @@ public static final Formatter getTwoDigitFormatter() {
/**
* The color of the selection divider.
*/
private int mSelectionDividerColor;
private int mSelectionDividerColor = DEFAULT_DIVIDER_COLOR;

/**
* The distance between the two selection dividers.
Expand Down Expand Up @@ -460,6 +471,11 @@ public static final Formatter getTwoDigitFormatter() {
*/
private int mOrientation;

/**
* The order of this widget.
*/
private int mOrder;

/**
* The context of this widget.
*/
Expand Down Expand Up @@ -574,6 +590,7 @@ public NumberPicker(Context context, AttributeSet attrs, int defStyle) {
mSelectionDividerThickness = attributesArray.getDimensionPixelSize(
R.styleable.NumberPicker_np_dividerThickness, defSelectionDividerThickness);

mOrder = attributesArray.getInt(R.styleable.NumberPicker_np_order, ASCENDING);
mOrientation = attributesArray.getInt(R.styleable.NumberPicker_np_orientation, VERTICAL);

mWidth = attributesArray.getDimensionPixelSize(R.styleable.NumberPicker_np_width, SIZE_UNSPECIFIED);
Expand All @@ -588,9 +605,9 @@ public NumberPicker(Context context, AttributeSet attrs, int defStyle) {
mMinValue = attributesArray.getInt(R.styleable.NumberPicker_np_min, mMinValue);

mSelectedTextColor = attributesArray.getColor(R.styleable.NumberPicker_np_selectedTextColor, mSelectedTextColor);
mSelectedTextSize = attributesArray.getDimension(R.styleable.NumberPicker_np_selectedTextSize, spToPx(mSelectedTextSize));
mTextColor = attributesArray.getColor(R.styleable.NumberPicker_np_textColor, mTextColor);
mTextSize = attributesArray.getDimension(R.styleable.NumberPicker_np_textSize, spToPx(mTextSize));
mSelectedTextSize = attributesArray.getDimension(R.styleable.NumberPicker_np_selectedTextSize, spToPx(mSelectedTextSize));
mTypeface = Typeface.create(attributesArray.getString(R.styleable.NumberPicker_np_typeface), Typeface.NORMAL);
mFormatter = stringToFormatter(attributesArray.getString(R.styleable.NumberPicker_np_formatter));
mWheelItemCount = attributesArray.getInt(R.styleable.NumberPicker_np_wheelItemCount, mWheelItemCount);
Expand Down Expand Up @@ -1003,32 +1020,58 @@ public void setEnabled(boolean enabled) {

@Override
public void scrollBy(int x, int y) {
int[] selectorIndices = mSelectorIndices;
int[] selectorIndices = getSelectorIndices();
int gap;
if (isHorizontalMode()) {
if (!mWrapSelectorWheel && x > 0
&& selectorIndices[mWheelMiddleItemIndex] <= mMinValue) {
mCurrentScrollOffset = mInitialScrollOffset;
return;
}
if (!mWrapSelectorWheel && x < 0
&& selectorIndices[mWheelMiddleItemIndex] >= mMaxValue) {
mCurrentScrollOffset = mInitialScrollOffset;
return;
if (isAscendingOrder()) {
if (!mWrapSelectorWheel && x > 0
&& selectorIndices[mWheelMiddleItemIndex] <= mMinValue) {
mCurrentScrollOffset = mInitialScrollOffset;
return;
}
if (!mWrapSelectorWheel && x < 0
&& selectorIndices[mWheelMiddleItemIndex] >= mMaxValue) {
mCurrentScrollOffset = mInitialScrollOffset;
return;
}
} else {
if (!mWrapSelectorWheel && x > 0
&& selectorIndices[mWheelMiddleItemIndex] >= mMaxValue) {
mCurrentScrollOffset = mInitialScrollOffset;
return;
}
if (!mWrapSelectorWheel && x < 0
&& selectorIndices[mWheelMiddleItemIndex] <= mMinValue) {
mCurrentScrollOffset = mInitialScrollOffset;
return;
}
}

mCurrentScrollOffset += x;
gap = mSelectorTextGapWidth;
} else {
if (!mWrapSelectorWheel && y > 0
&& selectorIndices[mWheelMiddleItemIndex] <= mMinValue) {
mCurrentScrollOffset = mInitialScrollOffset;
return;
}
if (!mWrapSelectorWheel && y < 0
&& selectorIndices[mWheelMiddleItemIndex] >= mMaxValue) {
mCurrentScrollOffset = mInitialScrollOffset;
return;
if (isAscendingOrder()) {
if (!mWrapSelectorWheel && y > 0
&& selectorIndices[mWheelMiddleItemIndex] <= mMinValue) {
mCurrentScrollOffset = mInitialScrollOffset;
return;
}
if (!mWrapSelectorWheel && y < 0
&& selectorIndices[mWheelMiddleItemIndex] >= mMaxValue) {
mCurrentScrollOffset = mInitialScrollOffset;
return;
}
} else {
if (!mWrapSelectorWheel && y > 0
&& selectorIndices[mWheelMiddleItemIndex] >= mMaxValue) {
mCurrentScrollOffset = mInitialScrollOffset;
return;
}
if (!mWrapSelectorWheel && y < 0
&& selectorIndices[mWheelMiddleItemIndex] <= mMinValue) {
mCurrentScrollOffset = mInitialScrollOffset;
return;
}
}

mCurrentScrollOffset += y;
Expand All @@ -1037,15 +1080,23 @@ public void scrollBy(int x, int y) {

while (mCurrentScrollOffset - mInitialScrollOffset > gap) {
mCurrentScrollOffset -= mSelectorElementSize;
decrementSelectorIndices(selectorIndices);
if (isAscendingOrder()) {
decrementSelectorIndices(selectorIndices);
} else {
incrementSelectorIndices(selectorIndices);
}
setValueInternal(selectorIndices[mWheelMiddleItemIndex], true);
if (!mWrapSelectorWheel && selectorIndices[mWheelMiddleItemIndex] < mMinValue) {
mCurrentScrollOffset = mInitialScrollOffset;
}
}
while (mCurrentScrollOffset - mInitialScrollOffset < -gap) {
mCurrentScrollOffset += mSelectorElementSize;
incrementSelectorIndices(selectorIndices);
if (isAscendingOrder()) {
incrementSelectorIndices(selectorIndices);
} else {
decrementSelectorIndices(selectorIndices);
}
setValueInternal(selectorIndices[mWheelMiddleItemIndex], true);
if (!mWrapSelectorWheel && selectorIndices[mWheelMiddleItemIndex] > mMaxValue) {
mCurrentScrollOffset = mInitialScrollOffset;
Expand Down Expand Up @@ -1369,7 +1420,7 @@ protected void onDraw(Canvas canvas) {
}

// draw the selector wheel
int[] selectorIndices = mSelectorIndices;
int[] selectorIndices = getSelectorIndices();
for (int i = 0; i < selectorIndices.length; i++) {
if (i == mWheelMiddleItemIndex) {
mSelectorWheelPaint.setTextSize(mSelectedTextSize);
Expand All @@ -1379,7 +1430,7 @@ protected void onDraw(Canvas canvas) {
mSelectorWheelPaint.setColor(mTextColor);
}

int selectorIndex = selectorIndices[i];
int selectorIndex = selectorIndices[isAscendingOrder() ? i : selectorIndices.length - i - 1];
String scrollSelectorValue = mSelectorIndexToStringCache.get(selectorIndex);
// Do not draw the middle item if input is visible since the input
// is shown only if the wheel is static and it covers the middle
Expand Down Expand Up @@ -1528,7 +1579,7 @@ public static int resolveSizeAndState(int size, int measureSpec, int childMeasur
*/
private void initializeSelectorWheelIndices() {
mSelectorIndexToStringCache.clear();
int[] selectorIndices = mSelectorIndices;
int[] selectorIndices = getSelectorIndices();
int current = getValue();
for (int i = 0; i < mSelectorIndices.length; i++) {
int selectorIndex = current + (i - mWheelMiddleItemIndex);
Expand Down Expand Up @@ -1599,7 +1650,7 @@ private void changeValueByOne(boolean increment) {

private void initializeSelectorWheel() {
initializeSelectorWheelIndices();
int[] selectorIndices = mSelectorIndices;
int[] selectorIndices = getSelectorIndices();
int totalTextSize = selectorIndices.length * (int) mTextSize;
float textGapCount = selectorIndices.length;
int editTextTextPosition;
Expand Down Expand Up @@ -1693,6 +1744,10 @@ private int getWrappedSelectorIndex(int selectorIndex) {
return selectorIndex;
}

private int[] getSelectorIndices() {
return mSelectorIndices;
}

/**
* Increments the <code>selectorIndices</code> whose string representations
* will be displayed in the selector.
Expand Down Expand Up @@ -2038,6 +2093,15 @@ public void setDividerThickness(int thickness) {
mSelectionDividerThickness = (int) dpToPx(thickness);
}

/**
* Should sort numbers in ascending or descending order.
* @param order Pass {@link #ASCENDING} or {@link #ASCENDING}.
* Default value is {@link #DESCENDING}.
*/
public void setOrder(@Order int order) {
mOrder = order;
}

public void setOrientation(@Orientation int orientation) {
mOrientation = orientation;
setWidthAndHeight();
Expand Down Expand Up @@ -2070,6 +2134,15 @@ public void setSelectedTextColorResource(@ColorRes int colorId) {
setSelectedTextColor(ContextCompat.getColor(mContext, colorId));
}

public void setSelectedTextSize(float textSize) {
mSelectedTextSize = textSize;
mSelectedText.setTextSize(pxToSp(mSelectedTextSize));
}

public void setSelectedTextSize(@DimenRes int dimenId) {
setSelectedTextSize(getResources().getDimension(dimenId));
}

public void setTextColor(@ColorInt int color) {
mTextColor = color;
mSelectorWheelPaint.setColor(mTextColor);
Expand All @@ -2087,14 +2160,7 @@ public void setTextSize(float textSize) {
public void setTextSize(@DimenRes int dimenId) {
setTextSize(getResources().getDimension(dimenId));
}
public void setSelectedTextSize(float textSize) {
mSelectedTextSize = textSize;
mSelectedText.setTextSize(pxToSp(mSelectedTextSize));
}

public void setSelectedTextSize(@DimenRes int dimenId) {
setSelectedTextSize(getResources().getDimension(dimenId));
}
public void setTypeface(Typeface typeface) {
mTypeface = typeface;
if (mTypeface != null) {
Expand Down Expand Up @@ -2155,7 +2221,11 @@ private float pxToSp(float px) {
}

public boolean isHorizontalMode() {
return mOrientation == HORIZONTAL;
return getOrientation() == HORIZONTAL;
}

public boolean isAscendingOrder() {
return getOrder() == ASCENDING;
}

public int getDividerColor() {
Expand All @@ -2170,6 +2240,10 @@ public float getDividerThickness() {
return pxToDp(mSelectionDividerThickness);
}

public int getOrder() {
return mOrder;
}

public int getOrientation() {
return mOrientation;
}
Expand All @@ -2186,6 +2260,10 @@ public int getSelectedTextColor() {
return mSelectedTextColor;
}

public float getSelectedTextSize() {
return mSelectedTextSize;
}

public int getTextColor() {
return mTextColor;
}
Expand All @@ -2198,7 +2276,4 @@ public Typeface getTypeface() {
return mTypeface;
}

public float getmSelectedTextSize() {
return mSelectedTextSize;
}
}
6 changes: 5 additions & 1 deletion library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,18 @@
<attr name="np_formatter" format="string" />
<attr name="np_max" format="integer" />
<attr name="np_min" format="integer" />
<attr name="np_order" format="enum">
<enum name="ascending" value="0" />
<enum name="descending" value="1" />
</attr>
<attr name="np_orientation" format="enum">
<enum name="horizontal" value="0" />
<enum name="vertical" value="1" />
</attr>
<attr name="np_selectedTextColor" format="color" />
<attr name="np_selectedTextSize" format="dimension" />
<attr name="np_textColor" format="color" />
<attr name="np_textSize" format="dimension" />
<attr name="np_selectedTextSize" format="dimension" />
<attr name="np_typeface" format="string" />
<attr name="np_value" format="integer" />
<attr name="np_wheelItemCount" format="integer" />
Expand Down
Loading

0 comments on commit c619afc

Please sign in to comment.