Android PageTransformer

JellyBean 4.2 added a lovely little callback to the ViewPager class called the PageTransformer. (Also available in the Compatibility Lib 11)

Big deal! you may mutter? Look at this:
GreenTree on Moupp – Page fades in

So the page from the right fades in and the page going left fades out! Really simple, but nice effect. Works nicely with picture pagers. Code? Sure!

mViewPager.setPageTransformer(false, new PageTransformer()
    public void transformPage(View v, float pos)
        final float invt = Math.abs(Math.abs(pos) - 1);

Lovely bit of code, we normalise the float coming in (so its always 0 to 1) then we apply that to the View.setAlpha(float) method. This works with any of the View transformation methods! (Alpha,setX/Y, rotateX/Y).

You can of course get a bit silly with this! So please be sensible, I don’t want to be seeing 360 spins on page transitions… (yeah I’ve tried!)

Update: Damian Flannery made this neat little transition

comments powered by Disqus