on*methods are invoked on siginficant events and several
145 | * accessor methods are expected to provide the ViewDragHelper with more information
146 | * about the state of the parent view upon request. The callback also makes decisions
147 | * governing the range and draggability of child views.
148 | */
149 | public static abstract class Callback {
150 | /**
151 | * Called when the drag state changes. See the STATE_* constants
152 | * for more information.
153 | *
154 | * @param state The new drag state
155 | *
156 | * @see #STATE_IDLE
157 | * @see #STATE_DRAGGING
158 | * @see #STATE_SETTLING
159 | */
160 | public void onViewDragStateChanged(int state) {}
161 |
162 | /**
163 | * Called when the captured view's position changes as the result of a drag or settle.
164 | *
165 | * @param changedView View whose position changed
166 | * @param left New X coordinate of the left edge of the view
167 | * @param top New Y coordinate of the top edge of the view
168 | * @param dx Change in X position from the last call
169 | * @param dy Change in Y position from the last call
170 | */
171 | public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {}
172 |
173 | /**
174 | * Called when a child view is captured for dragging or settling. The ID of the pointer
175 | * currently dragging the captured view is supplied. If activePointerId is
176 | * identified as {@link #INVALID_POINTER} the capture is programmatic instead of
177 | * pointer-initiated.
178 | *
179 | * @param capturedChild Child view that was captured
180 | * @param activePointerId Pointer id tracking the child capture
181 | */
182 | public void onViewCaptured(View capturedChild, int activePointerId) {}
183 |
184 | /**
185 | * Called when the child view is no longer being actively dragged.
186 | * The fling velocity is also supplied, if relevant. The velocity values may
187 | * be clamped to system minimums or maximums.
188 | *
189 | * Calling code may decide to fling or otherwise release the view to let it
190 | * settle into place. It should do so using {@link #settleCapturedViewAt(int, int)}
191 | * or {@link #flingCapturedView(int, int, int, int)}. If the Callback invokes
192 | * one of these methods, the ViewDragHelper will enter {@link #STATE_SETTLING}
193 | * and the view capture will not fully end until it comes to a complete stop.
194 | * If neither of these methods is invoked before onViewReleased returns,
195 | * the view will stop in place and the ViewDragHelper will return to
196 | * {@link #STATE_IDLE}.
index
248 | */
249 | public int getOrderedChildIndex(int index) {
250 | return index;
251 | }
252 |
253 | /**
254 | * Return the magnitude of a draggable child view's horizontal range of motion in pixels.
255 | * This method should return 0 for views that cannot move horizontally.
256 | *
257 | * @param child Child view to check
258 | * @return range of horizontal motion in pixels
259 | */
260 | public int getViewHorizontalDragRange(View child) {
261 | return 0;
262 | }
263 |
264 | /**
265 | * Return the magnitude of a draggable child view's vertical range of motion in pixels.
266 | * This method should return 0 for views that cannot move vertically.
267 | *
268 | * @param child Child view to check
269 | * @return range of vertical motion in pixels
270 | */
271 | public int getViewVerticalDragRange(View child) {
272 | return 0;
273 | }
274 |
275 | /**
276 | * Called when the user's input indicates that they want to capture the given child view
277 | * with the pointer indicated by pointerId. The callback should return true if the user
278 | * is permitted to drag the given view with the indicated pointer.
279 | *
280 | * ViewDragHelper may call this method multiple times for the same view even if 281 | * the view is already captured; this indicates that a new pointer is trying to take 282 | * control of the view.
283 | * 284 | *If this method returns true, a call to {@link #onViewCaptured(View, int)} 285 | * will follow if the capture is successful.
286 | * 287 | * @param child Child the user is attempting to capture 288 | * @param pointerId ID of the pointer attempting the capture 289 | * @return true if capture should be allowed, false otherwise 290 | */ 291 | public abstract boolean tryCaptureView(View child, int pointerId); 292 | 293 | /** 294 | * Restrict the motion of the dragged child view along the horizontal axis. 295 | * The default implementation does not allow horizontal motion; the extending 296 | * class must override this method and provide the desired clamping. 297 | * 298 | * 299 | * @param child Child view being dragged 300 | * @param left Attempted motion along the X axis 301 | * @param dx Proposed change in position for left 302 | * @return The new clamped position for left 303 | */ 304 | public int clampViewPositionHorizontal(View child, int left, int dx) { 305 | return 0; 306 | } 307 | 308 | /** 309 | * Restrict the motion of the dragged child view along the vertical axis. 310 | * The default implementation does not allow vertical motion; the extending 311 | * class must override this method and provide the desired clamping. 312 | * 313 | * 314 | * @param child Child view being dragged 315 | * @param top Attempted motion along the Y axis 316 | * @param dy Proposed change in position for top 317 | * @return The new clamped position for top 318 | */ 319 | public int clampViewPositionVertical(View child, int top, int dy) { 320 | return 0; 321 | } 322 | } 323 | 324 | /** 325 | * Interpolator defining the animation curve for mScroller 326 | */ 327 | private static final Interpolator sInterpolator = new Interpolator() { 328 | public float getInterpolation(float t) { 329 | t -= 1.0f; 330 | return t * t * t * t * t + 1.0f; 331 | } 332 | }; 333 | 334 | private final Runnable mSetIdleRunnable = new Runnable() { 335 | public void run() { 336 | setDragState(STATE_IDLE); 337 | } 338 | }; 339 | 340 | /** 341 | * Factory method to create a new ViewDragHelper. 342 | * 343 | * @param forParent Parent view to monitor 344 | * @param cb Callback to provide information and receive events 345 | * @return a new ViewDragHelper instance 346 | */ 347 | public static ViewDragHelper create(ViewGroup forParent, Callback cb) { 348 | return new ViewDragHelper(forParent.getContext(), forParent, cb); 349 | } 350 | 351 | /** 352 | * Factory method to create a new ViewDragHelper. 353 | * 354 | * @param forParent Parent view to monitor 355 | * @param sensitivity Multiplier for how sensitive the helper should be about detecting 356 | * the start of a drag. Larger values are more sensitive. 1.0f is normal. 357 | * @param cb Callback to provide information and receive events 358 | * @return a new ViewDragHelper instance 359 | */ 360 | public static ViewDragHelper create(ViewGroup forParent, float sensitivity, Callback cb) { 361 | final ViewDragHelper helper = create(forParent, cb); 362 | helper.mTouchSlop = (int) (helper.mTouchSlop * (1 / sensitivity)); 363 | return helper; 364 | } 365 | 366 | /** 367 | * Apps should use ViewDragHelper.create() to get a new instance. 368 | * This will allow VDH to use internal compatibility implementations for different 369 | * platform versions. 370 | * 371 | * @param context Context to initialize config-dependent params from 372 | * @param forParent Parent view to monitor 373 | */ 374 | private ViewDragHelper(Context context, ViewGroup forParent, Callback cb) { 375 | if (forParent == null) { 376 | throw new IllegalArgumentException("Parent view may not be null"); 377 | } 378 | if (cb == null) { 379 | throw new IllegalArgumentException("Callback may not be null"); 380 | } 381 | 382 | mParentView = forParent; 383 | mCallback = cb; 384 | 385 | final ViewConfiguration vc = ViewConfiguration.get(context); 386 | final float density = context.getResources().getDisplayMetrics().density; 387 | mEdgeSize = (int) (EDGE_SIZE * density + 0.5f); 388 | 389 | mTouchSlop = vc.getScaledTouchSlop(); 390 | mMaxVelocity = vc.getScaledMaximumFlingVelocity(); 391 | mMinVelocity = vc.getScaledMinimumFlingVelocity(); 392 | mScroller = ScrollerCompat.create(context, sInterpolator); 393 | } 394 | 395 | /** 396 | * Set the minimum velocity that will be detected as having a magnitude greater than zero 397 | * in pixels per second. Callback methods accepting a velocity will be clamped appropriately. 398 | * 399 | * @param minVel Minimum velocity to detect 400 | */ 401 | public void setMinVelocity(float minVel) { 402 | mMinVelocity = minVel; 403 | } 404 | 405 | /** 406 | * Return the currently configured minimum velocity. Any flings with a magnitude less 407 | * than this value in pixels per second. Callback methods accepting a velocity will receive 408 | * zero as a velocity value if the real detected velocity was below this threshold. 409 | * 410 | * @return the minimum velocity that will be detected 411 | */ 412 | public float getMinVelocity() { 413 | return mMinVelocity; 414 | } 415 | 416 | /** 417 | * Retrieve the current drag state of this helper. This will return one of 418 | * {@link #STATE_IDLE}, {@link #STATE_DRAGGING} or {@link #STATE_SETTLING}. 419 | * @return The current drag state 420 | */ 421 | public int getViewDragState() { 422 | return mDragState; 423 | } 424 | 425 | /** 426 | * Enable edge tracking for the selected edges of the parent view. 427 | * The callback's {@link neal.adapterview.core.ViewDragHelper.Callback#onEdgeTouched(int, int)} and 428 | * {@link neal.adapterview.core.ViewDragHelper.Callback#onEdgeDragStarted(int, int)} methods will only be invoked 429 | * for edges for which edge tracking has been enabled. 430 | * 431 | * @param edgeFlags Combination of edge flags describing the edges to watch 432 | * @see #EDGE_LEFT 433 | * @see #EDGE_TOP 434 | * @see #EDGE_RIGHT 435 | * @see #EDGE_BOTTOM 436 | */ 437 | public void setEdgeTrackingEnabled(int edgeFlags) { 438 | mTrackingEdges = edgeFlags; 439 | } 440 | 441 | /** 442 | * Return the size of an edge. This is the range in pixels along the edges of this view 443 | * that will actively detect edge touches or drags if edge tracking is enabled. 444 | * 445 | * @return The size of an edge in pixels 446 | * @see #setEdgeTrackingEnabled(int) 447 | */ 448 | public int getEdgeSize() { 449 | return mEdgeSize; 450 | } 451 | 452 | /** 453 | * Capture a specific child view for dragging within the parent. The callback will be notified 454 | * but {@link neal.adapterview.core.ViewDragHelper.Callback#tryCaptureView(View, int)} will not be asked permission to 455 | * capture this view. 456 | * 457 | * @param childView Child view to capture 458 | * @param activePointerId ID of the pointer that is dragging the captured child view 459 | */ 460 | public void captureChildView(View childView, int activePointerId) { 461 | if (childView.getParent() != mParentView) { 462 | throw new IllegalArgumentException("captureChildView: parameter must be a descendant " + 463 | "of the ViewDragHelper's tracked parent view (" + mParentView + ")"); 464 | } 465 | 466 | mCapturedView = childView; 467 | mActivePointerId = activePointerId; 468 | mCallback.onViewCaptured(childView, activePointerId); 469 | setDragState(STATE_DRAGGING); 470 | } 471 | 472 | /** 473 | * @return The currently captured view, or null if no view has been captured. 474 | */ 475 | public View getCapturedView() { 476 | return mCapturedView; 477 | } 478 | 479 | /** 480 | * @return The ID of the pointer currently dragging the captured view, 481 | * or {@link #INVALID_POINTER}. 482 | */ 483 | public int getActivePointerId() { 484 | return mActivePointerId; 485 | } 486 | 487 | /** 488 | * @return The minimum distance in pixels that the user must travel to initiate a drag 489 | */ 490 | public int getTouchSlop() { 491 | return mTouchSlop; 492 | } 493 | 494 | /** 495 | * The result of a call to this method is equivalent to 496 | * {@link #processTouchEvent(MotionEvent)} receiving an ACTION_CANCEL event. 497 | */ 498 | public void cancel() { 499 | mActivePointerId = INVALID_POINTER; 500 | clearMotionHistory(); 501 | 502 | if (mVelocityTracker != null) { 503 | mVelocityTracker.recycle(); 504 | mVelocityTracker = null; 505 | } 506 | } 507 | 508 | /** 509 | * {@link #cancel()}, but also abort all motion in progress and snap to the end of any 510 | * animation. 511 | */ 512 | public void abort() { 513 | cancel(); 514 | if (mDragState == STATE_SETTLING) { 515 | final int oldX = mScroller.getCurrX(); 516 | final int oldY = mScroller.getCurrY(); 517 | mScroller.abortAnimation(); 518 | final int newX = mScroller.getCurrX(); 519 | final int newY = mScroller.getCurrY(); 520 | mCallback.onViewPositionChanged(mCapturedView, newX, newY, newX - oldX, newY - oldY); 521 | } 522 | setDragState(STATE_IDLE); 523 | } 524 | 525 | /** 526 | * Animate the viewchild to the given (left, top) position.
527 | * If this method returns true, the caller should invoke {@link #continueSettling(boolean)}
528 | * on each subsequent frame to continue the motion until it returns false. If this method
529 | * returns false there is no further work to do to complete the movement.
530 | *
531 | * This operation does not count as a capture event, though {@link #getCapturedView()} 532 | * will still report the sliding view while the slide is in progress.
533 | * 534 | * @param child Child view to capture and animate 535 | * @param finalLeft Final left position of child 536 | * @param finalTop Final top position of child 537 | * @return true if animation should continue through {@link #continueSettling(boolean)} calls 538 | */ 539 | public boolean smoothSlideViewTo(View child, int finalLeft, int finalTop) { 540 | mCapturedView = child; 541 | mActivePointerId = INVALID_POINTER; 542 | 543 | return forceSettleCapturedViewAt(finalLeft, finalTop, 0, 0); 544 | } 545 | 546 | /** 547 | * Settle the captured view at the given (left, top) position. 548 | * The appropriate velocity from prior motion will be taken into account. 549 | * If this method returns true, the caller should invoke {@link #continueSettling(boolean)} 550 | * on each subsequent frame to continue the motion until it returns false. If this method 551 | * returns false there is no further work to do to complete the movement. 552 | * 553 | * @param finalLeft Settled left edge position for the captured view 554 | * @param finalTop Settled top edge position for the captured view 555 | * @return true if animation should continue through {@link #continueSettling(boolean)} calls 556 | */ 557 | public boolean settleCapturedViewAt(int finalLeft, int finalTop) { 558 | if (!mReleaseInProgress) { 559 | throw new IllegalStateException("Cannot settleCapturedViewAt outside of a call to " + 560 | "Callback#onViewReleased"); 561 | } 562 | 563 | return forceSettleCapturedViewAt(finalLeft, finalTop, 564 | (int) VelocityTrackerCompat.getXVelocity(mVelocityTracker, mActivePointerId), 565 | (int) VelocityTrackerCompat.getYVelocity(mVelocityTracker, mActivePointerId)); 566 | } 567 | 568 | /** 569 | * Settle the captured view at the given (left, top) position. 570 | * 571 | * @param finalLeft Target left position for the captured view 572 | * @param finalTop Target top position for the captured view 573 | * @param xvel Horizontal velocity 574 | * @param yvel Vertical velocity 575 | * @return true if animation should continue through {@link #continueSettling(boolean)} calls 576 | */ 577 | private boolean forceSettleCapturedViewAt(int finalLeft, int finalTop, int xvel, int yvel) { 578 | final int startLeft = mCapturedView.getLeft(); 579 | final int startTop = mCapturedView.getTop(); 580 | final int dx = finalLeft - startLeft; 581 | final int dy = finalTop - startTop; 582 | 583 | if (dx == 0 && dy == 0) { 584 | // Nothing to do. Send callbacks, be done. 585 | mScroller.abortAnimation(); 586 | setDragState(STATE_IDLE); 587 | return false; 588 | } 589 | 590 | //final int duration = computeSettleDuration(mCapturedView, dx, dy, xvel, yvel); 591 | //TODO 修改时间 592 | final int duration=500; 593 | mScroller.startScroll(startLeft, startTop, dx, dy, duration); 594 | 595 | setDragState(STATE_SETTLING); 596 | return true; 597 | } 598 | 599 | private int computeSettleDuration(View child, int dx, int dy, int xvel, int yvel) { 600 | xvel = clampMag(xvel, (int) mMinVelocity, (int) mMaxVelocity); 601 | yvel = clampMag(yvel, (int) mMinVelocity, (int) mMaxVelocity); 602 | final int absDx = Math.abs(dx); 603 | final int absDy = Math.abs(dy); 604 | final int absXVel = Math.abs(xvel); 605 | final int absYVel = Math.abs(yvel); 606 | final int addedVel = absXVel + absYVel; 607 | final int addedDistance = absDx + absDy; 608 | 609 | final float xweight = xvel != 0 ? (float) absXVel / addedVel : 610 | (float) absDx / addedDistance; 611 | final float yweight = yvel != 0 ? (float) absYVel / addedVel : 612 | (float) absDy / addedDistance; 613 | 614 | int xduration = computeAxisDuration(dx, xvel, mCallback.getViewHorizontalDragRange(child)); 615 | int yduration = computeAxisDuration(dy, yvel, mCallback.getViewVerticalDragRange(child)); 616 | 617 | return (int) (xduration * xweight + yduration * yweight); 618 | } 619 | 620 | private int computeAxisDuration(int delta, int velocity, int motionRange) { 621 | if (delta == 0) { 622 | return 0; 623 | } 624 | 625 | final int width = mParentView.getWidth(); 626 | final int halfWidth = width / 2; 627 | final float distanceRatio = Math.min(1f, (float) Math.abs(delta) / width); 628 | final float distance = halfWidth + halfWidth * 629 | distanceInfluenceForSnapDuration(distanceRatio); 630 | 631 | int duration; 632 | velocity = Math.abs(velocity); 633 | if (velocity > 0) { 634 | duration = 4 * Math.round(1000 * Math.abs(distance / velocity)); 635 | } else { 636 | final float range = (float) Math.abs(delta) / motionRange; 637 | duration = (int) ((range + 1) * BASE_SETTLE_DURATION); 638 | } 639 | return Math.min(duration, MAX_SETTLE_DURATION); 640 | } 641 | 642 | /** 643 | * Clamp the magnitude of value for absMin and absMax. 644 | * If the value is below the minimum, it will be clamped to zero. 645 | * If the value is above the maximum, it will be clamped to the maximum. 646 | * 647 | * @param value Value to clamp 648 | * @param absMin Absolute value of the minimum significant value to return 649 | * @param absMax Absolute value of the maximum value to return 650 | * @return The clamped value with the same sign asvalue
651 | */
652 | private int clampMag(int value, int absMin, int absMax) {
653 | final int absValue = Math.abs(value);
654 | if (absValue < absMin) return 0;
655 | if (absValue > absMax) return value > 0 ? absMax : -absMax;
656 | return value;
657 | }
658 |
659 | /**
660 | * Clamp the magnitude of value for absMin and absMax.
661 | * If the value is below the minimum, it will be clamped to zero.
662 | * If the value is above the maximum, it will be clamped to the maximum.
663 | *
664 | * @param value Value to clamp
665 | * @param absMin Absolute value of the minimum significant value to return
666 | * @param absMax Absolute value of the maximum value to return
667 | * @return The clamped value with the same sign as value
668 | */
669 | private float clampMag(float value, float absMin, float absMax) {
670 | final float absValue = Math.abs(value);
671 | if (absValue < absMin) return 0;
672 | if (absValue > absMax) return value > 0 ? absMax : -absMax;
673 | return value;
674 | }
675 |
676 | private float distanceInfluenceForSnapDuration(float f) {
677 | f -= 0.5f; // center the values about 0.
678 | f *= 0.3f * Math.PI / 2.0f;
679 | return (float) Math.sin(f);
680 | }
681 |
682 | /**
683 | * Settle the captured view based on standard free-moving fling behavior.
684 | * The caller should invoke {@link #continueSettling(boolean)} on each subsequent frame
685 | * to continue the motion until it returns false.
686 | *
687 | * @param minLeft Minimum X position for the view's left edge
688 | * @param minTop Minimum Y position for the view's top edge
689 | * @param maxLeft Maximum X position for the view's left edge
690 | * @param maxTop Maximum Y position for the view's top edge
691 | */
692 | public void flingCapturedView(int minLeft, int minTop, int maxLeft, int maxTop) {
693 | if (!mReleaseInProgress) {
694 | throw new IllegalStateException("Cannot flingCapturedView outside of a call to " +
695 | "Callback#onViewReleased");
696 | }
697 |
698 | mScroller.fling(mCapturedView.getLeft(), mCapturedView.getTop(),
699 | (int) VelocityTrackerCompat.getXVelocity(mVelocityTracker, mActivePointerId),
700 | (int) VelocityTrackerCompat.getYVelocity(mVelocityTracker, mActivePointerId),
701 | minLeft, maxLeft, minTop, maxTop);
702 |
703 | setDragState(STATE_SETTLING);
704 | }
705 |
706 | /**
707 | * Move the captured settling view by the appropriate amount for the current time.
708 | * If continueSettling returns true, the caller should call it again
709 | * on the next frame to continue.
710 | *
711 | * @param deferCallbacks true if state callbacks should be deferred via posted message.
712 | * Set this to true if you are calling this method from
713 | * {@link View#computeScroll()} or similar methods
714 | * invoked as part of layout or drawing.
715 | * @return true if settle is still in progress
716 | */
717 | public boolean continueSettling(boolean deferCallbacks) {
718 | // Make sure, there is a captured view
719 | if (mCapturedView == null) {
720 | return false;
721 | }
722 | if (mDragState == STATE_SETTLING) {
723 | boolean keepGoing = mScroller.computeScrollOffset();
724 | final int x = mScroller.getCurrX();
725 | final int y = mScroller.getCurrY();
726 | final int dx = x - mCapturedView.getLeft();
727 | final int dy = y - mCapturedView.getTop();
728 |
729 | if (dx != 0) {
730 | mCapturedView.offsetLeftAndRight(dx);
731 | }
732 | if (dy != 0) {
733 | mCapturedView.offsetTopAndBottom(dy);
734 | }
735 |
736 | if (dx != 0 || dy != 0) {
737 | mCallback.onViewPositionChanged(mCapturedView, x, y, dx, dy);
738 | }
739 |
740 | if (keepGoing && x == mScroller.getFinalX() && y == mScroller.getFinalY()) {
741 | // Close enough. The interpolator/scroller might think we're still moving
742 | // but the user sure doesn't.
743 | mScroller.abortAnimation();
744 | keepGoing = mScroller.isFinished();
745 | }
746 |
747 | if (!keepGoing) {
748 | if (deferCallbacks) {
749 | mParentView.post(mSetIdleRunnable);
750 | } else {
751 | setDragState(STATE_IDLE);
752 | }
753 | }
754 | }
755 |
756 | return mDragState == STATE_SETTLING;
757 | }
758 |
759 | /**
760 | * Like all callback events this must happen on the UI thread, but release
761 | * involves some extra semantics. During a release (mReleaseInProgress)
762 | * is the only time it is valid to call {@link #settleCapturedViewAt(int, int)}
763 | * or {@link #flingCapturedView(int, int, int, int)}.
764 | */
765 | private void dispatchViewReleased(float xvel, float yvel) {
766 | mReleaseInProgress = true;
767 | mCallback.onViewReleased(mCapturedView, xvel, yvel);
768 | mReleaseInProgress = false;
769 |
770 | if (mDragState == STATE_DRAGGING) {
771 | // onViewReleased didn't call a method that would have changed this. Go idle.
772 | setDragState(STATE_IDLE);
773 | }
774 | }
775 |
776 | private void clearMotionHistory() {
777 | if (mInitialMotionX == null) {
778 | return;
779 | }
780 | Arrays.fill(mInitialMotionX, 0);
781 | Arrays.fill(mInitialMotionY, 0);
782 | Arrays.fill(mLastMotionX, 0);
783 | Arrays.fill(mLastMotionY, 0);
784 | Arrays.fill(mInitialEdgesTouched, 0);
785 | Arrays.fill(mEdgeDragsInProgress, 0);
786 | Arrays.fill(mEdgeDragsLocked, 0);
787 | mPointersDown = 0;
788 | }
789 |
790 | private void clearMotionHistory(int pointerId) {
791 | if (mInitialMotionX == null) {
792 | return;
793 | }
794 | mInitialMotionX[pointerId] = 0;
795 | mInitialMotionY[pointerId] = 0;
796 | mLastMotionX[pointerId] = 0;
797 | mLastMotionY[pointerId] = 0;
798 | mInitialEdgesTouched[pointerId] = 0;
799 | mEdgeDragsInProgress[pointerId] = 0;
800 | mEdgeDragsLocked[pointerId] = 0;
801 | mPointersDown &= ~(1 << pointerId);
802 | }
803 |
804 | private void ensureMotionHistorySizeForId(int pointerId) {
805 | if (mInitialMotionX == null || mInitialMotionX.length <= pointerId) {
806 | float[] imx = new float[pointerId + 1];
807 | float[] imy = new float[pointerId + 1];
808 | float[] lmx = new float[pointerId + 1];
809 | float[] lmy = new float[pointerId + 1];
810 | int[] iit = new int[pointerId + 1];
811 | int[] edip = new int[pointerId + 1];
812 | int[] edl = new int[pointerId + 1];
813 |
814 | if (mInitialMotionX != null) {
815 | System.arraycopy(mInitialMotionX, 0, imx, 0, mInitialMotionX.length);
816 | System.arraycopy(mInitialMotionY, 0, imy, 0, mInitialMotionY.length);
817 | System.arraycopy(mLastMotionX, 0, lmx, 0, mLastMotionX.length);
818 | System.arraycopy(mLastMotionY, 0, lmy, 0, mLastMotionY.length);
819 | System.arraycopy(mInitialEdgesTouched, 0, iit, 0, mInitialEdgesTouched.length);
820 | System.arraycopy(mEdgeDragsInProgress, 0, edip, 0, mEdgeDragsInProgress.length);
821 | System.arraycopy(mEdgeDragsLocked, 0, edl, 0, mEdgeDragsLocked.length);
822 | }
823 |
824 | mInitialMotionX = imx;
825 | mInitialMotionY = imy;
826 | mLastMotionX = lmx;
827 | mLastMotionY = lmy;
828 | mInitialEdgesTouched = iit;
829 | mEdgeDragsInProgress = edip;
830 | mEdgeDragsLocked = edl;
831 | }
832 | }
833 |
834 | private void saveInitialMotion(float x, float y, int pointerId) {
835 | ensureMotionHistorySizeForId(pointerId);
836 | mInitialMotionX[pointerId] = mLastMotionX[pointerId] = x;
837 | mInitialMotionY[pointerId] = mLastMotionY[pointerId] = y;
838 | mInitialEdgesTouched[pointerId] = getEdgesTouched((int) x, (int) y);
839 | mPointersDown |= 1 << pointerId;
840 | }
841 |
842 | private void saveLastMotion(MotionEvent ev) {
843 | final int pointerCount = MotionEventCompat.getPointerCount(ev);
844 | for (int i = 0; i < pointerCount; i++) {
845 | final int pointerId = MotionEventCompat.getPointerId(ev, i);
846 | final float x = MotionEventCompat.getX(ev, i);
847 | final float y = MotionEventCompat.getY(ev, i);
848 | if (mLastMotionX != null && mLastMotionY != null) {
849 | mLastMotionX[pointerId] = x;
850 | mLastMotionY[pointerId] = y;
851 | }
852 | }
853 | }
854 |
855 | /**
856 | * Check if the given pointer ID represents a pointer that is currently down (to the best
857 | * of the ViewDragHelper's knowledge).
858 | *
859 | * The state used to report this information is populated by the methods 860 | * {@link #shouldInterceptTouchEvent(MotionEvent)} or 861 | * {@link #processTouchEvent(MotionEvent)}. If one of these methods has not 862 | * been called for all relevant MotionEvents to track, the information reported 863 | * by this method may be stale or incorrect.
864 | * 865 | * @param pointerId pointer ID to check; corresponds to IDs provided by MotionEvent 866 | * @return true if the pointer with the given ID is still down 867 | */ 868 | public boolean isPointerDown(int pointerId) { 869 | return (mPointersDown & 1 << pointerId) != 0; 870 | } 871 | 872 | void setDragState(int state) { 873 | if (mDragState != state) { 874 | mDragState = state; 875 | mCallback.onViewDragStateChanged(state); 876 | if (state == STATE_IDLE) { 877 | mCapturedView = null; 878 | } 879 | } 880 | } 881 | 882 | /** 883 | * Attempt to capture the view with the given pointer ID. The callback will be involved. 884 | * This will put us into the "dragging" state. If we've already captured this view with 885 | * this pointer this method will immediately return true without consulting the callback. 886 | * 887 | * @param toCapture View to capture 888 | * @param pointerId Pointer to capture with 889 | * @return true if capture was successful 890 | */ 891 | boolean tryCaptureViewForDrag(View toCapture, int pointerId) { 892 | if (toCapture == mCapturedView && mActivePointerId == pointerId) { 893 | // Already done! 894 | return true; 895 | } 896 | if (toCapture != null && mCallback.tryCaptureView(toCapture, pointerId)) { 897 | mActivePointerId = pointerId; 898 | captureChildView(toCapture, pointerId); 899 | return true; 900 | } 901 | return false; 902 | } 903 | 904 | /** 905 | * Tests scrollability within child views of v given a delta of dx. 906 | * 907 | * @param v View to test for horizontal scrollability 908 | * @param checkV Whether the view v passed should itself be checked for scrollability (true), 909 | * or just its children (false). 910 | * @param dx Delta scrolled in pixels along the X axis 911 | * @param dy Delta scrolled in pixels along the Y axis 912 | * @param x X coordinate of the active touch point 913 | * @param y Y coordinate of the active touch point 914 | * @return true if child views of v can be scrolled by delta of dx. 915 | */ 916 | protected boolean canScroll(View v, boolean checkV, int dx, int dy, int x, int y) { 917 | if (v instanceof ViewGroup) { 918 | final ViewGroup group = (ViewGroup) v; 919 | final int scrollX = v.getScrollX(); 920 | final int scrollY = v.getScrollY(); 921 | final int count = group.getChildCount(); 922 | // Count backwards - let topmost views consume scroll distance first. 923 | for (int i = count - 1; i >= 0; i--) { 924 | // TODO: Add versioned support here for transformed views. 925 | // This will not work for transformed views in Honeycomb+ 926 | final View child = group.getChildAt(i); 927 | if (x + scrollX >= child.getLeft() && x + scrollX < child.getRight() && 928 | y + scrollY >= child.getTop() && y + scrollY < child.getBottom() && 929 | canScroll(child, true, dx, dy, x + scrollX - child.getLeft(), 930 | y + scrollY - child.getTop())) { 931 | return true; 932 | } 933 | } 934 | } 935 | 936 | return checkV && (ViewCompat.canScrollHorizontally(v, -dx) || 937 | ViewCompat.canScrollVertically(v, -dy)); 938 | } 939 | 940 | /** 941 | * Check if this event as provided to the parent view's onInterceptTouchEvent should 942 | * cause the parent to intercept the touch event stream. 943 | * 944 | * @param ev MotionEvent provided to onInterceptTouchEvent 945 | * @return true if the parent view should return true from onInterceptTouchEvent 946 | */ 947 | public boolean shouldInterceptTouchEvent(MotionEvent ev) { 948 | final int action = MotionEventCompat.getActionMasked(ev); 949 | final int actionIndex = MotionEventCompat.getActionIndex(ev); 950 | 951 | if (action == MotionEvent.ACTION_DOWN) { 952 | // Reset things for a new event stream, just in case we didn't get 953 | // the whole previous stream. 954 | cancel(); 955 | } 956 | 957 | if (mVelocityTracker == null) { 958 | mVelocityTracker = VelocityTracker.obtain(); 959 | } 960 | mVelocityTracker.addMovement(ev); 961 | 962 | switch (action) { 963 | case MotionEvent.ACTION_DOWN: { 964 | final float x = ev.getX(); 965 | final float y = ev.getY(); 966 | final int pointerId = MotionEventCompat.getPointerId(ev, 0); 967 | saveInitialMotion(x, y, pointerId); 968 | 969 | final View toCapture = findTopChildUnder((int) x, (int) y); 970 | 971 | // Catch a settling view if possible. 972 | if (toCapture == mCapturedView && mDragState == STATE_SETTLING) { 973 | tryCaptureViewForDrag(toCapture, pointerId); 974 | } 975 | 976 | final int edgesTouched = mInitialEdgesTouched[pointerId]; 977 | if ((edgesTouched & mTrackingEdges) != 0) { 978 | mCallback.onEdgeTouched(edgesTouched & mTrackingEdges, pointerId); 979 | } 980 | break; 981 | } 982 | 983 | case MotionEventCompat.ACTION_POINTER_DOWN: { 984 | final int pointerId = MotionEventCompat.getPointerId(ev, actionIndex); 985 | final float x = MotionEventCompat.getX(ev, actionIndex); 986 | final float y = MotionEventCompat.getY(ev, actionIndex); 987 | 988 | saveInitialMotion(x, y, pointerId); 989 | 990 | // A ViewDragHelper can only manipulate one view at a time. 991 | if (mDragState == STATE_IDLE) { 992 | final int edgesTouched = mInitialEdgesTouched[pointerId]; 993 | if ((edgesTouched & mTrackingEdges) != 0) { 994 | mCallback.onEdgeTouched(edgesTouched & mTrackingEdges, pointerId); 995 | } 996 | } else if (mDragState == STATE_SETTLING) { 997 | // Catch a settling view if possible. 998 | final View toCapture = findTopChildUnder((int) x, (int) y); 999 | if (toCapture == mCapturedView) { 1000 | tryCaptureViewForDrag(toCapture, pointerId); 1001 | } 1002 | } 1003 | break; 1004 | } 1005 | 1006 | case MotionEvent.ACTION_MOVE: { 1007 | // First to cross a touch slop over a draggable view wins. Also report edge drags. 1008 | final int pointerCount = MotionEventCompat.getPointerCount(ev); 1009 | for (int i = 0; i < pointerCount && mInitialMotionX != null && mInitialMotionY != null; i++) { 1010 | final int pointerId = MotionEventCompat.getPointerId(ev, i); 1011 | if (pointerId >= mInitialMotionX.length || pointerId >= mInitialMotionY.length) { 1012 | continue; 1013 | } 1014 | final float x = MotionEventCompat.getX(ev, i); 1015 | final float y = MotionEventCompat.getY(ev, i); 1016 | final float dx = x - mInitialMotionX[pointerId]; 1017 | final float dy = y - mInitialMotionY[pointerId]; 1018 | 1019 | reportNewEdgeDrags(dx, dy, pointerId); 1020 | if (mDragState == STATE_DRAGGING) { 1021 | // Callback might have started an edge drag 1022 | break; 1023 | } 1024 | 1025 | final View toCapture = findTopChildUnder((int)mInitialMotionX[pointerId], (int)mInitialMotionY[pointerId]); 1026 | if (toCapture != null && checkTouchSlop(toCapture, dx, dy) && 1027 | tryCaptureViewForDrag(toCapture, pointerId)) { 1028 | break; 1029 | } 1030 | } 1031 | saveLastMotion(ev); 1032 | break; 1033 | } 1034 | 1035 | case MotionEventCompat.ACTION_POINTER_UP: { 1036 | final int pointerId = MotionEventCompat.getPointerId(ev, actionIndex); 1037 | clearMotionHistory(pointerId); 1038 | break; 1039 | } 1040 | 1041 | case MotionEvent.ACTION_UP: 1042 | case MotionEvent.ACTION_CANCEL: { 1043 | cancel(); 1044 | break; 1045 | } 1046 | } 1047 | 1048 | return mDragState == STATE_DRAGGING; 1049 | } 1050 | 1051 | /** 1052 | * Process a touch event received by the parent view. This method will dispatch callback events 1053 | * as needed before returning. The parent view's onTouchEvent implementation should call this. 1054 | * 1055 | * @param ev The touch event received by the parent view 1056 | */ 1057 | public void processTouchEvent(MotionEvent ev) { 1058 | final int action = MotionEventCompat.getActionMasked(ev); 1059 | final int actionIndex = MotionEventCompat.getActionIndex(ev); 1060 | 1061 | if (action == MotionEvent.ACTION_DOWN) { 1062 | // Reset things for a new event stream, just in case we didn't get 1063 | // the whole previous stream. 1064 | cancel(); 1065 | } 1066 | 1067 | if (mVelocityTracker == null) { 1068 | mVelocityTracker = VelocityTracker.obtain(); 1069 | } 1070 | mVelocityTracker.addMovement(ev); 1071 | 1072 | switch (action) { 1073 | case MotionEvent.ACTION_DOWN: { 1074 | final float x = ev.getX(); 1075 | final float y = ev.getY(); 1076 | final int pointerId = MotionEventCompat.getPointerId(ev, 0); 1077 | final View toCapture = findTopChildUnder((int) x, (int) y); 1078 | 1079 | saveInitialMotion(x, y, pointerId); 1080 | 1081 | // Since the parent is already directly processing this touch event, 1082 | // there is no reason to delay for a slop before dragging. 1083 | // Start immediately if possible. 1084 | tryCaptureViewForDrag(toCapture, pointerId); 1085 | 1086 | final int edgesTouched = mInitialEdgesTouched[pointerId]; 1087 | if ((edgesTouched & mTrackingEdges) != 0) { 1088 | mCallback.onEdgeTouched(edgesTouched & mTrackingEdges, pointerId); 1089 | } 1090 | break; 1091 | } 1092 | 1093 | case MotionEventCompat.ACTION_POINTER_DOWN: { 1094 | final int pointerId = MotionEventCompat.getPointerId(ev, actionIndex); 1095 | final float x = MotionEventCompat.getX(ev, actionIndex); 1096 | final float y = MotionEventCompat.getY(ev, actionIndex); 1097 | 1098 | saveInitialMotion(x, y, pointerId); 1099 | 1100 | // A ViewDragHelper can only manipulate one view at a time. 1101 | if (mDragState == STATE_IDLE) { 1102 | // If we're idle we can do anything! Treat it like a normal down event. 1103 | 1104 | final View toCapture = findTopChildUnder((int) x, (int) y); 1105 | tryCaptureViewForDrag(toCapture, pointerId); 1106 | 1107 | final int edgesTouched = mInitialEdgesTouched[pointerId]; 1108 | if ((edgesTouched & mTrackingEdges) != 0) { 1109 | mCallback.onEdgeTouched(edgesTouched & mTrackingEdges, pointerId); 1110 | } 1111 | } else if (isCapturedViewUnder((int) x, (int) y)) { 1112 | // We're still tracking a captured view. If the same view is under this 1113 | // point, we'll swap to controlling it with this pointer instead. 1114 | // (This will still work if we're "catching" a settling view.) 1115 | 1116 | tryCaptureViewForDrag(mCapturedView, pointerId); 1117 | } 1118 | break; 1119 | } 1120 | 1121 | case MotionEvent.ACTION_MOVE: { 1122 | if (mDragState == STATE_DRAGGING) { 1123 | final int index = MotionEventCompat.findPointerIndex(ev, mActivePointerId); 1124 | final float x = MotionEventCompat.getX(ev, index); 1125 | final float y = MotionEventCompat.getY(ev, index); 1126 | final int idx = (int) (x - mLastMotionX[mActivePointerId]); 1127 | final int idy = (int) (y - mLastMotionY[mActivePointerId]); 1128 | 1129 | dragTo(mCapturedView.getLeft() + idx, mCapturedView.getTop() + idy, idx, idy); 1130 | 1131 | saveLastMotion(ev); 1132 | } else { 1133 | // Check to see if any pointer is now over a draggable view. 1134 | final int pointerCount = MotionEventCompat.getPointerCount(ev); 1135 | for (int i = 0; i < pointerCount; i++) { 1136 | final int pointerId = MotionEventCompat.getPointerId(ev, i); 1137 | final float x = MotionEventCompat.getX(ev, i); 1138 | final float y = MotionEventCompat.getY(ev, i); 1139 | final float dx = x - mInitialMotionX[pointerId]; 1140 | final float dy = y - mInitialMotionY[pointerId]; 1141 | 1142 | reportNewEdgeDrags(dx, dy, pointerId); 1143 | if (mDragState == STATE_DRAGGING) { 1144 | // Callback might have started an edge drag. 1145 | break; 1146 | } 1147 | 1148 | final View toCapture = findTopChildUnder((int) x, (int) y); 1149 | if (checkTouchSlop(toCapture, dx, dy) && 1150 | tryCaptureViewForDrag(toCapture, pointerId)) { 1151 | break; 1152 | } 1153 | } 1154 | saveLastMotion(ev); 1155 | } 1156 | break; 1157 | } 1158 | 1159 | case MotionEventCompat.ACTION_POINTER_UP: { 1160 | final int pointerId = MotionEventCompat.getPointerId(ev, actionIndex); 1161 | if (mDragState == STATE_DRAGGING && pointerId == mActivePointerId) { 1162 | // Try to find another pointer that's still holding on to the captured view. 1163 | int newActivePointer = INVALID_POINTER; 1164 | final int pointerCount = MotionEventCompat.getPointerCount(ev); 1165 | for (int i = 0; i < pointerCount; i++) { 1166 | final int id = MotionEventCompat.getPointerId(ev, i); 1167 | if (id == mActivePointerId) { 1168 | // This one's going away, skip. 1169 | continue; 1170 | } 1171 | 1172 | final float x = MotionEventCompat.getX(ev, i); 1173 | final float y = MotionEventCompat.getY(ev, i); 1174 | if (findTopChildUnder((int) x, (int) y) == mCapturedView && 1175 | tryCaptureViewForDrag(mCapturedView, id)) { 1176 | newActivePointer = mActivePointerId; 1177 | break; 1178 | } 1179 | } 1180 | 1181 | if (newActivePointer == INVALID_POINTER) { 1182 | // We didn't find another pointer still touching the view, release it. 1183 | releaseViewForPointerUp(); 1184 | } 1185 | } 1186 | clearMotionHistory(pointerId); 1187 | break; 1188 | } 1189 | 1190 | case MotionEvent.ACTION_UP: { 1191 | if (mDragState == STATE_DRAGGING) { 1192 | releaseViewForPointerUp(); 1193 | } 1194 | cancel(); 1195 | break; 1196 | } 1197 | 1198 | case MotionEvent.ACTION_CANCEL: { 1199 | if (mDragState == STATE_DRAGGING) { 1200 | dispatchViewReleased(0, 0); 1201 | } 1202 | cancel(); 1203 | break; 1204 | } 1205 | } 1206 | } 1207 | 1208 | private void reportNewEdgeDrags(float dx, float dy, int pointerId) { 1209 | int dragsStarted = 0; 1210 | if (checkNewEdgeDrag(dx, dy, pointerId, EDGE_LEFT)) { 1211 | dragsStarted |= EDGE_LEFT; 1212 | } 1213 | if (checkNewEdgeDrag(dy, dx, pointerId, EDGE_TOP)) { 1214 | dragsStarted |= EDGE_TOP; 1215 | } 1216 | if (checkNewEdgeDrag(dx, dy, pointerId, EDGE_RIGHT)) { 1217 | dragsStarted |= EDGE_RIGHT; 1218 | } 1219 | if (checkNewEdgeDrag(dy, dx, pointerId, EDGE_BOTTOM)) { 1220 | dragsStarted |= EDGE_BOTTOM; 1221 | } 1222 | 1223 | if (dragsStarted != 0) { 1224 | mEdgeDragsInProgress[pointerId] |= dragsStarted; 1225 | mCallback.onEdgeDragStarted(dragsStarted, pointerId); 1226 | } 1227 | } 1228 | 1229 | private boolean checkNewEdgeDrag(float delta, float odelta, int pointerId, int edge) { 1230 | final float absDelta = Math.abs(delta); 1231 | final float absODelta = Math.abs(odelta); 1232 | 1233 | if ((mInitialEdgesTouched[pointerId] & edge) != edge || (mTrackingEdges & edge) == 0 || 1234 | (mEdgeDragsLocked[pointerId] & edge) == edge || 1235 | (mEdgeDragsInProgress[pointerId] & edge) == edge || 1236 | (absDelta <= mTouchSlop && absODelta <= mTouchSlop)) { 1237 | return false; 1238 | } 1239 | if (absDelta < absODelta * 0.5f && mCallback.onEdgeLock(edge)) { 1240 | mEdgeDragsLocked[pointerId] |= edge; 1241 | return false; 1242 | } 1243 | return (mEdgeDragsInProgress[pointerId] & edge) == 0 && absDelta > mTouchSlop; 1244 | } 1245 | 1246 | /** 1247 | * Check if we've crossed a reasonable touch slop for the given child view. 1248 | * If the child cannot be dragged along the horizontal or vertical axis, motion 1249 | * along that axis will not count toward the slop check. 1250 | * 1251 | * @param child Child to check 1252 | * @param dx Motion since initial position along X axis 1253 | * @param dy Motion since initial position along Y axis 1254 | * @return true if the touch slop has been crossed 1255 | */ 1256 | private boolean checkTouchSlop(View child, float dx, float dy) { 1257 | if (child == null) { 1258 | return false; 1259 | } 1260 | final boolean checkHorizontal = mCallback.getViewHorizontalDragRange(child) > 0; 1261 | final boolean checkVertical = mCallback.getViewVerticalDragRange(child) > 0; 1262 | 1263 | if (checkHorizontal && checkVertical) { 1264 | return dx * dx + dy * dy > mTouchSlop * mTouchSlop; 1265 | } else if (checkHorizontal) { 1266 | return Math.abs(dx) > mTouchSlop; 1267 | } else if (checkVertical) { 1268 | return Math.abs(dy) > mTouchSlop; 1269 | } 1270 | return false; 1271 | } 1272 | 1273 | /** 1274 | * Check if any pointer tracked in the current gesture has crossed 1275 | * the required slop threshold. 1276 | * 1277 | *This depends on internal state populated by 1278 | * {@link #shouldInterceptTouchEvent(MotionEvent)} or 1279 | * {@link #processTouchEvent(MotionEvent)}. You should only rely on 1280 | * the results of this method after all currently available touch data 1281 | * has been provided to one of these two methods.
1282 | * 1283 | * @param directions Combination of direction flags, see {@link #DIRECTION_HORIZONTAL}, 1284 | * {@link #DIRECTION_VERTICAL}, {@link #DIRECTION_ALL} 1285 | * @return true if the slop threshold has been crossed, false otherwise 1286 | */ 1287 | public boolean checkTouchSlop(int directions) { 1288 | final int count = mInitialMotionX.length; 1289 | for (int i = 0; i < count; i++) { 1290 | if (checkTouchSlop(directions, i)) { 1291 | return true; 1292 | } 1293 | } 1294 | return false; 1295 | } 1296 | 1297 | /** 1298 | * Check if the specified pointer tracked in the current gesture has crossed 1299 | * the required slop threshold. 1300 | * 1301 | *This depends on internal state populated by 1302 | * {@link #shouldInterceptTouchEvent(MotionEvent)} or 1303 | * {@link #processTouchEvent(MotionEvent)}. You should only rely on 1304 | * the results of this method after all currently available touch data 1305 | * has been provided to one of these two methods.
1306 | * 1307 | * @param directions Combination of direction flags, see {@link #DIRECTION_HORIZONTAL}, 1308 | * {@link #DIRECTION_VERTICAL}, {@link #DIRECTION_ALL} 1309 | * @param pointerId ID of the pointer to slop check as specified by MotionEvent 1310 | * @return true if the slop threshold has been crossed, false otherwise 1311 | */ 1312 | public boolean checkTouchSlop(int directions, int pointerId) { 1313 | if (!isPointerDown(pointerId)) { 1314 | return false; 1315 | } 1316 | 1317 | final boolean checkHorizontal = (directions & DIRECTION_HORIZONTAL) == DIRECTION_HORIZONTAL; 1318 | final boolean checkVertical = (directions & DIRECTION_VERTICAL) == DIRECTION_VERTICAL; 1319 | 1320 | final float dx = mLastMotionX[pointerId] - mInitialMotionX[pointerId]; 1321 | final float dy = mLastMotionY[pointerId] - mInitialMotionY[pointerId]; 1322 | 1323 | if (checkHorizontal && checkVertical) { 1324 | return dx * dx + dy * dy > mTouchSlop * mTouchSlop; 1325 | } else if (checkHorizontal) { 1326 | return Math.abs(dx) > mTouchSlop; 1327 | } else if (checkVertical) { 1328 | return Math.abs(dy) > mTouchSlop; 1329 | } 1330 | return false; 1331 | } 1332 | 1333 | /** 1334 | * Check if any of the edges specified were initially touched in the currently active gesture. 1335 | * If there is no currently active gesture this method will return false. 1336 | * 1337 | * @param edges Edges to check for an initial edge touch. See {@link #EDGE_LEFT}, 1338 | * {@link #EDGE_TOP}, {@link #EDGE_RIGHT}, {@link #EDGE_BOTTOM} and 1339 | * {@link #EDGE_ALL} 1340 | * @return true if any of the edges specified were initially touched in the current gesture 1341 | */ 1342 | public boolean isEdgeTouched(int edges) { 1343 | final int count = mInitialEdgesTouched.length; 1344 | for (int i = 0; i < count; i++) { 1345 | if (isEdgeTouched(edges, i)) { 1346 | return true; 1347 | } 1348 | } 1349 | return false; 1350 | } 1351 | 1352 | /** 1353 | * Check if any of the edges specified were initially touched by the pointer with 1354 | * the specified ID. If there is no currently active gesture or if there is no pointer with 1355 | * the given ID currently down this method will return false. 1356 | * 1357 | * @param edges Edges to check for an initial edge touch. See {@link #EDGE_LEFT}, 1358 | * {@link #EDGE_TOP}, {@link #EDGE_RIGHT}, {@link #EDGE_BOTTOM} and 1359 | * {@link #EDGE_ALL} 1360 | * @return true if any of the edges specified were initially touched in the current gesture 1361 | */ 1362 | public boolean isEdgeTouched(int edges, int pointerId) { 1363 | return isPointerDown(pointerId) && (mInitialEdgesTouched[pointerId] & edges) != 0; 1364 | } 1365 | 1366 | private void releaseViewForPointerUp() { 1367 | mVelocityTracker.computeCurrentVelocity(1000, mMaxVelocity); 1368 | final float xvel = clampMag( 1369 | VelocityTrackerCompat.getXVelocity(mVelocityTracker, mActivePointerId), 1370 | mMinVelocity, mMaxVelocity); 1371 | final float yvel = clampMag( 1372 | VelocityTrackerCompat.getYVelocity(mVelocityTracker, mActivePointerId), 1373 | mMinVelocity, mMaxVelocity); 1374 | dispatchViewReleased(xvel, yvel); 1375 | } 1376 | 1377 | private void dragTo(int left, int top, int dx, int dy) { 1378 | int clampedX = left; 1379 | int clampedY = top; 1380 | final int oldLeft = mCapturedView.getLeft(); 1381 | final int oldTop = mCapturedView.getTop(); 1382 | if (dx != 0) { 1383 | clampedX = mCallback.clampViewPositionHorizontal(mCapturedView, left, dx); 1384 | mCapturedView.offsetLeftAndRight(clampedX - oldLeft); 1385 | } 1386 | if (dy != 0) { 1387 | clampedY = mCallback.clampViewPositionVertical(mCapturedView, top, dy); 1388 | mCapturedView.offsetTopAndBottom(clampedY - oldTop); 1389 | } 1390 | 1391 | if (dx != 0 || dy != 0) { 1392 | final int clampedDx = clampedX - oldLeft; 1393 | final int clampedDy = clampedY - oldTop; 1394 | mCallback.onViewPositionChanged(mCapturedView, clampedX, clampedY, 1395 | clampedDx, clampedDy); 1396 | } 1397 | } 1398 | 1399 | /** 1400 | * Determine if the currently captured view is under the given point in the 1401 | * parent view's coordinate system. If there is no captured view this method 1402 | * will return false. 1403 | * 1404 | * @param x X position to test in the parent's coordinate system 1405 | * @param y Y position to test in the parent's coordinate system 1406 | * @return true if the captured view is under the given point, false otherwise 1407 | */ 1408 | public boolean isCapturedViewUnder(int x, int y) { 1409 | return isViewUnder(mCapturedView, x, y); 1410 | } 1411 | 1412 | /** 1413 | * Determine if the supplied view is under the given point in the 1414 | * parent view's coordinate system. 1415 | * 1416 | * @param view Child view of the parent to hit test 1417 | * @param x X position to test in the parent's coordinate system 1418 | * @param y Y position to test in the parent's coordinate system 1419 | * @return true if the supplied view is under the given point, false otherwise 1420 | */ 1421 | public boolean isViewUnder(View view, int x, int y) { 1422 | if (view == null) { 1423 | return false; 1424 | } 1425 | return x >= view.getLeft() && 1426 | x < view.getRight() && 1427 | y >= view.getTop() && 1428 | y < view.getBottom(); 1429 | } 1430 | 1431 | /** 1432 | * Find the topmost child under the given point within the parent view's coordinate system. 1433 | * The child order is determined using {@link neal.adapterview.core.ViewDragHelper.Callback#getOrderedChildIndex(int)}. 1434 | * 1435 | * @param x X position to test in the parent's coordinate system 1436 | * @param y Y position to test in the parent's coordinate system 1437 | * @return The topmost child view under (x, y) or null if none found. 1438 | */ 1439 | public View findTopChildUnder(int x, int y) { 1440 | final int childCount = mParentView.getChildCount(); 1441 | for (int i = childCount - 1; i >= 0; i--) { 1442 | final View child = mParentView.getChildAt(mCallback.getOrderedChildIndex(i)); 1443 | if (x >= child.getLeft() && x < child.getRight() && 1444 | y >= child.getTop() && y < child.getBottom()) { 1445 | return child; 1446 | } 1447 | } 1448 | return null; 1449 | } 1450 | 1451 | private int getEdgesTouched(int x, int y) { 1452 | int result = 0; 1453 | 1454 | if (x < mParentView.getLeft() + mEdgeSize) result |= EDGE_LEFT; 1455 | if (y < mParentView.getTop() + mEdgeSize) result |= EDGE_TOP; 1456 | if (x > mParentView.getRight() - mEdgeSize) result |= EDGE_RIGHT; 1457 | if (y > mParentView.getBottom() - mEdgeSize) result |= EDGE_BOTTOM; 1458 | 1459 | return result; 1460 | } 1461 | } --------------------------------------------------------------------------------