Skip to content

Instantly share code, notes, and snippets.

@pyk
Last active April 9, 2016 16:32
Show Gist options
  • Save pyk/b7ab4dfa4f0f7d2e94f3 to your computer and use it in GitHub Desktop.
Save pyk/b7ab4dfa4f0f7d2e94f3 to your computer and use it in GitHub Desktop.
ViewPager.PageTransformer custom animation
/*
Usage:
viewPager.setPageTransformer(true, new CardTransformer(0.7f));
*/
public class CardTransformer implements PageTransformer {
private final float scalingStart;
public CardTransformer(float scalingStart) {
super();
this.scalingStart = 1 - scalingStart;
}
@Override
public void transformPage(View page, float position) {
if (position >= 0) {
final int w = page.getWidth();
float scaleFactor = 1 - scalingStart * position;
page.setAlpha(1 - position);
page.setScaleX(scaleFactor);
page.setScaleY(scaleFactor);
page.setTranslationX(w * (1 - position) - w);
}
}
}
@akashsoni143786
Copy link

in this code what we can define mBluler and mDimp

@OverRide
public void transformPage(View view, float position) {
int pageWidth = view.getWidth();

if (position < -1) { // [-Infinity,-1)
    // This page is way off-screen to the left.
    view.setAlpha(0);

} else if (position <= 1) { // [-1,1]
      view.findViewById(R.id.ViewPager2);

    mBlur.setTranslationX((float) (-(1 - position) * 0.5 * pageWidth));
    mBlurLabel.setTranslationX((float) (-(1 - position) * 0.5 * pageWidth));

    mDim.setTranslationX((float) (-(1 - position) * pageWidth));
    mDimLabel.setTranslationX((float) (-(1 - position) * pageWidth));

    mCheck.setTranslationX((float) (-(1 - position) * 1.5 * pageWidth));
    mDoneButton.setTranslationX((float) (-(1 - position) * 1.7 * pageWidth)); 
    // The 0.5, 1.5, 1.7 values you see here are what makes the view move in a different speed.
    // The bigger the number, the faster the view will translate.
    // The result float is preceded by a minus because the views travel in the opposite direction of the movement.

    mFirstColor.setTranslationX((position) * (pageWidth / 4));

    mSecondColor.setTranslationX((position) * (pageWidth / 1));

    mTint.setTranslationX((position) * (pageWidth / 2));

    mDesaturate.setTranslationX((position) * (pageWidth / 1));
    // This is another way to do it


} else { // (1,+Infinity]
    // This page is way off-screen to the right.
    view.setAlpha(0);
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment