Skip to content

Commit

Permalink
Revalidate against 24.2.0 removing deprecation
Browse files Browse the repository at this point in the history
  • Loading branch information
Jawnnypoo committed Aug 22, 2016
1 parent 3ee525b commit e55970b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 33 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ TranslationViewDragHelper.Callback mCallback = new TranslationViewDragHelper.Cal
See the sample [custom view](https://github.com/Commit451/TranslationViewDragHelper/blob/master/app/src/main/java/com/commit451/betterviewdraghelper/sample/AllowsForDragFrameLayout.java) for a more complete example

# Basis
The current version of this helper is derived from `ViewDragHelper` source from `24.0.0` of the Support Library. Due to private constructor access on ViewDragHelper, the source has to be copied out into the TranslationViewDragHelper
The current version of this helper is derived from `ViewDragHelper` source from `24.0.0` of the Support Library. Due to private constructor access on ViewDragHelper, the source has to be copied out into the `TranslationViewDragHelper`. You can check the diff [here](https://www.diffchecker.com/)

License
--------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@
import java.util.Arrays;

/**
* Modified version of {@link android.support.v4.widget.ViewDragHelper} which accounts for if views
* Modified version of ViewDragHelper which accounts for if views
* have been translated using {@link View#setX(float)} {@link View#setY(float)} or
* {@link View#setTranslationX(float)} and {@link View#setTranslationY(float)}
*/
public class TranslationViewDragHelper {
private static final String TAG = "TranslationViewDragHelper";
private static final String TAG = "TransViewDragHelper";

/**
* A null/invalid pointer ID.
Expand Down Expand Up @@ -326,13 +326,15 @@ public int clampViewPositionVertical(View child, int top, int dy) {
* Interpolator defining the animation curve for mScroller
*/
private static final Interpolator sInterpolator = new Interpolator() {
@Override
public float getInterpolation(float t) {
t -= 1.0f;
return t * t * t * t * t + 1.0f;
}
};

private final Runnable mSetIdleRunnable = new Runnable() {
@Override
public void run() {
setDragState(STATE_IDLE);
}
Expand Down Expand Up @@ -790,7 +792,7 @@ private void clearMotionHistory() {
}

private void clearMotionHistory(int pointerId) {
if (mInitialMotionX == null) {
if (mInitialMotionX == null || !isPointerDown(pointerId)) {
return;
}
mInitialMotionX[pointerId] = 0;
Expand Down Expand Up @@ -842,11 +844,15 @@ private void saveInitialMotion(float x, float y, int pointerId) {
}

private void saveLastMotion(MotionEvent ev) {
final int pointerCount = MotionEventCompat.getPointerCount(ev);
final int pointerCount = ev.getPointerCount();
for (int i = 0; i < pointerCount; i++) {
final int pointerId = MotionEventCompat.getPointerId(ev, i);
final float x = MotionEventCompat.getX(ev, i);
final float y = MotionEventCompat.getY(ev, i);
final int pointerId = ev.getPointerId(i);
// If pointer is invalid then skip saving on ACTION_MOVE.
if (!isValidPointerForActionMove(pointerId)) {
continue;
}
final float x = ev.getX(i);
final float y = ev.getY(i);
mLastMotionX[pointerId] = x;
mLastMotionY[pointerId] = y;
}
Expand Down Expand Up @@ -964,7 +970,7 @@ public boolean shouldInterceptTouchEvent(MotionEvent ev) {
case MotionEvent.ACTION_DOWN: {
final float x = ev.getX();
final float y = ev.getY();
final int pointerId = MotionEventCompat.getPointerId(ev, 0);
final int pointerId = ev.getPointerId(0);
saveInitialMotion(x, y, pointerId);

final View toCapture = findTopChildUnder((int) x, (int) y);
Expand All @@ -982,9 +988,9 @@ public boolean shouldInterceptTouchEvent(MotionEvent ev) {
}

case MotionEventCompat.ACTION_POINTER_DOWN: {
final int pointerId = MotionEventCompat.getPointerId(ev, actionIndex);
final float x = MotionEventCompat.getX(ev, actionIndex);
final float y = MotionEventCompat.getY(ev, actionIndex);
final int pointerId = ev.getPointerId(actionIndex);
final float x = ev.getX(actionIndex);
final float y = ev.getY(actionIndex);

saveInitialMotion(x, y, pointerId);

Expand All @@ -1008,15 +1014,15 @@ public boolean shouldInterceptTouchEvent(MotionEvent ev) {
if (mInitialMotionX == null || mInitialMotionY == null) break;

// First to cross a touch slop over a draggable view wins. Also report edge drags.
final int pointerCount = MotionEventCompat.getPointerCount(ev);
final int pointerCount = ev.getPointerCount();
for (int i = 0; i < pointerCount; i++) {
final int pointerId = MotionEventCompat.getPointerId(ev, i);
final int pointerId = ev.getPointerId(i);

// If pointer is invalid then skip the ACTION_MOVE.
if (!isValidPointerForActionMove(pointerId)) continue;

final float x = MotionEventCompat.getX(ev, i);
final float y = MotionEventCompat.getY(ev, i);
final float x = ev.getX(i);
final float y = ev.getY(i);
final float dx = x - mInitialMotionX[pointerId];
final float dy = y - mInitialMotionY[pointerId];

Expand Down Expand Up @@ -1060,7 +1066,7 @@ public boolean shouldInterceptTouchEvent(MotionEvent ev) {
}

case MotionEventCompat.ACTION_POINTER_UP: {
final int pointerId = MotionEventCompat.getPointerId(ev, actionIndex);
final int pointerId = ev.getPointerId(actionIndex);
clearMotionHistory(pointerId);
break;
}
Expand Down Expand Up @@ -1100,7 +1106,7 @@ public void processTouchEvent(MotionEvent ev) {
case MotionEvent.ACTION_DOWN: {
final float x = ev.getX();
final float y = ev.getY();
final int pointerId = MotionEventCompat.getPointerId(ev, 0);
final int pointerId = ev.getPointerId(0);
final View toCapture = findTopChildUnder((int) x, (int) y);

saveInitialMotion(x, y, pointerId);
Expand All @@ -1118,9 +1124,9 @@ public void processTouchEvent(MotionEvent ev) {
}

case MotionEventCompat.ACTION_POINTER_DOWN: {
final int pointerId = MotionEventCompat.getPointerId(ev, actionIndex);
final float x = MotionEventCompat.getX(ev, actionIndex);
final float y = MotionEventCompat.getY(ev, actionIndex);
final int pointerId = ev.getPointerId(actionIndex);
final float x = ev.getX(actionIndex);
final float y = ev.getY(actionIndex);

saveInitialMotion(x, y, pointerId);

Expand Down Expand Up @@ -1150,9 +1156,9 @@ public void processTouchEvent(MotionEvent ev) {
// If pointer is invalid then skip the ACTION_MOVE.
if (!isValidPointerForActionMove(mActivePointerId)) break;

final int index = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
final float x = MotionEventCompat.getX(ev, index);
final float y = MotionEventCompat.getY(ev, index);
final int index = ev.findPointerIndex(mActivePointerId);
final float x = ev.getX(index);
final float y = ev.getY(index);
final int idx = (int) (x - mLastMotionX[mActivePointerId]);
final int idy = (int) (y - mLastMotionY[mActivePointerId]);

Expand All @@ -1161,15 +1167,15 @@ public void processTouchEvent(MotionEvent ev) {
saveLastMotion(ev);
} else {
// Check to see if any pointer is now over a draggable view.
final int pointerCount = MotionEventCompat.getPointerCount(ev);
final int pointerCount = ev.getPointerCount();
for (int i = 0; i < pointerCount; i++) {
final int pointerId = MotionEventCompat.getPointerId(ev, i);
final int pointerId = ev.getPointerId(i);

// If pointer is invalid then skip the ACTION_MOVE.
if (!isValidPointerForActionMove(pointerId)) continue;

final float x = MotionEventCompat.getX(ev, i);
final float y = MotionEventCompat.getY(ev, i);
final float x = ev.getX(i);
final float y = ev.getY(i);
final float dx = x - mInitialMotionX[pointerId];
final float dy = y - mInitialMotionY[pointerId];

Expand All @@ -1191,20 +1197,20 @@ public void processTouchEvent(MotionEvent ev) {
}

case MotionEventCompat.ACTION_POINTER_UP: {
final int pointerId = MotionEventCompat.getPointerId(ev, actionIndex);
final int pointerId = ev.getPointerId(actionIndex);
if (mDragState == STATE_DRAGGING && pointerId == mActivePointerId) {
// Try to find another pointer that's still holding on to the captured view.
int newActivePointer = INVALID_POINTER;
final int pointerCount = MotionEventCompat.getPointerCount(ev);
final int pointerCount = ev.getPointerCount();
for (int i = 0; i < pointerCount; i++) {
final int id = MotionEventCompat.getPointerId(ev, i);
final int id = ev.getPointerId(i);
if (id == mActivePointerId) {
// This one's going away, skip.
continue;
}

final float x = MotionEventCompat.getX(ev, i);
final float y = MotionEventCompat.getY(ev, i);
final float x = ev.getX(i);
final float y = ev.getY(i);
if (findTopChildUnder((int) x, (int) y) == mCapturedView &&
tryCaptureViewForDrag(mCapturedView, id)) {
newActivePointer = mActivePointerId;
Expand Down

0 comments on commit e55970b

Please sign in to comment.