diff options
| author | Ned Burns <pixel@google.com> | 2016-08-18 14:22:57 -0400 |
|---|---|---|
| committer | Ned Burns <pixel@google.com> | 2016-08-29 20:09:23 -0400 |
| commit | 20ad07358175f116896b9c86d50d6ce4038a66d8 (patch) | |
| tree | 0c8a4339d04a7d6c6803db350d8aa2735ac742bd /core/java/android/widget/ScrollView.java | |
| parent | 2cf3cc82e3f529b4353e552f5f38a22e9374c003 (diff) | |
Add rotary encoder support to scrolling containers
Change-Id: I1b7a2a60ac9864f2639af81fff810db601b2fbd4
Diffstat (limited to 'core/java/android/widget/ScrollView.java')
| -rw-r--r-- | core/java/android/widget/ScrollView.java | 50 |
1 files changed, 29 insertions, 21 deletions
diff --git a/core/java/android/widget/ScrollView.java b/core/java/android/widget/ScrollView.java index 493b28c688f9..e696ff7229e8 100644 --- a/core/java/android/widget/ScrollView.java +++ b/core/java/android/widget/ScrollView.java @@ -135,6 +135,8 @@ public class ScrollView extends FrameLayout { private int mOverscrollDistance; private int mOverflingDistance; + private int mScrollFactor; + /** * ID of the active pointer. This is used to retain consistency during * drags/flings if multiple pointers are used. @@ -248,6 +250,7 @@ public class ScrollView extends FrameLayout { mMaximumVelocity = configuration.getScaledMaximumFlingVelocity(); mOverscrollDistance = configuration.getScaledOverscrollDistance(); mOverflingDistance = configuration.getScaledOverflingDistance(); + mScrollFactor = configuration.getScaledScrollFactor(); } @Override @@ -782,30 +785,35 @@ public class ScrollView extends FrameLayout { @Override public boolean onGenericMotionEvent(MotionEvent event) { - if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) { - switch (event.getAction()) { - case MotionEvent.ACTION_SCROLL: { - if (!mIsBeingDragged) { - final float vscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL); - if (vscroll != 0) { - final int delta = (int) (vscroll * getVerticalScrollFactor()); - final int range = getScrollRange(); - int oldScrollY = mScrollY; - int newScrollY = oldScrollY - delta; - if (newScrollY < 0) { - newScrollY = 0; - } else if (newScrollY > range) { - newScrollY = range; - } - if (newScrollY != oldScrollY) { - super.scrollTo(mScrollX, newScrollY); - return true; - } - } + switch (event.getAction()) { + case MotionEvent.ACTION_SCROLL: + final float axisValue; + if (event.isFromSource(InputDevice.SOURCE_CLASS_POINTER)) { + axisValue = event.getAxisValue(MotionEvent.AXIS_VSCROLL); + } else if (event.isFromSource(InputDevice.SOURCE_ROTARY_ENCODER)) { + axisValue = event.getAxisValue(MotionEvent.AXIS_SCROLL); + } else { + axisValue = 0; + } + + final int delta = Math.round(axisValue * mScrollFactor); + if (delta != 0) { + final int range = getScrollRange(); + int oldScrollY = mScrollY; + int newScrollY = oldScrollY - delta; + if (newScrollY < 0) { + newScrollY = 0; + } else if (newScrollY > range) { + newScrollY = range; + } + if (newScrollY != oldScrollY) { + super.scrollTo(mScrollX, newScrollY); + return true; } } - } + break; } + return super.onGenericMotionEvent(event); } |
