diff options
| author | Svetoslav Ganov <svetoslavganov@google.com> | 2012-04-02 20:31:05 -0700 |
|---|---|---|
| committer | Svetoslav Ganov <svetoslavganov@google.com> | 2012-04-02 21:20:14 -0700 |
| commit | fe41ce4ef97e7da585fdbf06988da8047e72b9f6 (patch) | |
| tree | 150802be2a49f4d963c1bcfcd94d961a0e07ae1f /core/java/android/widget/NumberPicker.java | |
| parent | 88796897d323f9291af3116754622e41221946b1 (diff) | |
Polish the Number/Date/Pickers per UX request.
1. Now the NumberPicker max height is a bit smaller.
2. The Time/Date picker add top and bottom margin to
compensate for the shorter NumberPickers.
3. The Time/DatePicker dialogs have only "Done" button
and tapping onside saves the current state.
bug:6277808
Change-Id: I4c5928debb1c3b7fe126d6cd6745e3c5eb980901
Diffstat (limited to 'core/java/android/widget/NumberPicker.java')
| -rw-r--r-- | core/java/android/widget/NumberPicker.java | 60 |
1 files changed, 50 insertions, 10 deletions
diff --git a/core/java/android/widget/NumberPicker.java b/core/java/android/widget/NumberPicker.java index 4e56cd6a46ee..d897a39ce08c 100644 --- a/core/java/android/widget/NumberPicker.java +++ b/core/java/android/widget/NumberPicker.java @@ -112,10 +112,10 @@ public class NumberPicker extends LinearLayout { private static final int SELECTOR_ADJUSTMENT_DURATION_MILLIS = 800; /** - * The duration of scrolling to the next/previous value while changing the - * current value by one, i.e. increment or decrement. + * The duration of scrolling to the next/previous value while snapping to + * a given position. */ - private static final int CHANGE_CURRENT_BY_ONE_SCROLL_DURATION = 300; + private static final int SNAP_SCROLL_DURATION = 300; /** * The strength of fading in the top and bottom while drawing the selector. @@ -140,7 +140,7 @@ public class NumberPicker extends LinearLayout { /** * Coefficient for adjusting touch scroll distance. */ - private static final float TOUCH_SCROLL_DECELERATION_COEFFICIENT = 2.5f; + private static final float TOUCH_SCROLL_DECELERATION_COEFFICIENT = 2.0f; /** * The resource id for the default layout. @@ -152,7 +152,7 @@ public class NumberPicker extends LinearLayout { */ private static final char[] DIGIT_CHARACTERS = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' - }; + }; /** * Constant for unspecified size. @@ -838,7 +838,13 @@ public class NumberPicker extends LinearLayout { if (absDeltaMoveY > mMinFlingDistance) { fling(initialVelocity); } else { - changeValueByOne(deltaMove < 0); + final int normalizedDeltaMove = + (int) (absDeltaMoveY / TOUCH_SCROLL_DECELERATION_COEFFICIENT); + if (normalizedDeltaMove < mSelectorElementHeight) { + snapToNextValue(deltaMove < 0); + } else { + snapToClosestValue(); + } } onScrollStateChange(OnScrollListener.SCROLL_STATE_FLING); } else { @@ -1509,11 +1515,9 @@ public class NumberPicker extends LinearLayout { } mPreviousScrollerY = 0; if (increment) { - mFlingScroller.startScroll(0, 0, 0, -mSelectorElementHeight, - CHANGE_CURRENT_BY_ONE_SCROLL_DURATION); + mFlingScroller.startScroll(0, 0, 0, -mSelectorElementHeight, SNAP_SCROLL_DURATION); } else { - mFlingScroller.startScroll(0, 0, 0, mSelectorElementHeight, - CHANGE_CURRENT_BY_ONE_SCROLL_DURATION); + mFlingScroller.startScroll(0, 0, 0, mSelectorElementHeight, SNAP_SCROLL_DURATION); } invalidate(); } else { @@ -1902,6 +1906,42 @@ public class NumberPicker extends LinearLayout { return false; } + private void snapToNextValue(boolean increment) { + int deltaY = mCurrentScrollOffset - mInitialScrollOffset; + int amountToScroll = 0; + if (deltaY != 0) { + mPreviousScrollerY = 0; + if (deltaY > 0) { + if (increment) { + amountToScroll = - deltaY; + } else { + amountToScroll = mSelectorElementHeight - deltaY; + } + } else { + if (increment) { + amountToScroll = - mSelectorElementHeight - deltaY; + } else { + amountToScroll = - deltaY; + } + } + mFlingScroller.startScroll(0, 0, 0, amountToScroll, SNAP_SCROLL_DURATION); + invalidate(); + } + } + + private void snapToClosestValue() { + // adjust to the closest value + int deltaY = mInitialScrollOffset - mCurrentScrollOffset; + if (deltaY != 0) { + mPreviousScrollerY = 0; + if (Math.abs(deltaY) > mSelectorElementHeight / 2) { + deltaY += (deltaY > 0) ? -mSelectorElementHeight : mSelectorElementHeight; + } + mFlingScroller.startScroll(0, 0, 0, deltaY, SNAP_SCROLL_DURATION); + invalidate(); + } + } + /** * Command for setting the input text selection. */ |
