Skip to content

Commit

Permalink
#229 Reflect swipe velocity in swipe animation
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyakaido committed Apr 2, 2019
1 parent 6ace6c3 commit 3d4c9bb
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.yuyakaido.android.cardstackview;

public enum Duration {
Fast(50),
Normal(200),
Slow(400);

public final int duration;

Duration(int duration) {
this.duration = duration;
}

public static Duration fromVelocity(int velocity) {
if (velocity < 1000) {
return Slow;
} else if (velocity < 5000) {
return Normal;
}
return Fast;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@
import android.view.View;

import com.yuyakaido.android.cardstackview.CardStackLayoutManager;
import com.yuyakaido.android.cardstackview.Duration;
import com.yuyakaido.android.cardstackview.SwipeAnimationSetting;

public class CardStackSnapHelper extends SnapHelper {

private int velocityX = 0;
private int velocityY = 0;

@Nullable
@Override
public int[] calculateDistanceToFinalSnap(
Expand All @@ -25,10 +30,22 @@ public int[] calculateDistanceToFinalSnap(
CardStackSetting setting = manager.getCardStackSetting();
float horizontal = Math.abs(x) / (float) targetView.getWidth();
float vertical = Math.abs(y) / (float) targetView.getHeight();
if (setting.swipeThreshold < horizontal || setting.swipeThreshold < vertical) {
Duration duration = Duration.fromVelocity(velocityY < velocityX ? velocityX : velocityY);
if (duration == Duration.Fast || setting.swipeThreshold < horizontal || setting.swipeThreshold < vertical) {
CardStackState state = manager.getCardStackState();
if (setting.directions.contains(state.getDirection())) {
state.targetPosition = state.topPosition + 1;

SwipeAnimationSetting swipeAnimationSetting = new SwipeAnimationSetting.Builder()
.setDirection(setting.swipeAnimationSetting.getDirection())
.setDuration(duration.duration)
.setInterpolator(setting.swipeAnimationSetting.getInterpolator())
.build();
manager.setSwipeAnimationSetting(swipeAnimationSetting);

this.velocityX = 0;
this.velocityY = 0;

CardStackSmoothScroller scroller = new CardStackSmoothScroller(CardStackSmoothScroller.ScrollType.ManualSwipe, manager);
scroller.setTargetPosition(manager.getTopPosition());
manager.startSmoothScroll(scroller);
Expand All @@ -43,7 +60,6 @@ public int[] calculateDistanceToFinalSnap(
manager.startSmoothScroll(scroller);
}
}

}
}
return new int[2];
Expand Down Expand Up @@ -73,6 +89,8 @@ public int findTargetSnapPosition(
int velocityX,
int velocityY
) {
this.velocityX = Math.abs(velocityX);
this.velocityY = Math.abs(velocityY);
if (layoutManager instanceof CardStackLayoutManager) {
CardStackLayoutManager manager = (CardStackLayoutManager) layoutManager;
return manager.getTopPosition();
Expand Down

0 comments on commit 3d4c9bb

Please sign in to comment.