Skip to content

Commit

Permalink
Merge pull request #135 from open-keychain/alt-swipe-anim
Browse files Browse the repository at this point in the history
nicer animation flow for dismiss on swipe
  • Loading branch information
wmora committed Aug 6, 2015
2 parents 9aba8bd + ee92b4d commit 40674e7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 17 deletions.
5 changes: 3 additions & 2 deletions lib/src/main/java/com/nispok/snackbar/Snackbar.java
Original file line number Diff line number Diff line change
Expand Up @@ -688,10 +688,11 @@ public void onClick(View view) {
snackbarAction.setVisibility(GONE);
}

setClickable(true);
View inner = layout.findViewById(R.id.sb__inner);
inner.setClickable(true);

if (mCanSwipeToDismiss && res.getBoolean(R.bool.sb__is_swipeable)) {
setOnTouchListener(new SwipeDismissTouchListener(this, null,
inner.setOnTouchListener(new SwipeDismissTouchListener(this, null,
new SwipeDismissTouchListener.DismissCallbacks() {
@Override
public boolean canDismiss(Object token) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class SwipeDismissTouchListener implements View.OnTouchListener {
private long mAnimationTime;

// Fixed properties
private View mView;
private View mContainerView;
private DismissCallbacks mCallbacks;
private int mViewWidth = 1; // 1 and not 0 to prevent dividing by zero

Expand Down Expand Up @@ -94,18 +94,18 @@ public SwipeDismissTouchListener(View view, Object token, DismissCallbacks callb
mMaxFlingVelocity = vc.getScaledMaximumFlingVelocity();
mAnimationTime = view.getContext().getResources().getInteger(
android.R.integer.config_shortAnimTime);
mView = view;
mContainerView = view;
mToken = token;
mCallbacks = callbacks;
}

@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
public boolean onTouch(final View view, MotionEvent motionEvent) {
// offset because the view is translated during swipe
motionEvent.offsetLocation(mTranslationX, 0);

if (mViewWidth < 2) {
mViewWidth = mView.getWidth();
mViewWidth = view.getWidth();
}

switch (motionEvent.getActionMasked()) {
Expand Down Expand Up @@ -147,23 +147,30 @@ public boolean onTouch(View view, MotionEvent motionEvent) {
}
if (dismiss) {
// dismiss
mView.animate()
view.animate()
.translationX(dismissRight ? mViewWidth : -mViewWidth)
.alpha(0)
.setDuration(mAnimationTime)
.setListener(null);
mContainerView.animate()
.alpha(0)
.setDuration(mAnimationTime)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
performDismiss();
performDismiss(view);
}
});
} else if (mSwiping) {
// cancel
mView.animate()
view.animate()
.translationX(0)
.alpha(1)
.setDuration(mAnimationTime)
.setListener(null);
mContainerView.animate()
.alpha(1)
.setDuration(mAnimationTime);
}
if (mVelocityTracker != null) {
mVelocityTracker.recycle();
Expand All @@ -181,11 +188,14 @@ public void onAnimationEnd(Animator animation) {
break;
}

mView.animate()
view.animate()
.translationX(0)
.alpha(1)
.setDuration(mAnimationTime)
.setListener(null);
mContainerView.animate()
.alpha(1)
.setDuration(mAnimationTime);
mVelocityTracker.recycle();
mVelocityTracker = null;
mTranslationX = 0;
Expand All @@ -207,25 +217,27 @@ public void onAnimationEnd(Animator animation) {
mSwiping = true;
mSwipingSlop = (deltaX > 0 ? mSlop : -mSlop);

if(mView.getParent() != null) {
mView.getParent().requestDisallowInterceptTouchEvent(true);
if(view.getParent() != null) {
view.getParent().requestDisallowInterceptTouchEvent(true);
}

// Cancel listview's touch
MotionEvent cancelEvent = MotionEvent.obtain(motionEvent);
cancelEvent.setAction(MotionEvent.ACTION_CANCEL |
(motionEvent.getActionIndex() <<
MotionEvent.ACTION_POINTER_INDEX_SHIFT));
mView.onTouchEvent(cancelEvent);
view.onTouchEvent(cancelEvent);
cancelEvent.recycle();
}

if (mSwiping) {
mTranslationX = deltaX;
mView.setTranslationX(deltaX - mSwipingSlop);
view.setTranslationX(deltaX - mSwipingSlop);
// TODO: use an ease-out interpolator or such
mView.setAlpha(Math.max(0f, Math.min(1f,
view.setAlpha(Math.max(0f, Math.min(1f,
1f - 2f * Math.abs(deltaX) / mViewWidth)));
mContainerView.setAlpha(Math.max(0.2f, Math.min(1f,
1f - Math.abs(deltaX) / mViewWidth)));
return true;
}
break;
Expand All @@ -234,7 +246,7 @@ public void onAnimationEnd(Animator animation) {
return false;
}

private void performDismiss() {
mCallbacks.onDismiss(mView, mToken);
private void performDismiss(View view) {
mCallbacks.onDismiss(view, mToken);
}
}
1 change: 1 addition & 0 deletions lib/src/main/res/layout/sb__template.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
android:layout_height="3dp" />

<LinearLayout
android:id="@+id/sb__inner"
android:layout_height="wrap_content"
android:layout_width="match_parent">

Expand Down

0 comments on commit 40674e7

Please sign in to comment.