59public class ScrollView2D
extends FrameLayout
62 static final int ANIMATED_SCROLL_GAP = 250;
63 static final float MAX_SCROLL_FACTOR = 0.5f;
64 private final Rect mTempRect =
new Rect();
66 private long mLastScroll;
67 private Scroller mScroller;
68 private boolean scrollEnabled =
true;
74 private boolean mTwoDScrollViewMovedFocus;
78 private float mLastMotionY;
79 private float mLastMotionX;
84 private boolean mIsLayoutDirty =
true;
90 private View mChildToScrollTo =
null;
96 private boolean mIsBeingDragged =
false;
100 private VelocityTracker mVelocityTracker;
104 private int mTouchSlop;
105 private int mMinimumVelocity;
106 private int mMaximumVelocity;
107 public ScrollView2D(Context context)
110 initTwoDScrollView();
113 public ScrollView2D(Context context, AttributeSet attrs)
115 super(context, attrs);
116 initTwoDScrollView();
119 public ScrollView2D(Context context, AttributeSet attrs,
int defStyle)
121 super(context, attrs, defStyle);
122 initTwoDScrollView();
125 @Override
protected float getTopFadingEdgeStrength()
127 if (getChildCount() == 0)
131 final int length = getVerticalFadingEdgeLength();
132 if (getScrollY() < length)
134 return getScrollY() / (float)length;
139 @Override
protected float getBottomFadingEdgeStrength()
141 if (getChildCount() == 0)
145 final int length = getVerticalFadingEdgeLength();
146 final int bottomEdge = getHeight() - getPaddingBottom();
147 final int span = getChildAt(0).getBottom() - getScrollY() - bottomEdge;
150 return span / (float)length;
155 @Override
protected float getLeftFadingEdgeStrength()
157 if (getChildCount() == 0)
161 final int length = getHorizontalFadingEdgeLength();
162 if (getScrollX() < length)
164 return getScrollX() / (float)length;
169 @Override
protected float getRightFadingEdgeStrength()
171 if (getChildCount() == 0)
175 final int length = getHorizontalFadingEdgeLength();
176 final int rightEdge = getWidth() - getPaddingRight();
177 final int span = getChildAt(0).getRight() - getScrollX() - rightEdge;
180 return span / (float)length;
190 scrollEnabled = enable;
199 return (
int)(MAX_SCROLL_FACTOR * getHeight());
202 public int getMaxScrollAmountHorizontal()
204 return (
int)(MAX_SCROLL_FACTOR * getWidth());
207 private void initTwoDScrollView()
209 mScroller =
new Scroller(getContext());
211 setDescendantFocusability(FOCUS_AFTER_DESCENDANTS);
212 setWillNotDraw(
false);
213 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
214 mTouchSlop = configuration.getScaledTouchSlop();
215 mMinimumVelocity = configuration.getScaledMinimumFlingVelocity();
216 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
219 @Override
public void addView(View child)
221 if (getChildCount() > 0)
223 throw new IllegalStateException(
"TwoDScrollView can host only one direct child");
225 super.addView(child);
228 @Override
public void addView(View child,
int index)
230 if (getChildCount() > 0)
232 throw new IllegalStateException(
"TwoDScrollView can host only one direct child");
234 super.addView(child, index);
237 @Override
public void addView(View child, ViewGroup.LayoutParams params)
239 if (getChildCount() > 0)
241 throw new IllegalStateException(
"TwoDScrollView can host only one direct child");
243 super.addView(child, params);
246 @Override
public void addView(View child,
int index, ViewGroup.LayoutParams params)
248 if (getChildCount() > 0)
250 throw new IllegalStateException(
"TwoDScrollView can host only one direct child");
252 super.addView(child, index, params);
258 private boolean canScroll()
262 View child = getChildAt(0);
265 int childHeight = child.getHeight();
266 int childWidth = child.getWidth();
267 return (getHeight() < childHeight + getPaddingTop() + getPaddingBottom()) ||
268 (getWidth() < childWidth + getPaddingLeft() + getPaddingRight());
273 @Override
public boolean onInterceptTouchEvent(MotionEvent ev)
285 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q &&
286 ev.getClassification() == MotionEvent.CLASSIFICATION_TWO_FINGER_SWIPE)
288 mIsBeingDragged =
false;
292 final int action = ev.getAction();
293 if ((action == MotionEvent.ACTION_MOVE) && (mIsBeingDragged))
299 mIsBeingDragged =
false;
302 final float y = ev.getY();
303 final float x = ev.getX();
306 case MotionEvent.ACTION_MOVE:
315 final int yDiff = (int)Math.abs(y - mLastMotionY);
316 final int xDiff = (int)Math.abs(x - mLastMotionX);
317 if (yDiff > mTouchSlop || xDiff > mTouchSlop)
319 mIsBeingDragged =
true;
323 case MotionEvent.ACTION_DOWN:
333 mIsBeingDragged = !mScroller.isFinished();
336 case MotionEvent.ACTION_CANCEL:
337 case MotionEvent.ACTION_UP:
339 mIsBeingDragged =
false;
347 return mIsBeingDragged;
350 @Override
public boolean onTouchEvent(MotionEvent ev)
353 if (ev.getAction() == MotionEvent.ACTION_DOWN && ev.getEdgeFlags() != 0)
365 if (mVelocityTracker ==
null)
367 mVelocityTracker = VelocityTracker.obtain();
369 mVelocityTracker.addMovement(ev);
371 final int action = ev.getAction();
372 final float y = ev.getY();
373 final float x = ev.getX();
377 case MotionEvent.ACTION_DOWN:
382 if (!mScroller.isFinished())
384 mScroller.abortAnimation();
391 case MotionEvent.ACTION_MOVE:
393 int deltaX = (int)(mLastMotionX - x);
394 int deltaY = (int)(mLastMotionY - y);
400 if (getScrollX() < 0)
407 final int rightEdge = getWidth() - getPaddingRight();
408 final int availableToScroll =
409 getChildAt(0).getRight() - getScrollX() - rightEdge;
410 if (availableToScroll > 0)
412 deltaX = Math.min(availableToScroll, deltaX);
421 if (getScrollY() < 0)
428 final int bottomEdge = getHeight() - getPaddingBottom();
429 final int availableToScroll =
430 getChildAt(0).getBottom() - getScrollY() - bottomEdge;
431 if (availableToScroll > 0)
433 deltaY = Math.min(availableToScroll, deltaY);
440 if (deltaY != 0 || deltaX != 0)
441 scrollBy(deltaX, deltaY);
443 case MotionEvent.ACTION_UP:
444 final VelocityTracker velocityTracker = mVelocityTracker;
445 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
446 int initialXVelocity = (int)velocityTracker.getXVelocity();
447 int initialYVelocity = (int)velocityTracker.getYVelocity();
448 if ((Math.abs(initialXVelocity) + Math.abs(initialYVelocity) > mMinimumVelocity) &&
451 fling(-initialXVelocity, -initialYVelocity);
453 if (mVelocityTracker !=
null)
455 mVelocityTracker.recycle();
456 mVelocityTracker =
null;
477 private View findFocusableViewInMyBounds(
final boolean topFocus,
final int top,
478 final boolean leftFocus,
final int left,
479 View preferredFocusable)
486 final int verticalFadingEdgeLength = getVerticalFadingEdgeLength() / 2;
487 final int topWithoutFadingEdge = top + verticalFadingEdgeLength;
488 final int bottomWithoutFadingEdge = top + getHeight() - verticalFadingEdgeLength;
489 final int horizontalFadingEdgeLength = getHorizontalFadingEdgeLength() / 2;
490 final int leftWithoutFadingEdge = left + horizontalFadingEdgeLength;
491 final int rightWithoutFadingEdge = left + getWidth() - horizontalFadingEdgeLength;
493 if ((preferredFocusable !=
null) &&
494 (preferredFocusable.getTop() < bottomWithoutFadingEdge) &&
495 (preferredFocusable.getBottom() > topWithoutFadingEdge) &&
496 (preferredFocusable.getLeft() < rightWithoutFadingEdge) &&
497 (preferredFocusable.getRight() > leftWithoutFadingEdge))
499 return preferredFocusable;
501 return findFocusableViewInBounds(topFocus, topWithoutFadingEdge, bottomWithoutFadingEdge,
502 leftFocus, leftWithoutFadingEdge, rightWithoutFadingEdge);
519 private View findFocusableViewInBounds(
boolean topFocus,
int top,
int bottom,
boolean leftFocus,
522 List<View> focusables = getFocusables(View.FOCUS_FORWARD);
523 View focusCandidate =
null;
532 boolean foundFullyContainedFocusable =
false;
534 int count = focusables.size();
535 for (
int i = 0; i < count; i++)
537 View view = focusables.get(i);
538 int viewTop = view.getTop();
539 int viewBottom = view.getBottom();
540 int viewLeft = view.getLeft();
541 int viewRight = view.getRight();
543 if (top < viewBottom && viewTop < bottom && left < viewRight && viewLeft < right)
549 final boolean viewIsFullyContained = (top < viewTop) && (viewBottom < bottom) &&
550 (left < viewLeft) && (viewRight < right);
551 if (focusCandidate ==
null)
554 focusCandidate = view;
555 foundFullyContainedFocusable = viewIsFullyContained;
559 final boolean viewIsCloserToVerticalBoundary =
560 (topFocus && viewTop < focusCandidate.getTop()) ||
561 (!topFocus && viewBottom > focusCandidate.getBottom());
562 final boolean viewIsCloserToHorizontalBoundary =
563 (leftFocus && viewLeft < focusCandidate.getLeft()) ||
564 (!leftFocus && viewRight > focusCandidate.getRight());
565 if (foundFullyContainedFocusable)
567 if (viewIsFullyContained && viewIsCloserToVerticalBoundary &&
568 viewIsCloserToHorizontalBoundary)
575 focusCandidate = view;
580 if (viewIsFullyContained)
583 focusCandidate = view;
584 foundFullyContainedFocusable =
true;
586 else if (viewIsCloserToVerticalBoundary && viewIsCloserToHorizontalBoundary)
592 focusCandidate = view;
598 return focusCandidate;
617 boolean down = direction == View.FOCUS_DOWN;
618 int height = getHeight();
620 mTempRect.bottom = height;
623 int count = getChildCount();
626 View view = getChildAt(count - 1);
627 mTempRect.bottom = view.getBottom();
628 mTempRect.top = mTempRect.bottom - height;
631 return scrollAndFocus(direction, mTempRect.top, mTempRect.bottom, 0, 0, 0);
635 boolean right = direction == View.FOCUS_DOWN;
636 int width = getWidth();
638 mTempRect.right = width;
641 int count = getChildCount();
644 View view = getChildAt(count - 1);
645 mTempRect.right = view.getBottom();
646 mTempRect.left = mTempRect.right - width;
649 return scrollAndFocus(0, 0, 0, direction, mTempRect.top, mTempRect.bottom);
666 private boolean scrollAndFocus(
int directionY,
int top,
int bottom,
int directionX,
int left,
669 boolean handled =
true;
670 int height = getHeight();
671 int containerTop = getScrollY();
672 int containerBottom = containerTop + height;
673 boolean up = directionY == View.FOCUS_UP;
674 int width = getWidth();
675 int containerLeft = getScrollX();
676 int containerRight = containerLeft + width;
677 boolean leftwards = directionX == View.FOCUS_UP;
678 View newFocused = findFocusableViewInBounds(up, top, bottom, leftwards, left, right);
679 if (newFocused ==
null)
683 if ((top >= containerTop && bottom <= containerBottom) ||
684 (left >= containerLeft && right <= containerRight))
690 int deltaY = up ? (top - containerTop) : (bottom - containerBottom);
691 int deltaX = leftwards ? (left - containerLeft) : (right - containerRight);
692 doScroll(deltaX, deltaY);
694 if (newFocused != findFocus() && newFocused.requestFocus(directionY))
696 mTwoDScrollViewMovedFocus =
true;
697 mTwoDScrollViewMovedFocus =
false;
711 View currentFocused = findFocus();
712 if (currentFocused ==
this)
713 currentFocused =
null;
714 View nextFocused = FocusFinder.getInstance().findNextFocus(
this, currentFocused, direction);
720 if (nextFocused !=
null)
722 nextFocused.getDrawingRect(mTempRect);
723 offsetDescendantRectToMyCoords(nextFocused, mTempRect);
725 doScroll(0, scrollDelta);
726 nextFocused.requestFocus(direction);
731 int scrollDelta = maxJump;
732 if (direction == View.FOCUS_UP && getScrollY() < scrollDelta)
734 scrollDelta = getScrollY();
736 else if (direction == View.FOCUS_DOWN)
738 if (getChildCount() > 0)
740 int daBottom = getChildAt(0).getBottom();
741 int screenBottom = getScrollY() + getHeight();
742 if (daBottom - screenBottom < maxJump)
744 scrollDelta = daBottom - screenBottom;
748 if (scrollDelta == 0)
752 doScroll(0, direction == View.FOCUS_DOWN ? scrollDelta : -scrollDelta);
757 if (nextFocused !=
null)
759 nextFocused.getDrawingRect(mTempRect);
760 offsetDescendantRectToMyCoords(nextFocused, mTempRect);
762 doScroll(scrollDelta, 0);
763 nextFocused.requestFocus(direction);
768 int scrollDelta = maxJump;
769 if (direction == View.FOCUS_UP && getScrollY() < scrollDelta)
771 scrollDelta = getScrollY();
773 else if (direction == View.FOCUS_DOWN)
775 if (getChildCount() > 0)
777 int daBottom = getChildAt(0).getBottom();
778 int screenBottom = getScrollY() + getHeight();
779 if (daBottom - screenBottom < maxJump)
781 scrollDelta = daBottom - screenBottom;
785 if (scrollDelta == 0)
789 doScroll(direction == View.FOCUS_DOWN ? scrollDelta : -scrollDelta, 0);
800 private void doScroll(
int deltaX,
int deltaY)
802 if (deltaX != 0 || deltaY != 0)
816 long duration = AnimationUtils.currentAnimationTimeMillis() - mLastScroll;
817 if (duration > ANIMATED_SCROLL_GAP)
819 mScroller.startScroll(getScrollX(), getScrollY(), dx, dy);
820 awakenScrollBars(mScroller.getDuration());
825 if (!mScroller.isFinished())
827 mScroller.abortAnimation();
831 mLastScroll = AnimationUtils.currentAnimationTimeMillis();
851 int count = getChildCount();
852 return count == 0 ? getHeight() : (getChildAt(0)).getBottom();
855 @Override
protected int computeHorizontalScrollRange()
857 int count = getChildCount();
858 return count == 0 ? getWidth() : (getChildAt(0)).getRight();
862 protected void measureChild(View child,
int parentWidthMeasureSpec,
int parentHeightMeasureSpec)
864 ViewGroup.LayoutParams lp = child.getLayoutParams();
865 int childWidthMeasureSpec;
866 int childHeightMeasureSpec;
868 childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
869 getPaddingLeft() + getPaddingRight(), lp.width);
870 childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
872 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
876 protected void measureChildWithMargins(View child,
int parentWidthMeasureSpec,
int widthUsed,
877 int parentHeightMeasureSpec,
int heightUsed)
879 final MarginLayoutParams lp = (MarginLayoutParams)child.getLayoutParams();
880 final int childWidthMeasureSpec =
881 MeasureSpec.makeMeasureSpec(lp.leftMargin + lp.rightMargin, MeasureSpec.UNSPECIFIED);
882 final int childHeightMeasureSpec =
883 MeasureSpec.makeMeasureSpec(lp.topMargin + lp.bottomMargin, MeasureSpec.UNSPECIFIED);
885 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
888 @Override
public void computeScroll()
890 if (mScroller.computeScrollOffset())
908 int oldX = getScrollX();
909 int oldY = getScrollY();
910 int x = mScroller.getCurrX();
911 int y = mScroller.getCurrY();
912 if (getChildCount() > 0)
914 View child = getChildAt(0);
916 clamp(x, getWidth() - getPaddingRight() - getPaddingLeft(), child.getWidth()),
917 clamp(y, getHeight() - getPaddingBottom() - getPaddingTop(),
924 if (oldX != getScrollX() || oldY != getScrollY())
926 onScrollChanged(getScrollX(), getScrollY(), oldX, oldY);
939 private void scrollToChild(View child)
941 child.getDrawingRect(mTempRect);
943 offsetDescendantRectToMyCoords(child, mTempRect);
945 if (scrollDelta != 0)
947 scrollBy(0, scrollDelta);
959 private boolean scrollToChildRect(Rect rect,
boolean immediate)
962 final boolean scroll = delta != 0;
987 if (getChildCount() == 0)
989 int height = getHeight();
990 int screenTop = getScrollY();
991 int screenBottom = screenTop + height;
992 int fadingEdge = getVerticalFadingEdgeLength();
996 screenTop += fadingEdge;
1000 if (rect.bottom < getChildAt(0).getHeight())
1002 screenBottom -= fadingEdge;
1004 int scrollYDelta = 0;
1005 if (rect.bottom > screenBottom && rect.top > screenTop)
1010 if (rect.height() > height)
1013 scrollYDelta += (rect.top - screenTop);
1018 scrollYDelta += (rect.bottom - screenBottom);
1022 int bottom = getChildAt(0).getBottom();
1023 int distanceToBottom = bottom - screenBottom;
1024 scrollYDelta = Math.min(scrollYDelta, distanceToBottom);
1026 else if (rect.top < screenTop && rect.bottom < screenBottom)
1032 if (rect.height() > height)
1035 scrollYDelta -= (screenBottom - rect.bottom);
1040 scrollYDelta -= (screenTop - rect.top);
1044 scrollYDelta = Math.max(scrollYDelta, -getScrollY());
1046 return scrollYDelta;
1049 @Override
public void requestChildFocus(View child, View focused)
1051 if (!mTwoDScrollViewMovedFocus)
1053 if (!mIsLayoutDirty)
1055 scrollToChild(focused);
1060 mChildToScrollTo = focused;
1063 super.requestChildFocus(child, focused);
1078 if (direction == View.FOCUS_FORWARD)
1080 direction = View.FOCUS_DOWN;
1082 else if (direction == View.FOCUS_BACKWARD)
1084 direction = View.FOCUS_UP;
1087 final View nextFocus = previouslyFocusedRect ==
null
1088 ? FocusFinder.getInstance().findNextFocus(
this,
null, direction)
1089 : FocusFinder.getInstance().findNextFocusFromRect(
1090 this, previouslyFocusedRect, direction);
1092 if (nextFocus ==
null)
1097 return nextFocus.requestFocus(direction, previouslyFocusedRect);
1101 public boolean requestChildRectangleOnScreen(View child, Rect rectangle,
boolean immediate)
1104 rectangle.offset(child.getLeft() - child.getScrollX(), child.getTop() - child.getScrollY());
1105 return scrollToChildRect(rectangle, immediate);
1108 @Override
public void requestLayout()
1110 mIsLayoutDirty =
true;
1111 super.requestLayout();
1114 private int lastCenterContentH = -1;
1115 private int lastCenterTop = 0;
1117 @Override
protected void onLayout(
boolean changed,
int l,
int t,
int r,
int b)
1119 super.onLayout(changed, l, t, r, b);
1120 mIsLayoutDirty =
false;
1122 if (mChildToScrollTo !=
null && isViewDescendantOf(mChildToScrollTo,
this))
1124 scrollToChild(mChildToScrollTo);
1126 mChildToScrollTo =
null;
1130 if (getChildCount() > 0)
1132 View child = getChildAt(0);
1133 SessionView sv = findViewById(R.id.sessionView);
1134 int ptw = sv !=
null ? sv.getTouchPointerPaddingWidth() : 0;
1135 int pth = sv !=
null ? sv.getTouchPointerPaddingHeight() : 0;
1136 int contentW = child.getMeasuredWidth() - ptw;
1137 int contentH = child.getMeasuredHeight() - pth;
1138 int usableW = getWidth() - getPaddingLeft() - getPaddingRight();
1139 int usableH = getHeight() - getPaddingTop() - getPaddingBottom();
1140 int left = getPaddingLeft() + Math.max(0, (usableW - contentW) / 2);
1142 if (contentH == lastCenterContentH)
1145 top = lastCenterTop;
1149 top = getPaddingTop() + Math.max(0, (usableH - contentH) / 2);
1150 lastCenterContentH = contentH;
1151 lastCenterTop = top;
1153 child.layout(left, top, left + child.getMeasuredWidth(),
1154 top + child.getMeasuredHeight());
1158 scrollTo(getScrollX(), getScrollY());
1161 @Override
protected void onSizeChanged(
int w,
int h,
int oldw,
int oldh)
1163 super.onSizeChanged(w, h, oldw, oldh);
1165 View currentFocused = findFocus();
1166 if (
null == currentFocused ||
this == currentFocused)
1172 currentFocused.getDrawingRect(mTempRect);
1173 offsetDescendantRectToMyCoords(currentFocused, mTempRect);
1176 doScroll(scrollDeltaX, scrollDeltaY);
1182 private boolean isViewDescendantOf(View child, View parent)
1184 if (child == parent)
1189 final ViewParent theParent = child.getParent();
1190 return (theParent instanceof ViewGroup) && isViewDescendantOf((View)theParent, parent);
1200 public void fling(
int velocityX,
int velocityY)
1202 if (getChildCount() > 0)
1204 int height = getHeight() - getPaddingBottom() - getPaddingTop();
1205 int bottom = getChildAt(0).getHeight();
1206 int width = getWidth() - getPaddingRight() - getPaddingLeft();
1207 int right = getChildAt(0).getWidth();
1209 mScroller.fling(getScrollX(), getScrollY(), velocityX, velocityY, 0, right - width, 0,
1212 final boolean movingDown = velocityY > 0;
1213 final boolean movingRight = velocityX > 0;
1215 View newFocused = findFocusableViewInMyBounds(
1216 movingRight, mScroller.getFinalX(), movingDown, mScroller.getFinalY(), findFocus());
1217 if (newFocused ==
null)
1222 if (newFocused != findFocus() &&
1223 newFocused.requestFocus(movingDown ? View.FOCUS_DOWN : View.FOCUS_UP))
1225 mTwoDScrollViewMovedFocus =
true;
1226 mTwoDScrollViewMovedFocus =
false;
1229 awakenScrollBars(mScroller.getDuration());
1242 if (getChildCount() > 0)
1244 View child = getChildAt(0);
1245 x = clamp(x, getWidth() - getPaddingRight() - getPaddingLeft(), child.getWidth());
1246 y = clamp(y, getHeight() - getPaddingBottom() - getPaddingTop(), child.getHeight());
1247 if (x != getScrollX() || y != getScrollY())
1249 super.scrollTo(x, y);
1254 private int clamp(
int n,
int my,
int child)
1256 if (my >= child || n < 0)
1275 if ((my + n) > child)
1287 public void setScrollViewListener(ScrollView2DListener scrollViewListener)
1289 this.scrollView2DListener = scrollViewListener;
1292 @Override
protected void onScrollChanged(
int x,
int y,
int oldx,
int oldy)
1294 super.onScrollChanged(x, y, oldx, oldy);
1295 if (scrollView2DListener !=
null)
1297 scrollView2DListener.onScrollChanged(
this, x, y, oldx, oldy);
1303 void onScrollChanged(ScrollView2D scrollView,
int x,
int y,
int oldx,
int oldy);