Make swipe up/down to dismiss post view more sensitive
This commit is contained in:
parent
23f3ef45c2
commit
a6a4b6292d
@ -1,24 +1,22 @@
|
||||
package awais.instagrabber.customviews;
|
||||
|
||||
import android.content.Context;
|
||||
import android.util.Log;
|
||||
import android.view.GestureDetector;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
import android.view.ViewConfiguration;
|
||||
import android.view.ViewParent;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
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 GestureDetector gestureDetector;
|
||||
private Context context;
|
||||
private float flingVelocity;
|
||||
private double flingVelocity;
|
||||
private OnVerticalDragListener onVerticalDragListener;
|
||||
|
||||
private final GestureDetector.OnGestureListener gestureListener = new GestureDetector.SimpleOnGestureListener() {
|
||||
@ -31,32 +29,13 @@ public class VerticalDragHelper {
|
||||
|
||||
@Override
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
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;
|
||||
@ -64,7 +43,6 @@ public class VerticalDragHelper {
|
||||
private float prevRawX;
|
||||
private float dX;
|
||||
private float prevDY;
|
||||
private GestureDetector dragPreventionGestureDetector;
|
||||
|
||||
public VerticalDragHelper(@NonNull final View view) {
|
||||
this.view = view;
|
||||
@ -80,14 +58,12 @@ public class VerticalDragHelper {
|
||||
|
||||
protected void init() {
|
||||
gestureDetector = new GestureDetector(context, gestureListener);
|
||||
dragPreventionGestureDetector = new GestureDetector(context, dragPreventionGestureListener);
|
||||
}
|
||||
|
||||
public boolean onDragTouch(final MotionEvent event) {
|
||||
if (onVerticalDragListener == null) {
|
||||
return false;
|
||||
}
|
||||
// dragPreventionGestureDetector.onTouchEvent(event);
|
||||
if (gestureDetector.onTouchEvent(event)) {
|
||||
return true;
|
||||
}
|
||||
@ -151,53 +127,11 @@ public class VerticalDragHelper {
|
||||
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 {
|
||||
void onDrag(final float dY);
|
||||
|
||||
void onDragEnd();
|
||||
|
||||
void onFling(final float flingVelocity);
|
||||
void onFling(final double flingVelocity);
|
||||
}
|
||||
}
|
||||
|
@ -125,8 +125,8 @@ public class PostViewV2Fragment extends SharedElementTransitionDialogFragment {
|
||||
@Override
|
||||
public void onDragEnd() {
|
||||
// animate and dismiss if user drags the view more that 30% of the view
|
||||
if (Math.abs(binding.getRoot().getY()) > Utils.displayMetrics.heightPixels * 0.35) {
|
||||
animateAndDismiss(binding.getRoot().getY() < 0 ? -1 : 1);
|
||||
if (Math.abs(binding.getRoot().getY()) > Utils.displayMetrics.heightPixels * 0.25) {
|
||||
animateAndDismiss(binding.getRoot().getY() < 0 ? 1 : -1);
|
||||
return;
|
||||
}
|
||||
// animate back the view to proper position
|
||||
@ -134,16 +134,16 @@ public class PostViewV2Fragment extends SharedElementTransitionDialogFragment {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFling(final float flingVelocity) {
|
||||
public void onFling(final double flingVelocity) {
|
||||
// animate and dismiss if user flings up/down
|
||||
animateAndDismiss(flingVelocity < 0 ? -1 : 1);
|
||||
animateAndDismiss(flingVelocity > 0 ? 1 : -1);
|
||||
}
|
||||
|
||||
private void animateAndDismiss(final int direction) {
|
||||
final int height = binding.getRoot().getHeight();
|
||||
final int finalYDist = height + Utils.getStatusBarHeight(context);
|
||||
// less than 0 means up direction, else down
|
||||
final int finalY = direction < 0 ? -finalYDist : finalYDist;
|
||||
final int finalY = direction > 0 ? -finalYDist : finalYDist;
|
||||
animateY(binding.getRoot(), finalY, 200, new AnimatorListenerAdapter() {
|
||||
@Override
|
||||
public void onAnimationEnd(final Animator animation) {
|
||||
|
Loading…
Reference in New Issue
Block a user