Skip to content

Instantly share code, notes, and snippets.

@delacrixmorgan
Last active November 21, 2021 09:45
Show Gist options
  • Save delacrixmorgan/6b05ab74d1acb01b471fcc3151d70703 to your computer and use it in GitHub Desktop.
Save delacrixmorgan/6b05ab74d1acb01b471fcc3151d70703 to your computer and use it in GitHub Desktop.
Cube ViewPager Transformer

Cube ViewPager Transformer

class CubeTransformer : ViewPager.PageTransformer {
    override fun transformPage(view: View, position: Float) {
        val deltaY = 0.5F

        view.pivotX = if (position < 0F) view.width.toFloat() else 0F
        view.pivotY = view.height * deltaY
        view.rotationY = 45F * position
    }
}
@thebradness
Copy link

Thank you! This is so simple and works beautifully. (I couldn't find this anywhere else.)
Here it is with ViewPager2 and Java for anyone looking for it:

public class CubeTransformer implements ViewPager2.PageTransformer {
    @Override
    public void transformPage(@NonNull View page, float position) {
        float deltaY = 0.5f;
        float pivotX = position < 0f ? page.getWidth() : 0f;
        float pivotY = page.getHeight() * deltaY;
        float rotationY = 45f * position;

        page.setPivotX(pivotX);
        page.setPivotY(pivotY);
        page.setRotationY(rotationY);
    }
}

And then in your activity:

ViewPager2 vp = findViewById(R.id.swipe_frame);
vp.setAdapter(new SwipeAdapter());
vp.setPageTransformer(new CubeTransformer());

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