|
@ -1,24 +1,22 @@ |
|
|
package awais.instagrabber.customviews; |
|
|
package awais.instagrabber.customviews; |
|
|
|
|
|
|
|
|
import android.content.Context; |
|
|
import android.content.Context; |
|
|
import android.util.Log; |
|
|
|
|
|
import android.view.GestureDetector; |
|
|
import android.view.GestureDetector; |
|
|
import android.view.MotionEvent; |
|
|
import android.view.MotionEvent; |
|
|
import android.view.View; |
|
|
import android.view.View; |
|
|
import android.view.ViewConfiguration; |
|
|
|
|
|
import android.view.ViewParent; |
|
|
import android.view.ViewParent; |
|
|
|
|
|
|
|
|
import androidx.annotation.NonNull; |
|
|
import androidx.annotation.NonNull; |
|
|
|
|
|
|
|
|
public class VerticalDragHelper { |
|
|
public class VerticalDragHelper { |
|
|
private static final String TAG = "VerticalDragHelper"; |
|
|
|
|
|
private static final float PIXELS_PER_SECOND = 10; |
|
|
|
|
|
|
|
|
// private static final String TAG = "VerticalDragHelper"; |
|
|
|
|
|
private static final double SWIPE_THRESHOLD_VELOCITY = 80; |
|
|
|
|
|
|
|
|
private final View view; |
|
|
private final View view; |
|
|
|
|
|
|
|
|
private GestureDetector gestureDetector; |
|
|
private GestureDetector gestureDetector; |
|
|
private Context context; |
|
|
private Context context; |
|
|
private float flingVelocity; |
|
|
|
|
|
|
|
|
private double flingVelocity; |
|
|
private OnVerticalDragListener onVerticalDragListener; |
|
|
private OnVerticalDragListener onVerticalDragListener; |
|
|
|
|
|
|
|
|
private final GestureDetector.OnGestureListener gestureListener = new GestureDetector.SimpleOnGestureListener() { |
|
|
private final GestureDetector.OnGestureListener gestureListener = new GestureDetector.SimpleOnGestureListener() { |
|
@ -31,32 +29,13 @@ public class VerticalDragHelper { |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public boolean onFling(final MotionEvent e1, final MotionEvent e2, final float velocityX, final float velocityY) { |
|
|
public boolean onFling(final MotionEvent e1, final MotionEvent e2, final float velocityX, final float velocityY) { |
|
|
float maxFlingVelocity = ViewConfiguration.get(context).getScaledMaximumFlingVelocity(); |
|
|
|
|
|
float velocityPercentY = velocityY / maxFlingVelocity; |
|
|
|
|
|
float normalizedVelocityY = velocityPercentY * PIXELS_PER_SECOND; |
|
|
|
|
|
if (Math.abs(normalizedVelocityY) > 4) { |
|
|
|
|
|
flingVelocity = normalizedVelocityY; |
|
|
|
|
|
|
|
|
double yDir = e1.getY() - e2.getY(); |
|
|
|
|
|
// Log.d(TAG, "onFling: yDir: " + yDir); |
|
|
|
|
|
if (yDir < -SWIPE_THRESHOLD_VELOCITY || yDir > SWIPE_THRESHOLD_VELOCITY) { |
|
|
|
|
|
flingVelocity = yDir; |
|
|
} |
|
|
} |
|
|
return super.onFling(e1, e2, velocityX, velocityY); |
|
|
return super.onFling(e1, e2, velocityX, velocityY); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
private final GestureDetector.OnGestureListener dragPreventionGestureListener = new GestureDetector.SimpleOnGestureListener() { |
|
|
|
|
|
float prevDistanceY = 0; |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
public boolean onScroll(final MotionEvent e1, final MotionEvent e2, final float distanceX, final float distanceY) { |
|
|
|
|
|
Log.d(TAG, "onScroll: distanceX: " + distanceX + ", distanceY: " + distanceY); |
|
|
|
|
|
return super.onScroll(e1, e2, distanceX, distanceY); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
public boolean onSingleTapUp(final MotionEvent e) { |
|
|
|
|
|
Log.d(TAG, "onSingleTapUp"); |
|
|
|
|
|
return super.onSingleTapUp(e); |
|
|
|
|
|
} |
|
|
|
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
private float prevRawY; |
|
|
private float prevRawY; |
|
@ -64,7 +43,6 @@ public class VerticalDragHelper { |
|
|
private float prevRawX; |
|
|
private float prevRawX; |
|
|
private float dX; |
|
|
private float dX; |
|
|
private float prevDY; |
|
|
private float prevDY; |
|
|
private GestureDetector dragPreventionGestureDetector; |
|
|
|
|
|
|
|
|
|
|
|
public VerticalDragHelper(@NonNull final View view) { |
|
|
public VerticalDragHelper(@NonNull final View view) { |
|
|
this.view = view; |
|
|
this.view = view; |
|
@ -80,14 +58,12 @@ public class VerticalDragHelper { |
|
|
|
|
|
|
|
|
protected void init() { |
|
|
protected void init() { |
|
|
gestureDetector = new GestureDetector(context, gestureListener); |
|
|
gestureDetector = new GestureDetector(context, gestureListener); |
|
|
dragPreventionGestureDetector = new GestureDetector(context, dragPreventionGestureListener); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public boolean onDragTouch(final MotionEvent event) { |
|
|
public boolean onDragTouch(final MotionEvent event) { |
|
|
if (onVerticalDragListener == null) { |
|
|
if (onVerticalDragListener == null) { |
|
|
return false; |
|
|
return false; |
|
|
} |
|
|
} |
|
|
// dragPreventionGestureDetector.onTouchEvent(event); |
|
|
|
|
|
if (gestureDetector.onTouchEvent(event)) { |
|
|
if (gestureDetector.onTouchEvent(event)) { |
|
|
return true; |
|
|
return true; |
|
|
} |
|
|
} |
|
@ -151,53 +127,11 @@ public class VerticalDragHelper { |
|
|
return gestureDetector.onTouchEvent(event); |
|
|
return gestureDetector.onTouchEvent(event); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private final static int DIRECTION_UP = 0; |
|
|
|
|
|
private final static int DIRECTION_DOWN = 1; |
|
|
|
|
|
float prevY = -1; |
|
|
|
|
|
int edgeHitCount = 0; |
|
|
|
|
|
float prevDirection = -1; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// private boolean shouldPreventDrag(final MotionEvent event) { |
|
|
|
|
|
// switch (event.getAction()) { |
|
|
|
|
|
// case MotionEvent.ACTION_DOWN: |
|
|
|
|
|
// if (!firstDrag) { |
|
|
|
|
|
// firstDrag = true; |
|
|
|
|
|
// } |
|
|
|
|
|
// return false; |
|
|
|
|
|
// case MotionEvent.ACTION_MOVE: |
|
|
|
|
|
// float y = event.getY(); |
|
|
|
|
|
// int direction = -2; |
|
|
|
|
|
// if (prevY != -1) { |
|
|
|
|
|
// final float dy = y - prevY; |
|
|
|
|
|
// // Log.d(TAG, "shouldPreventDrag: dy: " + dy); |
|
|
|
|
|
// if (dy > 0) { |
|
|
|
|
|
// direction = DIRECTION_DOWN; |
|
|
|
|
|
// // move direction is down |
|
|
|
|
|
// } else { |
|
|
|
|
|
// direction = DIRECTION_UP; |
|
|
|
|
|
// // move direction is up |
|
|
|
|
|
// } |
|
|
|
|
|
// } |
|
|
|
|
|
// prevY = y; |
|
|
|
|
|
// if (prevDirection == direction) { |
|
|
|
|
|
// edgeHitCount++; |
|
|
|
|
|
// } else { |
|
|
|
|
|
// edgeHitCount = 1; |
|
|
|
|
|
// } |
|
|
|
|
|
// if (edgeHitCount >= 2) { |
|
|
|
|
|
// return false; |
|
|
|
|
|
// } |
|
|
|
|
|
// return true; |
|
|
|
|
|
// break; |
|
|
|
|
|
// } |
|
|
|
|
|
// } |
|
|
|
|
|
|
|
|
|
|
|
public interface OnVerticalDragListener { |
|
|
public interface OnVerticalDragListener { |
|
|
void onDrag(final float dY); |
|
|
void onDrag(final float dY); |
|
|
|
|
|
|
|
|
void onDragEnd(); |
|
|
void onDragEnd(); |
|
|
|
|
|
|
|
|
void onFling(final float flingVelocity); |
|
|
|
|
|
|
|
|
void onFling(final double flingVelocity); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |