Skip to content

Instantly share code, notes, and snippets.

@0xSG
Created May 6, 2019 08:37
Show Gist options
  • Save 0xSG/2245b9f4d59aadd7f190436e0d174f27 to your computer and use it in GitHub Desktop.
Save 0xSG/2245b9f4d59aadd7f190436e0d174f27 to your computer and use it in GitHub Desktop.
smooth animation of a ViewPager on start of the activity. (in case you can't or won't use ViewPagerIndicator to announce the presence a ViewPager)
@Override
public void onStart() {
super.onStart();
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(new PagerHintMovement(-10), "progress", -1f, 1f);
objectAnimator.setInterpolator(new AccelerateInterpolator());
objectAnimator.setDuration(500);
objectAnimator.setRepeatCount(3);
objectAnimator.setRepeatMode(ValueAnimator.REVERSE);
objectAnimator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
Log.d(TAG, "Animation: Starting fake drag on view pager.");
mHeroDetailPager.beginFakeDrag();
}
@Override
public void onAnimationEnd(Animator animation) {
Log.d(TAG, "Animation: Ending fake drag on view pager.");
mHeroDetailPager.endFakeDrag();
mHeroDetailPager.setCurrentItem(0);
}
@Override
public void onAnimationCancel(Animator animation) {
mHeroDetailPager.setCurrentItem(0);
}
@Override
public void onAnimationRepeat(Animator animation) {
if(animation.getInterpolator() instanceof AccelerateInterpolator){
animation.setInterpolator(new DecelerateInterpolator());
}else{
animation.setInterpolator(new AccelerateInterpolator());
}
}
});
objectAnimator.setStartDelay(2000);
objectAnimator.start();
}
class PagerHintMovement{
float goal;
float progress;
PagerHintMovement(float goal) {
this.goal = goal;
}
public float getProgress() {
return progress;
}
public void setProgress(float progress) {
this.progress = progress;
if(mHeroDetailPager.isFakeDragging()){
Log.d(TAG, "Animation: Updating fake drag with value="+ goal * progress+"px");
mHeroDetailPager.fakeDragBy( goal * progress);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment