Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reflect swipe velocity in swipe animation #230

Merged
merged 3 commits into from
Apr 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.yuyakaido.android.cardstackview;

public enum Duration {
Fast(100),
Normal(200),
Slow(500);

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 @@ -38,7 +38,7 @@ public Interpolator getInterpolator() {

public static class Builder {
private Direction direction = Direction.Bottom;
private int duration = 200;
private int duration = Duration.Normal.duration;
private Interpolator interpolator = new DecelerateInterpolator();

public RewindAnimationSetting.Builder setDirection(Direction direction) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public Interpolator getInterpolator() {

public static class Builder {
private Direction direction = Direction.Right;
private int duration = 200;
private int duration = Duration.Normal.duration;
private Interpolator interpolator = new AccelerateInterpolator();

public Builder setDirection(Direction direction) {
Expand Down
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