├── settings.gradle
├── slidinguppanel.png
├── demo
├── ic_launcher-web.png
├── res
│ ├── drawable-xhdpi
│ │ ├── graphic.png
│ │ └── ic_launcher.png
│ ├── drawable-hdpi
│ │ └── ic_launcher.png
│ ├── drawable-ldpi
│ │ └── ic_launcher.png
│ ├── drawable-mdpi
│ │ └── ic_launcher.png
│ ├── drawable-xxhdpi
│ │ └── ic_launcher.png
│ ├── menu
│ │ └── demo.xml
│ ├── values
│ │ ├── strings.xml
│ │ └── styles.xml
│ └── layout
│ │ └── activity_demo.xml
├── settings.gradle
├── project.properties
├── build.gradle
├── proguard-project.txt
├── AndroidManifest.xml
└── src
│ └── com
│ └── sothree
│ └── slidinguppanel
│ └── demo
│ └── DemoActivity.java
├── library
├── gradle.properties
├── libs
│ ├── android-support-v4.jar
│ └── nineoldandroids-2.4.0.jar
├── res
│ ├── drawable
│ │ ├── above_shadow.xml
│ │ └── below_shadow.xml
│ └── values
│ │ └── attrs.xml
├── AndroidManifest.xml
├── project.properties
├── build.gradle
└── src
│ └── com
│ └── sothree
│ └── slidinguppanel
│ ├── SlidingUpPanelLayout.java
│ └── ViewDragHelper.java
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .gitignore
├── gradle.properties
├── gradlew.bat
├── maven_push.gradle
├── gradlew
├── README.md
└── LICENSE.txt
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':library'
2 | include ':demo'
3 |
--------------------------------------------------------------------------------
/slidinguppanel.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/anvesh523/AndroidSlidingUpPanel/HEAD/slidinguppanel.png
--------------------------------------------------------------------------------
/demo/ic_launcher-web.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/anvesh523/AndroidSlidingUpPanel/HEAD/demo/ic_launcher-web.png
--------------------------------------------------------------------------------
/library/gradle.properties:
--------------------------------------------------------------------------------
1 | POM_NAME=Android Sliding Up Panel Library
2 | POM_ARTIFACT_ID=library
3 | POM_PACKAGING=aar
4 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/anvesh523/AndroidSlidingUpPanel/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/demo/res/drawable-xhdpi/graphic.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/anvesh523/AndroidSlidingUpPanel/HEAD/demo/res/drawable-xhdpi/graphic.png
--------------------------------------------------------------------------------
/library/libs/android-support-v4.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/anvesh523/AndroidSlidingUpPanel/HEAD/library/libs/android-support-v4.jar
--------------------------------------------------------------------------------
/demo/res/drawable-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/anvesh523/AndroidSlidingUpPanel/HEAD/demo/res/drawable-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/demo/res/drawable-ldpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/anvesh523/AndroidSlidingUpPanel/HEAD/demo/res/drawable-ldpi/ic_launcher.png
--------------------------------------------------------------------------------
/demo/res/drawable-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/anvesh523/AndroidSlidingUpPanel/HEAD/demo/res/drawable-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/library/libs/nineoldandroids-2.4.0.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/anvesh523/AndroidSlidingUpPanel/HEAD/library/libs/nineoldandroids-2.4.0.jar
--------------------------------------------------------------------------------
/demo/res/drawable-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/anvesh523/AndroidSlidingUpPanel/HEAD/demo/res/drawable-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/demo/res/drawable-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/anvesh523/AndroidSlidingUpPanel/HEAD/demo/res/drawable-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/demo/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':Tnt2'
2 |
3 | include 'androidslidingup'
4 |
5 | project(':androidslidingup').projectDir = new File(settingsDir, '../library/')
6 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Wed Jan 14 17:29:14 PST 2015
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.2.1-all.zip
7 |
--------------------------------------------------------------------------------
/library/res/drawable/above_shadow.xml:
--------------------------------------------------------------------------------
1 |
2 |
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(android.view.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 Callback#onEdgeTouched(int, int)} and 428 | * {@link 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 Callback#tryCaptureView(android.view.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(android.view.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 | mScroller.startScroll(startLeft, startTop, dx, dy, duration); 592 | 593 | setDragState(STATE_SETTLING); 594 | return true; 595 | } 596 | 597 | private int computeSettleDuration(View child, int dx, int dy, int xvel, int yvel) { 598 | xvel = clampMag(xvel, (int) mMinVelocity, (int) mMaxVelocity); 599 | yvel = clampMag(yvel, (int) mMinVelocity, (int) mMaxVelocity); 600 | final int absDx = Math.abs(dx); 601 | final int absDy = Math.abs(dy); 602 | final int absXVel = Math.abs(xvel); 603 | final int absYVel = Math.abs(yvel); 604 | final int addedVel = absXVel + absYVel; 605 | final int addedDistance = absDx + absDy; 606 | 607 | final float xweight = xvel != 0 ? (float) absXVel / addedVel : 608 | (float) absDx / addedDistance; 609 | final float yweight = yvel != 0 ? (float) absYVel / addedVel : 610 | (float) absDy / addedDistance; 611 | 612 | int xduration = computeAxisDuration(dx, xvel, mCallback.getViewHorizontalDragRange(child)); 613 | int yduration = computeAxisDuration(dy, yvel, mCallback.getViewVerticalDragRange(child)); 614 | 615 | return (int) (xduration * xweight + yduration * yweight); 616 | } 617 | 618 | private int computeAxisDuration(int delta, int velocity, int motionRange) { 619 | if (delta == 0) { 620 | return 0; 621 | } 622 | 623 | final int width = mParentView.getWidth(); 624 | final int halfWidth = width / 2; 625 | final float distanceRatio = Math.min(1f, (float) Math.abs(delta) / width); 626 | final float distance = halfWidth + halfWidth * 627 | distanceInfluenceForSnapDuration(distanceRatio); 628 | 629 | int duration; 630 | velocity = Math.abs(velocity); 631 | if (velocity > 0) { 632 | duration = 4 * Math.round(1000 * Math.abs(distance / velocity)); 633 | } else { 634 | final float range = (float) Math.abs(delta) / motionRange; 635 | duration = (int) ((range + 1) * BASE_SETTLE_DURATION); 636 | } 637 | return Math.min(duration, MAX_SETTLE_DURATION); 638 | } 639 | 640 | /** 641 | * Clamp the magnitude of value for absMin and absMax. 642 | * If the value is below the minimum, it will be clamped to zero. 643 | * If the value is above the maximum, it will be clamped to the maximum. 644 | * 645 | * @param value Value to clamp 646 | * @param absMin Absolute value of the minimum significant value to return 647 | * @param absMax Absolute value of the maximum value to return 648 | * @return The clamped value with the same sign asvalue
649 | */
650 | private int clampMag(int value, int absMin, int absMax) {
651 | final int absValue = Math.abs(value);
652 | if (absValue < absMin) return 0;
653 | if (absValue > absMax) return value > 0 ? absMax : -absMax;
654 | return value;
655 | }
656 |
657 | /**
658 | * Clamp the magnitude of value for absMin and absMax.
659 | * If the value is below the minimum, it will be clamped to zero.
660 | * If the value is above the maximum, it will be clamped to the maximum.
661 | *
662 | * @param value Value to clamp
663 | * @param absMin Absolute value of the minimum significant value to return
664 | * @param absMax Absolute value of the maximum value to return
665 | * @return The clamped value with the same sign as value
666 | */
667 | private float clampMag(float value, float absMin, float absMax) {
668 | final float absValue = Math.abs(value);
669 | if (absValue < absMin) return 0;
670 | if (absValue > absMax) return value > 0 ? absMax : -absMax;
671 | return value;
672 | }
673 |
674 | private float distanceInfluenceForSnapDuration(float f) {
675 | f -= 0.5f; // center the values about 0.
676 | f *= 0.3f * Math.PI / 2.0f;
677 | return (float) Math.sin(f);
678 | }
679 |
680 | /**
681 | * Settle the captured view based on standard free-moving fling behavior.
682 | * The caller should invoke {@link #continueSettling(boolean)} on each subsequent frame
683 | * to continue the motion until it returns false.
684 | *
685 | * @param minLeft Minimum X position for the view's left edge
686 | * @param minTop Minimum Y position for the view's top edge
687 | * @param maxLeft Maximum X position for the view's left edge
688 | * @param maxTop Maximum Y position for the view's top edge
689 | */
690 | public void flingCapturedView(int minLeft, int minTop, int maxLeft, int maxTop) {
691 | if (!mReleaseInProgress) {
692 | throw new IllegalStateException("Cannot flingCapturedView outside of a call to " +
693 | "Callback#onViewReleased");
694 | }
695 |
696 | mScroller.fling(mCapturedView.getLeft(), mCapturedView.getTop(),
697 | (int) VelocityTrackerCompat.getXVelocity(mVelocityTracker, mActivePointerId),
698 | (int) VelocityTrackerCompat.getYVelocity(mVelocityTracker, mActivePointerId),
699 | minLeft, maxLeft, minTop, maxTop);
700 |
701 | setDragState(STATE_SETTLING);
702 | }
703 |
704 | /**
705 | * Move the captured settling view by the appropriate amount for the current time.
706 | * If continueSettling returns true, the caller should call it again
707 | * on the next frame to continue.
708 | *
709 | * @param deferCallbacks true if state callbacks should be deferred via posted message.
710 | * Set this to true if you are calling this method from
711 | * {@link android.view.View#computeScroll()} or similar methods
712 | * invoked as part of layout or drawing.
713 | * @return true if settle is still in progress
714 | */
715 | public boolean continueSettling(boolean deferCallbacks) {
716 | // Make sure, there is a captured view
717 | if (mCapturedView == null) {
718 | return false;
719 | }
720 | if (mDragState == STATE_SETTLING) {
721 | boolean keepGoing = mScroller.computeScrollOffset();
722 | final int x = mScroller.getCurrX();
723 | final int y = mScroller.getCurrY();
724 | final int dx = x - mCapturedView.getLeft();
725 | final int dy = y - mCapturedView.getTop();
726 |
727 | if (dx != 0) {
728 | mCapturedView.offsetLeftAndRight(dx);
729 | }
730 | if (dy != 0) {
731 | mCapturedView.offsetTopAndBottom(dy);
732 | }
733 |
734 | if (dx != 0 || dy != 0) {
735 | mCallback.onViewPositionChanged(mCapturedView, x, y, dx, dy);
736 | }
737 |
738 | if (keepGoing && x == mScroller.getFinalX() && y == mScroller.getFinalY()) {
739 | // Close enough. The interpolator/scroller might think we're still moving
740 | // but the user sure doesn't.
741 | mScroller.abortAnimation();
742 | keepGoing = mScroller.isFinished();
743 | }
744 |
745 | if (!keepGoing) {
746 | if (deferCallbacks) {
747 | mParentView.post(mSetIdleRunnable);
748 | } else {
749 | setDragState(STATE_IDLE);
750 | }
751 | }
752 | }
753 |
754 | return mDragState == STATE_SETTLING;
755 | }
756 |
757 | /**
758 | * Like all callback events this must happen on the UI thread, but release
759 | * involves some extra semantics. During a release (mReleaseInProgress)
760 | * is the only time it is valid to call {@link #settleCapturedViewAt(int, int)}
761 | * or {@link #flingCapturedView(int, int, int, int)}.
762 | */
763 | private void dispatchViewReleased(float xvel, float yvel) {
764 | mReleaseInProgress = true;
765 | mCallback.onViewReleased(mCapturedView, xvel, yvel);
766 | mReleaseInProgress = false;
767 |
768 | if (mDragState == STATE_DRAGGING) {
769 | // onViewReleased didn't call a method that would have changed this. Go idle.
770 | setDragState(STATE_IDLE);
771 | }
772 | }
773 |
774 | private void clearMotionHistory() {
775 | if (mInitialMotionX == null) {
776 | return;
777 | }
778 | Arrays.fill(mInitialMotionX, 0);
779 | Arrays.fill(mInitialMotionY, 0);
780 | Arrays.fill(mLastMotionX, 0);
781 | Arrays.fill(mLastMotionY, 0);
782 | Arrays.fill(mInitialEdgesTouched, 0);
783 | Arrays.fill(mEdgeDragsInProgress, 0);
784 | Arrays.fill(mEdgeDragsLocked, 0);
785 | mPointersDown = 0;
786 | }
787 |
788 | private void clearMotionHistory(int pointerId) {
789 | if (mInitialMotionX == null) {
790 | return;
791 | }
792 | mInitialMotionX[pointerId] = 0;
793 | mInitialMotionY[pointerId] = 0;
794 | mLastMotionX[pointerId] = 0;
795 | mLastMotionY[pointerId] = 0;
796 | mInitialEdgesTouched[pointerId] = 0;
797 | mEdgeDragsInProgress[pointerId] = 0;
798 | mEdgeDragsLocked[pointerId] = 0;
799 | mPointersDown &= ~(1 << pointerId);
800 | }
801 |
802 | private void ensureMotionHistorySizeForId(int pointerId) {
803 | if (mInitialMotionX == null || mInitialMotionX.length <= pointerId) {
804 | float[] imx = new float[pointerId + 1];
805 | float[] imy = new float[pointerId + 1];
806 | float[] lmx = new float[pointerId + 1];
807 | float[] lmy = new float[pointerId + 1];
808 | int[] iit = new int[pointerId + 1];
809 | int[] edip = new int[pointerId + 1];
810 | int[] edl = new int[pointerId + 1];
811 |
812 | if (mInitialMotionX != null) {
813 | System.arraycopy(mInitialMotionX, 0, imx, 0, mInitialMotionX.length);
814 | System.arraycopy(mInitialMotionY, 0, imy, 0, mInitialMotionY.length);
815 | System.arraycopy(mLastMotionX, 0, lmx, 0, mLastMotionX.length);
816 | System.arraycopy(mLastMotionY, 0, lmy, 0, mLastMotionY.length);
817 | System.arraycopy(mInitialEdgesTouched, 0, iit, 0, mInitialEdgesTouched.length);
818 | System.arraycopy(mEdgeDragsInProgress, 0, edip, 0, mEdgeDragsInProgress.length);
819 | System.arraycopy(mEdgeDragsLocked, 0, edl, 0, mEdgeDragsLocked.length);
820 | }
821 |
822 | mInitialMotionX = imx;
823 | mInitialMotionY = imy;
824 | mLastMotionX = lmx;
825 | mLastMotionY = lmy;
826 | mInitialEdgesTouched = iit;
827 | mEdgeDragsInProgress = edip;
828 | mEdgeDragsLocked = edl;
829 | }
830 | }
831 |
832 | private void saveInitialMotion(float x, float y, int pointerId) {
833 | ensureMotionHistorySizeForId(pointerId);
834 | mInitialMotionX[pointerId] = mLastMotionX[pointerId] = x;
835 | mInitialMotionY[pointerId] = mLastMotionY[pointerId] = y;
836 | mInitialEdgesTouched[pointerId] = getEdgesTouched((int) x, (int) y);
837 | mPointersDown |= 1 << pointerId;
838 | }
839 |
840 | private void saveLastMotion(MotionEvent ev) {
841 | final int pointerCount = MotionEventCompat.getPointerCount(ev);
842 | for (int i = 0; i < pointerCount; i++) {
843 | final int pointerId = MotionEventCompat.getPointerId(ev, i);
844 | final float x = MotionEventCompat.getX(ev, i);
845 | final float y = MotionEventCompat.getY(ev, i);
846 | if (mLastMotionX != null && mLastMotionY != null) {
847 | mLastMotionX[pointerId] = x;
848 | mLastMotionY[pointerId] = y;
849 | }
850 | }
851 | }
852 |
853 | /**
854 | * Check if the given pointer ID represents a pointer that is currently down (to the best
855 | * of the ViewDragHelper's knowledge).
856 | *
857 | * The state used to report this information is populated by the methods 858 | * {@link #shouldInterceptTouchEvent(android.view.MotionEvent)} or 859 | * {@link #processTouchEvent(android.view.MotionEvent)}. If one of these methods has not 860 | * been called for all relevant MotionEvents to track, the information reported 861 | * by this method may be stale or incorrect.
862 | * 863 | * @param pointerId pointer ID to check; corresponds to IDs provided by MotionEvent 864 | * @return true if the pointer with the given ID is still down 865 | */ 866 | public boolean isPointerDown(int pointerId) { 867 | return (mPointersDown & 1 << pointerId) != 0; 868 | } 869 | 870 | void setDragState(int state) { 871 | if (mDragState != state) { 872 | mDragState = state; 873 | mCallback.onViewDragStateChanged(state); 874 | if (state == STATE_IDLE) { 875 | mCapturedView = null; 876 | } 877 | } 878 | } 879 | 880 | /** 881 | * Attempt to capture the view with the given pointer ID. The callback will be involved. 882 | * This will put us into the "dragging" state. If we've already captured this view with 883 | * this pointer this method will immediately return true without consulting the callback. 884 | * 885 | * @param toCapture View to capture 886 | * @param pointerId Pointer to capture with 887 | * @return true if capture was successful 888 | */ 889 | boolean tryCaptureViewForDrag(View toCapture, int pointerId) { 890 | if (toCapture == mCapturedView && mActivePointerId == pointerId) { 891 | // Already done! 892 | return true; 893 | } 894 | if (toCapture != null && mCallback.tryCaptureView(toCapture, pointerId)) { 895 | mActivePointerId = pointerId; 896 | captureChildView(toCapture, pointerId); 897 | return true; 898 | } 899 | return false; 900 | } 901 | 902 | /** 903 | * Tests scrollability within child views of v given a delta of dx. 904 | * 905 | * @param v View to test for horizontal scrollability 906 | * @param checkV Whether the view v passed should itself be checked for scrollability (true), 907 | * or just its children (false). 908 | * @param dx Delta scrolled in pixels along the X axis 909 | * @param dy Delta scrolled in pixels along the Y axis 910 | * @param x X coordinate of the active touch point 911 | * @param y Y coordinate of the active touch point 912 | * @return true if child views of v can be scrolled by delta of dx. 913 | */ 914 | protected boolean canScroll(View v, boolean checkV, int dx, int dy, int x, int y) { 915 | if (v instanceof ViewGroup) { 916 | final ViewGroup group = (ViewGroup) v; 917 | final int scrollX = v.getScrollX(); 918 | final int scrollY = v.getScrollY(); 919 | final int count = group.getChildCount(); 920 | // Count backwards - let topmost views consume scroll distance first. 921 | for (int i = count - 1; i >= 0; i--) { 922 | // TODO: Add versioned support here for transformed views. 923 | // This will not work for transformed views in Honeycomb+ 924 | final View child = group.getChildAt(i); 925 | if (x + scrollX >= child.getLeft() && x + scrollX < child.getRight() && 926 | y + scrollY >= child.getTop() && y + scrollY < child.getBottom() && 927 | canScroll(child, true, dx, dy, x + scrollX - child.getLeft(), 928 | y + scrollY - child.getTop())) { 929 | return true; 930 | } 931 | } 932 | } 933 | 934 | return checkV && (ViewCompat.canScrollHorizontally(v, -dx) || 935 | ViewCompat.canScrollVertically(v, -dy)); 936 | } 937 | 938 | /** 939 | * Check if this event as provided to the parent view's onInterceptTouchEvent should 940 | * cause the parent to intercept the touch event stream. 941 | * 942 | * @param ev MotionEvent provided to onInterceptTouchEvent 943 | * @return true if the parent view should return true from onInterceptTouchEvent 944 | */ 945 | public boolean shouldInterceptTouchEvent(MotionEvent ev) { 946 | final int action = MotionEventCompat.getActionMasked(ev); 947 | final int actionIndex = MotionEventCompat.getActionIndex(ev); 948 | 949 | if (action == MotionEvent.ACTION_DOWN) { 950 | // Reset things for a new event stream, just in case we didn't get 951 | // the whole previous stream. 952 | cancel(); 953 | } 954 | 955 | if (mVelocityTracker == null) { 956 | mVelocityTracker = VelocityTracker.obtain(); 957 | } 958 | mVelocityTracker.addMovement(ev); 959 | 960 | switch (action) { 961 | case MotionEvent.ACTION_DOWN: { 962 | final float x = ev.getX(); 963 | final float y = ev.getY(); 964 | final int pointerId = MotionEventCompat.getPointerId(ev, 0); 965 | saveInitialMotion(x, y, pointerId); 966 | 967 | final View toCapture = findTopChildUnder((int) x, (int) y); 968 | 969 | // Catch a settling view if possible. 970 | if (toCapture == mCapturedView && mDragState == STATE_SETTLING) { 971 | tryCaptureViewForDrag(toCapture, pointerId); 972 | } 973 | 974 | final int edgesTouched = mInitialEdgesTouched[pointerId]; 975 | if ((edgesTouched & mTrackingEdges) != 0) { 976 | mCallback.onEdgeTouched(edgesTouched & mTrackingEdges, pointerId); 977 | } 978 | break; 979 | } 980 | 981 | case MotionEventCompat.ACTION_POINTER_DOWN: { 982 | final int pointerId = MotionEventCompat.getPointerId(ev, actionIndex); 983 | final float x = MotionEventCompat.getX(ev, actionIndex); 984 | final float y = MotionEventCompat.getY(ev, actionIndex); 985 | 986 | saveInitialMotion(x, y, pointerId); 987 | 988 | // A ViewDragHelper can only manipulate one view at a time. 989 | if (mDragState == STATE_IDLE) { 990 | final int edgesTouched = mInitialEdgesTouched[pointerId]; 991 | if ((edgesTouched & mTrackingEdges) != 0) { 992 | mCallback.onEdgeTouched(edgesTouched & mTrackingEdges, pointerId); 993 | } 994 | } else if (mDragState == STATE_SETTLING) { 995 | // Catch a settling view if possible. 996 | final View toCapture = findTopChildUnder((int) x, (int) y); 997 | if (toCapture == mCapturedView) { 998 | tryCaptureViewForDrag(toCapture, pointerId); 999 | } 1000 | } 1001 | break; 1002 | } 1003 | 1004 | case MotionEvent.ACTION_MOVE: { 1005 | // First to cross a touch slop over a draggable view wins. Also report edge drags. 1006 | final int pointerCount = MotionEventCompat.getPointerCount(ev); 1007 | for (int i = 0; i < pointerCount && mInitialMotionX != null && mInitialMotionY != null; i++) { 1008 | final int pointerId = MotionEventCompat.getPointerId(ev, i); 1009 | if (pointerId >= mInitialMotionX.length || pointerId >= mInitialMotionY.length) { 1010 | continue; 1011 | } 1012 | final float x = MotionEventCompat.getX(ev, i); 1013 | final float y = MotionEventCompat.getY(ev, i); 1014 | final float dx = x - mInitialMotionX[pointerId]; 1015 | final float dy = y - mInitialMotionY[pointerId]; 1016 | 1017 | reportNewEdgeDrags(dx, dy, pointerId); 1018 | if (mDragState == STATE_DRAGGING) { 1019 | // Callback might have started an edge drag 1020 | break; 1021 | } 1022 | 1023 | final View toCapture = findTopChildUnder((int)mInitialMotionX[pointerId], (int)mInitialMotionY[pointerId]); 1024 | if (toCapture != null && checkTouchSlop(toCapture, dx, dy) && 1025 | tryCaptureViewForDrag(toCapture, pointerId)) { 1026 | break; 1027 | } 1028 | } 1029 | saveLastMotion(ev); 1030 | break; 1031 | } 1032 | 1033 | case MotionEventCompat.ACTION_POINTER_UP: { 1034 | final int pointerId = MotionEventCompat.getPointerId(ev, actionIndex); 1035 | clearMotionHistory(pointerId); 1036 | break; 1037 | } 1038 | 1039 | case MotionEvent.ACTION_UP: 1040 | case MotionEvent.ACTION_CANCEL: { 1041 | cancel(); 1042 | break; 1043 | } 1044 | } 1045 | 1046 | return mDragState == STATE_DRAGGING; 1047 | } 1048 | 1049 | /** 1050 | * Process a touch event received by the parent view. This method will dispatch callback events 1051 | * as needed before returning. The parent view's onTouchEvent implementation should call this. 1052 | * 1053 | * @param ev The touch event received by the parent view 1054 | */ 1055 | public void processTouchEvent(MotionEvent ev) { 1056 | final int action = MotionEventCompat.getActionMasked(ev); 1057 | final int actionIndex = MotionEventCompat.getActionIndex(ev); 1058 | 1059 | if (action == MotionEvent.ACTION_DOWN) { 1060 | // Reset things for a new event stream, just in case we didn't get 1061 | // the whole previous stream. 1062 | cancel(); 1063 | } 1064 | 1065 | if (mVelocityTracker == null) { 1066 | mVelocityTracker = VelocityTracker.obtain(); 1067 | } 1068 | mVelocityTracker.addMovement(ev); 1069 | 1070 | switch (action) { 1071 | case MotionEvent.ACTION_DOWN: { 1072 | final float x = ev.getX(); 1073 | final float y = ev.getY(); 1074 | final int pointerId = MotionEventCompat.getPointerId(ev, 0); 1075 | final View toCapture = findTopChildUnder((int) x, (int) y); 1076 | 1077 | saveInitialMotion(x, y, pointerId); 1078 | 1079 | // Since the parent is already directly processing this touch event, 1080 | // there is no reason to delay for a slop before dragging. 1081 | // Start immediately if possible. 1082 | tryCaptureViewForDrag(toCapture, pointerId); 1083 | 1084 | final int edgesTouched = mInitialEdgesTouched[pointerId]; 1085 | if ((edgesTouched & mTrackingEdges) != 0) { 1086 | mCallback.onEdgeTouched(edgesTouched & mTrackingEdges, pointerId); 1087 | } 1088 | break; 1089 | } 1090 | 1091 | case MotionEventCompat.ACTION_POINTER_DOWN: { 1092 | final int pointerId = MotionEventCompat.getPointerId(ev, actionIndex); 1093 | final float x = MotionEventCompat.getX(ev, actionIndex); 1094 | final float y = MotionEventCompat.getY(ev, actionIndex); 1095 | 1096 | saveInitialMotion(x, y, pointerId); 1097 | 1098 | // A ViewDragHelper can only manipulate one view at a time. 1099 | if (mDragState == STATE_IDLE) { 1100 | // If we're idle we can do anything! Treat it like a normal down event. 1101 | 1102 | final View toCapture = findTopChildUnder((int) x, (int) y); 1103 | tryCaptureViewForDrag(toCapture, pointerId); 1104 | 1105 | final int edgesTouched = mInitialEdgesTouched[pointerId]; 1106 | if ((edgesTouched & mTrackingEdges) != 0) { 1107 | mCallback.onEdgeTouched(edgesTouched & mTrackingEdges, pointerId); 1108 | } 1109 | } else if (isCapturedViewUnder((int) x, (int) y)) { 1110 | // We're still tracking a captured view. If the same view is under this 1111 | // point, we'll swap to controlling it with this pointer instead. 1112 | // (This will still work if we're "catching" a settling view.) 1113 | 1114 | tryCaptureViewForDrag(mCapturedView, pointerId); 1115 | } 1116 | break; 1117 | } 1118 | 1119 | case MotionEvent.ACTION_MOVE: { 1120 | if (mDragState == STATE_DRAGGING) { 1121 | final int index = MotionEventCompat.findPointerIndex(ev, mActivePointerId); 1122 | final float x = MotionEventCompat.getX(ev, index); 1123 | final float y = MotionEventCompat.getY(ev, index); 1124 | final int idx = (int) (x - mLastMotionX[mActivePointerId]); 1125 | final int idy = (int) (y - mLastMotionY[mActivePointerId]); 1126 | 1127 | dragTo(mCapturedView.getLeft() + idx, mCapturedView.getTop() + idy, idx, idy); 1128 | 1129 | saveLastMotion(ev); 1130 | } else { 1131 | // Check to see if any pointer is now over a draggable view. 1132 | final int pointerCount = MotionEventCompat.getPointerCount(ev); 1133 | for (int i = 0; i < pointerCount; i++) { 1134 | final int pointerId = MotionEventCompat.getPointerId(ev, i) 1135 | ; 1136 | final float x = MotionEventCompat.getX(ev, i); 1137 | final float y = MotionEventCompat.getY(ev, i); 1138 | final float dx = x - mInitialMotionX[pointerId]; 1139 | final float dy = y - mInitialMotionY[pointerId]; 1140 | 1141 | reportNewEdgeDrags(dx, dy, pointerId); 1142 | if (mDragState == STATE_DRAGGING) { 1143 | // Callback might have started an edge drag. 1144 | break; 1145 | } 1146 | 1147 | final View toCapture = findTopChildUnder((int) x, (int) y); 1148 | if (checkTouchSlop(toCapture, dx, dy) && 1149 | tryCaptureViewForDrag(toCapture, pointerId)) { 1150 | break; 1151 | } 1152 | } 1153 | saveLastMotion(ev); 1154 | } 1155 | break; 1156 | } 1157 | 1158 | case MotionEventCompat.ACTION_POINTER_UP: { 1159 | final int pointerId = MotionEventCompat.getPointerId(ev, actionIndex); 1160 | if (mDragState == STATE_DRAGGING && pointerId == mActivePointerId) { 1161 | // Try to find another pointer that's still holding on to the captured view. 1162 | int newActivePointer = INVALID_POINTER; 1163 | final int pointerCount = MotionEventCompat.getPointerCount(ev); 1164 | for (int i = 0; i < pointerCount; i++) { 1165 | final int id = MotionEventCompat.getPointerId(ev, i); 1166 | if (id == mActivePointerId) { 1167 | // This one's going away, skip. 1168 | continue; 1169 | } 1170 | 1171 | final float x = MotionEventCompat.getX(ev, i); 1172 | final float y = MotionEventCompat.getY(ev, i); 1173 | if (findTopChildUnder((int) x, (int) y) == mCapturedView && 1174 | tryCaptureViewForDrag(mCapturedView, id)) { 1175 | newActivePointer = mActivePointerId; 1176 | break; 1177 | } 1178 | } 1179 | 1180 | if (newActivePointer == INVALID_POINTER) { 1181 | // We didn't find another pointer still touching the view, release it. 1182 | releaseViewForPointerUp(); 1183 | } 1184 | } 1185 | clearMotionHistory(pointerId); 1186 | break; 1187 | } 1188 | 1189 | case MotionEvent.ACTION_UP: { 1190 | if (mDragState == STATE_DRAGGING) { 1191 | releaseViewForPointerUp(); 1192 | } 1193 | cancel(); 1194 | break; 1195 | } 1196 | 1197 | case MotionEvent.ACTION_CANCEL: { 1198 | if (mDragState == STATE_DRAGGING) { 1199 | dispatchViewReleased(0, 0); 1200 | } 1201 | cancel(); 1202 | break; 1203 | } 1204 | } 1205 | } 1206 | 1207 | private void reportNewEdgeDrags(float dx, float dy, int pointerId) { 1208 | int dragsStarted = 0; 1209 | if (checkNewEdgeDrag(dx, dy, pointerId, EDGE_LEFT)) { 1210 | dragsStarted |= EDGE_LEFT; 1211 | } 1212 | if (checkNewEdgeDrag(dy, dx, pointerId, EDGE_TOP)) { 1213 | dragsStarted |= EDGE_TOP; 1214 | } 1215 | if (checkNewEdgeDrag(dx, dy, pointerId, EDGE_RIGHT)) { 1216 | dragsStarted |= EDGE_RIGHT; 1217 | } 1218 | if (checkNewEdgeDrag(dy, dx, pointerId, EDGE_BOTTOM)) { 1219 | dragsStarted |= EDGE_BOTTOM; 1220 | } 1221 | 1222 | if (dragsStarted != 0) { 1223 | mEdgeDragsInProgress[pointerId] |= dragsStarted; 1224 | mCallback.onEdgeDragStarted(dragsStarted, pointerId); 1225 | } 1226 | } 1227 | 1228 | private boolean checkNewEdgeDrag(float delta, float odelta, int pointerId, int edge) { 1229 | final float absDelta = Math.abs(delta); 1230 | final float absODelta = Math.abs(odelta); 1231 | 1232 | if ((mInitialEdgesTouched[pointerId] & edge) != edge || (mTrackingEdges & edge) == 0 || 1233 | (mEdgeDragsLocked[pointerId] & edge) == edge || 1234 | (mEdgeDragsInProgress[pointerId] & edge) == edge || 1235 | (absDelta <= mTouchSlop && absODelta <= mTouchSlop)) { 1236 | return false; 1237 | } 1238 | if (absDelta < absODelta * 0.5f && mCallback.onEdgeLock(edge)) { 1239 | mEdgeDragsLocked[pointerId] |= edge; 1240 | return false; 1241 | } 1242 | return (mEdgeDragsInProgress[pointerId] & edge) == 0 && absDelta > mTouchSlop; 1243 | } 1244 | 1245 | /** 1246 | * Check if we've crossed a reasonable touch slop for the given child view. 1247 | * If the child cannot be dragged along the horizontal or vertical axis, motion 1248 | * along that axis will not count toward the slop check. 1249 | * 1250 | * @param child Child to check 1251 | * @param dx Motion since initial position along X axis 1252 | * @param dy Motion since initial position along Y axis 1253 | * @return true if the touch slop has been crossed 1254 | */ 1255 | private boolean checkTouchSlop(View child, float dx, float dy) { 1256 | if (child == null) { 1257 | return false; 1258 | } 1259 | final boolean checkHorizontal = mCallback.getViewHorizontalDragRange(child) > 0; 1260 | final boolean checkVertical = mCallback.getViewVerticalDragRange(child) > 0; 1261 | 1262 | if (checkHorizontal && checkVertical) { 1263 | return dx * dx + dy * dy > mTouchSlop * mTouchSlop; 1264 | } else if (checkHorizontal) { 1265 | return Math.abs(dx) > mTouchSlop; 1266 | } else if (checkVertical) { 1267 | return Math.abs(dy) > mTouchSlop; 1268 | } 1269 | return false; 1270 | } 1271 | 1272 | /** 1273 | * Check if any pointer tracked in the current gesture has crossed 1274 | * the required slop threshold. 1275 | * 1276 | *This depends on internal state populated by 1277 | * {@link #shouldInterceptTouchEvent(android.view.MotionEvent)} or 1278 | * {@link #processTouchEvent(android.view.MotionEvent)}. You should only rely on 1279 | * the results of this method after all currently available touch data 1280 | * has been provided to one of these two methods.
1281 | * 1282 | * @param directions Combination of direction flags, see {@link #DIRECTION_HORIZONTAL}, 1283 | * {@link #DIRECTION_VERTICAL}, {@link #DIRECTION_ALL} 1284 | * @return true if the slop threshold has been crossed, false otherwise 1285 | */ 1286 | public boolean checkTouchSlop(int directions) { 1287 | final int count = mInitialMotionX.length; 1288 | for (int i = 0; i < count; i++) { 1289 | if (checkTouchSlop(directions, i)) { 1290 | return true; 1291 | } 1292 | } 1293 | return false; 1294 | } 1295 | 1296 | /** 1297 | * Check if the specified pointer tracked in the current gesture has crossed 1298 | * the required slop threshold. 1299 | * 1300 | *This depends on internal state populated by 1301 | * {@link #shouldInterceptTouchEvent(android.view.MotionEvent)} or 1302 | * {@link #processTouchEvent(android.view.MotionEvent)}. You should only rely on 1303 | * the results of this method after all currently available touch data 1304 | * has been provided to one of these two methods.
1305 | * 1306 | * @param directions Combination of direction flags, see {@link #DIRECTION_HORIZONTAL}, 1307 | * {@link #DIRECTION_VERTICAL}, {@link #DIRECTION_ALL} 1308 | * @param pointerId ID of the pointer to slop check as specified by MotionEvent 1309 | * @return true if the slop threshold has been crossed, false otherwise 1310 | */ 1311 | public boolean checkTouchSlop(int directions, int pointerId) { 1312 | if (!isPointerDown(pointerId)) { 1313 | return false; 1314 | } 1315 | 1316 | final boolean checkHorizontal = (directions & DIRECTION_HORIZONTAL) == DIRECTION_HORIZONTAL; 1317 | final boolean checkVertical = (directions & DIRECTION_VERTICAL) == DIRECTION_VERTICAL; 1318 | 1319 | final float dx = mLastMotionX[pointerId] - mInitialMotionX[pointerId]; 1320 | final float dy = mLastMotionY[pointerId] - mInitialMotionY[pointerId]; 1321 | 1322 | if (checkHorizontal && checkVertical) { 1323 | return dx * dx + dy * dy > mTouchSlop * mTouchSlop; 1324 | } else if (checkHorizontal) { 1325 | return Math.abs(dx) > mTouchSlop; 1326 | } else if (checkVertical) { 1327 | return Math.abs(dy) > mTouchSlop; 1328 | } 1329 | return false; 1330 | } 1331 | 1332 | /** 1333 | * Check if any of the edges specified were initially touched in the currently active gesture. 1334 | * If there is no currently active gesture this method will return false. 1335 | * 1336 | * @param edges Edges to check for an initial edge touch. See {@link #EDGE_LEFT}, 1337 | * {@link #EDGE_TOP}, {@link #EDGE_RIGHT}, {@link #EDGE_BOTTOM} and 1338 | * {@link #EDGE_ALL} 1339 | * @return true if any of the edges specified were initially touched in the current gesture 1340 | */ 1341 | public boolean isEdgeTouched(int edges) { 1342 | final int count = mInitialEdgesTouched.length; 1343 | for (int i = 0; i < count; i++) { 1344 | if (isEdgeTouched(edges, i)) { 1345 | return true; 1346 | } 1347 | } 1348 | return false; 1349 | } 1350 | 1351 | /** 1352 | * Check if any of the edges specified were initially touched by the pointer with 1353 | * the specified ID. If there is no currently active gesture or if there is no pointer with 1354 | * the given ID currently down this method will return false. 1355 | * 1356 | * @param edges Edges to check for an initial edge touch. See {@link #EDGE_LEFT}, 1357 | * {@link #EDGE_TOP}, {@link #EDGE_RIGHT}, {@link #EDGE_BOTTOM} and 1358 | * {@link #EDGE_ALL} 1359 | * @return true if any of the edges specified were initially touched in the current gesture 1360 | */ 1361 | public boolean isEdgeTouched(int edges, int pointerId) { 1362 | return isPointerDown(pointerId) && (mInitialEdgesTouched[pointerId] & edges) != 0; 1363 | } 1364 | 1365 | public boolean isDragging() { 1366 | return mDragState == STATE_DRAGGING; 1367 | } 1368 | 1369 | private void releaseViewForPointerUp() { 1370 | mVelocityTracker.computeCurrentVelocity(1000, mMaxVelocity); 1371 | final float xvel = clampMag( 1372 | VelocityTrackerCompat.getXVelocity(mVelocityTracker, mActivePointerId), 1373 | mMinVelocity, mMaxVelocity); 1374 | final float yvel = clampMag( 1375 | VelocityTrackerCompat.getYVelocity(mVelocityTracker, mActivePointerId), 1376 | mMinVelocity, mMaxVelocity); 1377 | dispatchViewReleased(xvel, yvel); 1378 | } 1379 | 1380 | private void dragTo(int left, int top, int dx, int dy) { 1381 | int clampedX = left; 1382 | int clampedY = top; 1383 | final int oldLeft = mCapturedView.getLeft(); 1384 | final int oldTop = mCapturedView.getTop(); 1385 | if (dx != 0) { 1386 | clampedX = mCallback.clampViewPositionHorizontal(mCapturedView, left, dx); 1387 | mCapturedView.offsetLeftAndRight(clampedX - oldLeft); 1388 | } 1389 | if (dy != 0) { 1390 | clampedY = mCallback.clampViewPositionVertical(mCapturedView, top, dy); 1391 | mCapturedView.offsetTopAndBottom(clampedY - oldTop); 1392 | } 1393 | 1394 | if (dx != 0 || dy != 0) { 1395 | final int clampedDx = clampedX - oldLeft; 1396 | final int clampedDy = clampedY - oldTop; 1397 | mCallback.onViewPositionChanged(mCapturedView, clampedX, clampedY, 1398 | clampedDx, clampedDy); 1399 | } 1400 | } 1401 | 1402 | /** 1403 | * Determine if the currently captured view is under the given point in the 1404 | * parent view's coordinate system. If there is no captured view this method 1405 | * will return false. 1406 | * 1407 | * @param x X position to test in the parent's coordinate system 1408 | * @param y Y position to test in the parent's coordinate system 1409 | * @return true if the captured view is under the given point, false otherwise 1410 | */ 1411 | public boolean isCapturedViewUnder(int x, int y) { 1412 | return isViewUnder(mCapturedView, x, y); 1413 | } 1414 | 1415 | /** 1416 | * Determine if the supplied view is under the given point in the 1417 | * parent view's coordinate system. 1418 | * 1419 | * @param view Child view of the parent to hit test 1420 | * @param x X position to test in the parent's coordinate system 1421 | * @param y Y position to test in the parent's coordinate system 1422 | * @return true if the supplied view is under the given point, false otherwise 1423 | */ 1424 | public boolean isViewUnder(View view, int x, int y) { 1425 | if (view == null) { 1426 | return false; 1427 | } 1428 | return x >= view.getLeft() && 1429 | x < view.getRight() && 1430 | y >= view.getTop() && 1431 | y < view.getBottom(); 1432 | } 1433 | 1434 | /** 1435 | * Find the topmost child under the given point within the parent view's coordinate system. 1436 | * The child order is determined using {@link Callback#getOrderedChildIndex(int)}. 1437 | * 1438 | * @param x X position to test in the parent's coordinate system 1439 | * @param y Y position to test in the parent's coordinate system 1440 | * @return The topmost child view under (x, y) or null if none found. 1441 | */ 1442 | public View findTopChildUnder(int x, int y) { 1443 | final int childCount = mParentView.getChildCount(); 1444 | for (int i = childCount - 1; i >= 0; i--) { 1445 | final View child = mParentView.getChildAt(mCallback.getOrderedChildIndex(i)); 1446 | if (x >= child.getLeft() && x < child.getRight() && 1447 | y >= child.getTop() && y < child.getBottom()) { 1448 | return child; 1449 | } 1450 | } 1451 | return null; 1452 | } 1453 | 1454 | private int getEdgesTouched(int x, int y) { 1455 | int result = 0; 1456 | 1457 | if (x < mParentView.getLeft() + mEdgeSize) result |= EDGE_LEFT; 1458 | if (y < mParentView.getTop() + mEdgeSize) result |= EDGE_TOP; 1459 | if (x > mParentView.getRight() - mEdgeSize) result |= EDGE_RIGHT; 1460 | if (y > mParentView.getBottom() - mEdgeSize) result |= EDGE_BOTTOM; 1461 | 1462 | return result; 1463 | } 1464 | } --------------------------------------------------------------------------------