Skip to content

Instantly share code, notes, and snippets.

@jayrambhia
Last active December 21, 2019 05:55
Show Gist options
  • Save jayrambhia/6066a732c3bf5ebde3b9a03da375cfa5 to your computer and use it in GitHub Desktop.
Save jayrambhia/6066a732c3bf5ebde3b9a03da375cfa5 to your computer and use it in GitHub Desktop.
ViewPager Cards
public class CardsPagerTransformerBasic implements ViewPager.PageTransformer {
private int baseElevation;
private int raisingElevation;
private float smallerScale;
public CardsPagerTransformerBasic(int baseElevation, int raisingElevation, float smallerScale) {
this.baseElevation = baseElevation;
this.raisingElevation = raisingElevation;
this.smallerScale = smallerScale;
}
@Override
public void transformPage(View page, float position) {
float absPosition = Math.abs(position);
if (absPosition >= 1) {
page.setElevation(baseElevation);
page.setScaleY(smallerScale);
} else {
// This will be during transformation
page.setElevation(((1 - absPosition) * raisingElevation + baseElevation));
page.setScaleY((smallerScale - 1) * absPosition + 1);
}
}
}
public class CardsPagerTransformerShift implements ViewPager.PageTransformer {
private int baseElevation;
private int raisingElevation;
private float smallerScale;
private float startOffset;
public CardsPagerTransformerShift(int baseElevation, int raisingElevation, float smallerScale, float startOffset) {
this.baseElevation = baseElevation;
this.raisingElevation = raisingElevation;
this.smallerScale = smallerScale;
this.startOffset = startOffset;
}
@Override
public void transformPage(View page, float position) {
float absPosition = Math.abs(position - startOffset);
if (absPosition >= 1) {
page.setElevation(baseElevation);
page.setScaleY(smallerScale);
} else {
// This will be during transformation
page.setElevation(((1 - absPosition) * raisingElevation + baseElevation));
page.setScaleY((smallerScale - 1) * absPosition + 1);
}
}
}
@coderspacedev
Copy link

how to use this code?

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