Implement cliff guard for swipeDismiss gesture
BUG: 14319825 Change-Id: I6a80c91b4a6ee2eaf49a8c0c69065d4b97d7f8f2
This commit is contained in:
@ -35,7 +35,7 @@ import android.widget.FrameLayout;
|
|||||||
public class SwipeDismissLayout extends FrameLayout {
|
public class SwipeDismissLayout extends FrameLayout {
|
||||||
private static final String TAG = "SwipeDismissLayout";
|
private static final String TAG = "SwipeDismissLayout";
|
||||||
|
|
||||||
private static final float TRANSLATION_MIN_ALPHA = 0.5f;
|
private static final float DISMISS_MIN_PROGRESS = 0.6f;
|
||||||
|
|
||||||
public interface OnDismissedListener {
|
public interface OnDismissedListener {
|
||||||
void onDismissed(SwipeDismissLayout layout);
|
void onDismissed(SwipeDismissLayout layout);
|
||||||
@ -77,6 +77,8 @@ public class SwipeDismissLayout extends FrameLayout {
|
|||||||
private OnDismissedListener mDismissedListener;
|
private OnDismissedListener mDismissedListener;
|
||||||
private OnSwipeProgressChangedListener mProgressListener;
|
private OnSwipeProgressChangedListener mProgressListener;
|
||||||
|
|
||||||
|
private float mLastX;
|
||||||
|
|
||||||
public SwipeDismissLayout(Context context) {
|
public SwipeDismissLayout(Context context) {
|
||||||
super(context);
|
super(context);
|
||||||
init(context);
|
init(context);
|
||||||
@ -95,7 +97,7 @@ public class SwipeDismissLayout extends FrameLayout {
|
|||||||
private void init(Context context) {
|
private void init(Context context) {
|
||||||
ViewConfiguration vc = ViewConfiguration.get(getContext());
|
ViewConfiguration vc = ViewConfiguration.get(getContext());
|
||||||
mSlop = vc.getScaledTouchSlop();
|
mSlop = vc.getScaledTouchSlop();
|
||||||
mMinFlingVelocity = vc.getScaledMinimumFlingVelocity() * 16;
|
mMinFlingVelocity = vc.getScaledMinimumFlingVelocity();
|
||||||
mMaxFlingVelocity = vc.getScaledMaximumFlingVelocity();
|
mMaxFlingVelocity = vc.getScaledMaximumFlingVelocity();
|
||||||
mAnimationTime = getContext().getResources().getInteger(
|
mAnimationTime = getContext().getResources().getInteger(
|
||||||
android.R.integer.config_shortAnimTime);
|
android.R.integer.config_shortAnimTime);
|
||||||
@ -193,8 +195,8 @@ public class SwipeDismissLayout extends FrameLayout {
|
|||||||
|
|
||||||
case MotionEvent.ACTION_MOVE:
|
case MotionEvent.ACTION_MOVE:
|
||||||
mVelocityTracker.addMovement(ev);
|
mVelocityTracker.addMovement(ev);
|
||||||
|
mLastX = ev.getRawX();
|
||||||
updateSwiping(ev);
|
updateSwiping(ev);
|
||||||
updateDismiss(ev);
|
|
||||||
if (mSwiping) {
|
if (mSwiping) {
|
||||||
setProgress(ev.getRawX() - mDownX);
|
setProgress(ev.getRawX() - mDownX);
|
||||||
break;
|
break;
|
||||||
@ -256,20 +258,16 @@ public class SwipeDismissLayout extends FrameLayout {
|
|||||||
float absVelocityX = Math.abs(velocityX);
|
float absVelocityX = Math.abs(velocityX);
|
||||||
float absVelocityY = Math.abs(mVelocityTracker.getYVelocity());
|
float absVelocityY = Math.abs(mVelocityTracker.getYVelocity());
|
||||||
|
|
||||||
if (deltaX > getWidth() / 2) {
|
if (deltaX > (getWidth() * DISMISS_MIN_PROGRESS) &&
|
||||||
mDismissed = true;
|
absVelocityX < mMinFlingVelocity &&
|
||||||
} else if (absVelocityX >= mMinFlingVelocity
|
ev.getRawX() >= mLastX) {
|
||||||
&& absVelocityX <= mMaxFlingVelocity
|
|
||||||
&& absVelocityY < absVelocityX / 2
|
|
||||||
&& velocityX > 0
|
|
||||||
&& deltaX > 0) {
|
|
||||||
mDismissed = true;
|
mDismissed = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Check if the user tried to undo this.
|
// Check if the user tried to undo this.
|
||||||
if (mDismissed && mSwiping) {
|
if (mDismissed && mSwiping) {
|
||||||
// Check if the user's finger is actually back
|
// Check if the user's finger is actually back
|
||||||
if (deltaX < getWidth() / 2) {
|
if (deltaX < (getWidth() * DISMISS_MIN_PROGRESS)) {
|
||||||
mDismissed = false;
|
mDismissed = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -32,7 +32,7 @@
|
|||||||
<dimen name="config_viewConfigurationTouchSlop">4dp</dimen>
|
<dimen name="config_viewConfigurationTouchSlop">4dp</dimen>
|
||||||
|
|
||||||
<!-- Minimum velocity to initiate a fling, as measured in dips per second. -->
|
<!-- Minimum velocity to initiate a fling, as measured in dips per second. -->
|
||||||
<dimen name="config_viewMinFlingVelocity">50dp</dimen>
|
<dimen name="config_viewMinFlingVelocity">500dp</dimen>
|
||||||
|
|
||||||
<!-- Maximum velocity to initiate a fling, as measured in dips per second. -->
|
<!-- Maximum velocity to initiate a fling, as measured in dips per second. -->
|
||||||
<dimen name="config_viewMaxFlingVelocity">8000dp</dimen>
|
<dimen name="config_viewMaxFlingVelocity">8000dp</dimen>
|
||||||
|
Reference in New Issue
Block a user