Skip to content

Commit

Permalink
Added option to control page scroll speed and interpolator
Browse files Browse the repository at this point in the history
  • Loading branch information
heinrichreimer committed Jun 24, 2016
1 parent a76e350 commit 803c227
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ protected void onCreate(Bundle savedInstanceState) {
label.setSpan(labelSpan, 0, label.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
setButtonCtaLabel(label);

setPagerScrollDuration(800);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
setPagerInterpolator(android.R.interpolator.fast_out_slow_in);
}

addSlide(new SimpleSlide.Builder()
.title(R.string.title_canteen_intro1)
.description(R.string.description_canteen_intro1)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.heinrichreimersoftware.materialintro.app;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ArgbEvaluator;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.content.res.ColorStateList;
Expand All @@ -18,6 +21,7 @@
import android.support.annotation.ColorRes;
import android.support.annotation.IntDef;
import android.support.annotation.IntRange;
import android.support.annotation.InterpolatorRes;
import android.support.annotation.NonNull;
import android.support.annotation.StringRes;
import android.support.v4.app.Fragment;
Expand All @@ -33,6 +37,8 @@
import android.util.TypedValue;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AnimationUtils;
import android.view.animation.Interpolator;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
Expand Down Expand Up @@ -122,10 +128,16 @@ public class IntroActivity extends AppCompatActivity {
private int autoplayCounter;
private long autoplayDelay;

private Interpolator pagerInterpolator;
private long pagerScrollDuration;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

pagerInterpolator = AnimationUtils.loadInterpolator(this, android.R.interpolator.accelerate_decelerate);
pagerScrollDuration = getResources().getInteger(android.R.integer.config_shortAnimTime);

if (savedInstanceState != null) {
if (savedInstanceState.containsKey(KEY_CURRENT_ITEM)) {
position = savedInstanceState.getInt(KEY_CURRENT_ITEM, position);
Expand Down Expand Up @@ -254,7 +266,7 @@ private void findViews(){

pager.setAdapter(adapter);
pager.addOnPageChangeListener(listener);
pager.setCurrentItem(position);
pager.setCurrentItem(position, false);

pagerIndicator.setViewPager(pager);

Expand All @@ -274,12 +286,60 @@ public void onClick(View v) {
CheatSheet.setup(buttonBack);
}

private void smoothScrollPagerTo(final int position) {
ValueAnimator animator = ValueAnimator.ofFloat(pager.getCurrentItem(), position);
animator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
pager.endFakeDrag();
pager.setCurrentItem(position);
}

@Override
public void onAnimationCancel(Animator animation) {
pager.endFakeDrag();
}
});
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float position = (Float) animation.getAnimatedValue();

fakeDragToPosition(position);
}

private boolean fakeDragToPosition(float position) {
// The following mimics the underlying calculations in ViewPager
float scrollX = pager.getScrollX();
int pagerWidth = pager.getWidth();
int currentPosition = pager.getCurrentItem();

if (position > currentPosition && Math.floor(position) != currentPosition && position % 1 != 0) {
pager.setCurrentItem((int) Math.floor(position), false);
}
else if (position < currentPosition && Math.ceil(position) != currentPosition && position % 1 != 0) {
pager.setCurrentItem((int) Math.ceil(position), false);
}

if (!pager.isFakeDragging() && !pager.beginFakeDrag())
return false;

pager.fakeDragBy(scrollX - pagerWidth * position);
return true;
}
});

animator.setInterpolator(pagerInterpolator);
animator.setDuration(pagerScrollDuration);
animator.start();
}

public void nextSlide() {
int currentItem = pager.getCurrentItem();
if (currentItem > adapter.getCount() - 1) finishIfNeeded();

if (canGoForward(currentItem, true)) {
pager.setCurrentItem(++currentItem, true);
smoothScrollPagerTo(++currentItem);
}
else {
AnimUtils.applyShakeAnimation(this, buttonNext);
Expand Down Expand Up @@ -308,7 +368,7 @@ else if (canGoForward(endPosition, true)) {
if (endPosition == pager.getCurrentItem())
return false;

pager.setCurrentItem(endPosition);
smoothScrollPagerTo(endPosition);

return autoplayCounter != 0;

Expand All @@ -319,7 +379,7 @@ public void previousSlide() {
if (currentItem <= 0) return;

if (canGoBackward(currentItem, true)) {
pager.setCurrentItem(--currentItem, true);
smoothScrollPagerTo(--currentItem);
}
else {
AnimUtils.applyShakeAnimation(this, buttonBack);
Expand All @@ -334,7 +394,7 @@ private void performButtonBackPress() {
while (endPosition < count && canGoForward(endPosition, true)) {
endPosition++;
}
pager.setCurrentItem(endPosition);
smoothScrollPagerTo(endPosition);
} else if (buttonBackFunction == BUTTON_BACK_FUNCTION_BACK) {
previousSlide();
}
Expand Down Expand Up @@ -768,6 +828,26 @@ public boolean isAutoplaying() {
return autoplayCallback != null;
}

public long getPagerScrollDuration() {
return pagerScrollDuration;
}

public void setPagerScrollDuration(@IntRange(from = 1) long pagerScrollDuration) {
this.pagerScrollDuration = pagerScrollDuration;
}

public Interpolator getPagerInterpolator() {
return pagerInterpolator;
}

public void setPagerInterpolator(Interpolator pagerInterpolator) {
this.pagerInterpolator = pagerInterpolator;
}

public void setPagerInterpolator(@InterpolatorRes int interpolatorRes) {
this.pagerInterpolator = AnimationUtils.loadInterpolator(this, interpolatorRes);
}

@SuppressWarnings("unused")
public boolean isFullscreen() {
return fullscreen;
Expand Down Expand Up @@ -1122,7 +1202,7 @@ public void onClick(View v) {
while (endPosition < count && canGoForward(endPosition, true)) {
endPosition++;
}
pager.setCurrentItem(endPosition);
smoothScrollPagerTo(endPosition);
}
}
}

0 comments on commit 803c227

Please sign in to comment.