From def8389960696562b202b6d70453f3c3b4c03754 Mon Sep 17 00:00:00 2001 From: Heinrich Reimer Date: Sat, 25 Jun 2016 00:44:30 +0200 Subject: [PATCH] Behavior change: page scroll change now reacts to the distance to scroll to implement https://material.google.com/motion/duration-easing.html#duration-easing-dynamic-durations (check https://github.com/HeinrichReimer/material-intro/blob/master/library/src/main/java/com/heinrichreimersoftware/materialintro/app/IntroActivity.java#L343 for the exact formula) Fixed bug when switching pages before the current page change animation is finished --- .../demo/CanteenIntroActivity.java | 2 +- .../materialintro/app/IntroActivity.java | 29 ++++++++++++++----- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/com/heinrichreimersoftware/materialintro/demo/CanteenIntroActivity.java b/app/src/main/java/com/heinrichreimersoftware/materialintro/demo/CanteenIntroActivity.java index 3a90ae1..6ef48f9 100644 --- a/app/src/main/java/com/heinrichreimersoftware/materialintro/demo/CanteenIntroActivity.java +++ b/app/src/main/java/com/heinrichreimersoftware/materialintro/demo/CanteenIntroActivity.java @@ -24,7 +24,7 @@ protected void onCreate(Bundle savedInstanceState) { label.setSpan(labelSpan, 0, label.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); setButtonCtaLabel(label); - setPageScrollDuration(800); + setPageScrollDuration(500); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { setPagerInterpolator(android.R.interpolator.fast_out_slow_in); } diff --git a/library/src/main/java/com/heinrichreimersoftware/materialintro/app/IntroActivity.java b/library/src/main/java/com/heinrichreimersoftware/materialintro/app/IntroActivity.java index 44295eb..c8f7172 100644 --- a/library/src/main/java/com/heinrichreimersoftware/materialintro/app/IntroActivity.java +++ b/library/src/main/java/com/heinrichreimersoftware/materialintro/app/IntroActivity.java @@ -287,6 +287,9 @@ public void onClick(View v) { } private void smoothScrollPagerTo(final int position) { + if (pager.isFakeDragging()) + return; + ValueAnimator animator = ValueAnimator.ofFloat(pager.getCurrentItem(), position); animator.addListener(new AnimatorListenerAdapter() { @Override @@ -329,11 +332,17 @@ else if (position < currentPosition && Math.ceil(position) != currentPosition && } }); + int distance = Math.abs(position - pager.getCurrentItem()); + animator.setInterpolator(pageScrollInterpolator); - animator.setDuration(pageScrollDuration); + animator.setDuration(calculateScrollDuration(distance)); animator.start(); } + private long calculateScrollDuration(int distance) { + return Math.round(pageScrollDuration * (distance + Math.sqrt(distance)) / 2); + } + public void nextSlide() { int currentItem = pager.getCurrentItem(); if (currentItem > adapter.getCount() - 1) finishIfNeeded(); @@ -343,16 +352,15 @@ public void nextSlide() { } else { AnimUtils.applyShakeAnimation(this, buttonNext); - } } - private boolean nextSlideAuto() { + private int nextSlideAuto() { int endPosition = pager.getCurrentItem(); int count = getCount(); if (count == 1) { - return false; + return 0; } else if (pager.getCurrentItem() >= count - 1) { while (endPosition >= 0 && canGoBackward(endPosition, true)) { @@ -365,12 +373,16 @@ else if (canGoForward(endPosition, true)) { endPosition++; } + int distance = Math.abs(endPosition - pager.getCurrentItem()); + if (endPosition == pager.getCurrentItem()) - return false; + return 0; smoothScrollPagerTo(endPosition); - return autoplayCounter != 0; + if (autoplayCounter == 0) + return 0; + return distance; } @@ -793,8 +805,9 @@ public void run() { cancelAutoplay(); return; } - if (nextSlideAuto()) - autoplayHandler.postDelayed(autoplayCallback, autoplayDelay); + int distance = nextSlideAuto(); + if (distance != 0) + autoplayHandler.postDelayed(autoplayCallback, autoplayDelay + calculateScrollDuration(distance)); } }; autoplayHandler.postDelayed(autoplayCallback, autoplayDelay);