Skip to content

Instantly share code, notes, and snippets.

@panpf
Created October 12, 2016 08:17
Show Gist options
  • Save panpf/998566acf395da65fd8cf677bd5f54ed to your computer and use it in GitHub Desktop.
Save panpf/998566acf395da65fd8cf677bd5f54ed to your computer and use it in GitHub Desktop.
偷看ViewPager下一页动画
/**
* 偷看ViewPager下一页动画
*/
public class PagerPeepAnimation extends Animation {
private ViewPager viewPager;
private AnimationListener wrapperAnimationListener;
private int peepWidth;
public PagerPeepAnimation(final ViewPager viewPager) {
this.viewPager = viewPager;
this.peepWidth = (int) ((10 * viewPager.getResources().getDisplayMetrics().density) + 0.5);
super.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
viewPager.beginFakeDrag();
if (wrapperAnimationListener != null) {
wrapperAnimationListener.onAnimationStart(animation);
}
}
@Override
public void onAnimationRepeat(Animation animation) {
if (wrapperAnimationListener != null) {
wrapperAnimationListener.onAnimationRepeat(animation);
}
}
@Override
public void onAnimationEnd(Animation animation) {
if (viewPager.isFakeDragging()) {
viewPager.endFakeDrag();
}
if (wrapperAnimationListener != null) {
wrapperAnimationListener.onAnimationEnd(animation);
}
}
});
}
@SuppressWarnings("unused")
public void setPeepWidth(int peepWidth) {
this.peepWidth = peepWidth;
}
@Override
public void setAnimationListener(AnimationListener listener) {
this.wrapperAnimationListener = listener;
}
@Override
public void initialize(int width, int height, int parentWidth, int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
setDuration(500);
setInterpolator(new LinearInterpolator());
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
if (viewPager == null || !viewPager.isFakeDragging()) {
return;
}
float preResult = (float) ((Math.atan(10 * (interpolatedTime - 0.5)) + Math.PI / 2f) * 2f / Math.PI);
if (preResult <= 1) {
viewPager.fakeDragBy(-peepWidth * preResult);
} else {
viewPager.fakeDragBy(peepWidth * (preResult - 2));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment