From f2f5517d76e48c2e735ba7cb9b8a05142b697167 Mon Sep 17 00:00:00 2001 From: AlmazKh Date: Mon, 25 Apr 2022 18:42:37 +0300 Subject: [PATCH 1/2] Added attr for changing gap size between picker items --- .../shawnlin/numberpicker/NumberPicker.java | 38 +++++++++++++++++-- library/src/main/res/values/attrs.xml | 1 + sample/src/main/res/layout/content_main.xml | 14 +++---- 3 files changed, 42 insertions(+), 11 deletions(-) diff --git a/library/src/main/java/com/shawnlin/numberpicker/NumberPicker.java b/library/src/main/java/com/shawnlin/numberpicker/NumberPicker.java index c320fea..61f272a 100644 --- a/library/src/main/java/com/shawnlin/numberpicker/NumberPicker.java +++ b/library/src/main/java/com/shawnlin/numberpicker/NumberPicker.java @@ -1,5 +1,7 @@ package com.shawnlin.numberpicker; +import static java.lang.annotation.RetentionPolicy.SOURCE; + import android.content.Context; import android.content.res.Configuration; import android.content.res.TypedArray; @@ -44,8 +46,6 @@ 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. */ @@ -174,6 +174,11 @@ public class NumberPicker extends LinearLayout { */ private static final float DEFAULT_LINE_SPACING_MULTIPLIER = 1f; + /** + * The default gap size. + */ + private static final int DEFAULT_GAP_SIZE = 0; + /** * Use a custom NumberPicker formatting callback to use two-digit minutes * strings like "01". Keeping a static formatter etc. is the most efficient @@ -575,6 +580,11 @@ public static Formatter getTwoDigitFormatter() { */ private int mOrientation; + /** + * The gap size between items + */ + private int mGapSize; + /** * The order of this widget. */ @@ -760,6 +770,8 @@ public NumberPicker(Context context, AttributeSet attrs, int defStyle) { mOrder = attributes.getInt(R.styleable.NumberPicker_np_order, ASCENDING); mOrientation = attributes.getInt(R.styleable.NumberPicker_np_orientation, VERTICAL); + mGapSize = attributes.getInt(R.styleable.NumberPicker_np_gapSize, DEFAULT_GAP_SIZE); + final float width = attributes.getDimensionPixelSize(R.styleable.NumberPicker_np_width, SIZE_UNSPECIFIED); final float height = attributes.getDimensionPixelSize(R.styleable.NumberPicker_np_height, @@ -1670,6 +1682,24 @@ public void setMaxValue(int maxValue) { invalidate(); } + /** + * Sets the gap size of the picker. + * + * @param size The gap size. + */ + public void setGapSize(int size) { + mGapSize = size; + } + + /** + * Gets the gap size of the picker. + * + * @return The gap size. + */ + public int getGapSize() { + return mGapSize; + } + /** * Gets the values to be displayed instead of string values. * @@ -2165,12 +2195,12 @@ private void initializeSelectorWheel() { float textGapCount = selectorIndices.length; if (isHorizontalMode()) { float totalTextGapWidth = (getRight() - getLeft()) - totalTextSize; - mSelectorTextGapWidth = (int) (totalTextGapWidth / textGapCount); + mSelectorTextGapWidth = (int) (totalTextGapWidth / textGapCount) + mGapSize; mSelectorElementSize = (int) getMaxTextSize() + mSelectorTextGapWidth; mInitialScrollOffset = (int) (mSelectedTextCenterX - mSelectorElementSize * mWheelMiddleItemIndex); } else { float totalTextGapHeight = (getBottom() - getTop()) - totalTextSize; - mSelectorTextGapHeight = (int) (totalTextGapHeight / textGapCount); + mSelectorTextGapHeight = (int) (totalTextGapHeight / textGapCount) + mGapSize; mSelectorElementSize = (int) getMaxTextSize() + mSelectorTextGapHeight; mInitialScrollOffset = (int) (mSelectedTextCenterY - mSelectorElementSize * mWheelMiddleItemIndex); } diff --git a/library/src/main/res/values/attrs.xml b/library/src/main/res/values/attrs.xml index 48396fa..bb704e2 100644 --- a/library/src/main/res/values/attrs.xml +++ b/library/src/main/res/values/attrs.xml @@ -57,5 +57,6 @@ + \ 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..0dd6e1d 100644 --- a/sample/src/main/res/layout/content_main.xml +++ b/sample/src/main/res/layout/content_main.xml @@ -6,10 +6,10 @@ android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" - android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_vertical_margin" + android:paddingBottom="@dimen/activity_vertical_margin" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.shawnlin.numberpicker.sample.MainActivity" tools:showIn="@layout/activity_main"> @@ -24,10 +24,12 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="60dp" - app:np_width="180dp" - app:np_height="64dp" app:np_accessibilityDescriptionEnabled="true" app:np_dividerColor="@color/colorAccent" + app:np_dividerType="underline" + app:np_fadingEdgeEnabled="false" + app:np_gapSize="50" + app:np_height="64dp" app:np_max="10" app:np_min="0" app:np_order="descending" @@ -38,8 +40,6 @@ app:np_textColor="@color/colorAccent" app:np_textSize="@dimen/text_size" app:np_typeface="@string/roboto_light" - app:np_fadingEdgeEnabled="false" - app:np_wrapSelectorWheel="true" - app:np_dividerType="underline" /> - + app:np_width="180dp" + app:np_wrapSelectorWheel="true" /> From 8fd21954270b3cd451e6c1ea38b4735a86948b67 Mon Sep 17 00:00:00 2001 From: Almaz Khamedzhanov <32297268+AlmazKh@users.noreply.github.com> Date: Mon, 25 Apr 2022 23:13:45 +0300 Subject: [PATCH 2/2] Update documentation by gap size attr --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 1211b68..6795ae1 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,9 @@ numberPicker.setMaxValue(59); numberPicker.setMinValue(0); numberPicker.setValue(3); +//Set gap size +numberPicker.setGapSize(50); + // Using string values // IMPORTANT! setMinValue to 1 and call setDisplayedValues after setMinValue and setMaxValue String[] data = {"A", "B", "C", "D", "E", "F", "G", "H", "I"}; @@ -127,6 +130,7 @@ add `xmlns:app="http://schemas.android.com/apk/res-auto"` app:np_height="180dp" app:np_dividerColor="@color/colorPrimary" app:np_formatter="@string/number_picker_formatter" + app:np_gapSize="50" app:np_max="59" app:np_min="0" app:np_selectedTextColor="@color/colorPrimary" @@ -155,6 +159,7 @@ add `xmlns:app="http://schemas.android.com/apk/res-auto"` |np_hideWheelUntilFocused|Flag whether the selector wheel should hidden until the picker has focus.| |np_itemSpacing|Amount of space between items.| |np_lineSpacingMultiplier|The line spacing multiplier for the multiple lines.| +|np_gapSize|The gap size of this widget.| |np_max|The max value of this widget.| |np_maxFlingVelocityCoefficient|The coefficient to adjust (divide) the max fling velocity.| |np_min|The min value of this widget.|