Created
September 22, 2016 22:19
-
-
Save cesco89/69cd31abb8a9289b28a7377fe67bd640 to your computer and use it in GitHub Desktop.
Crossfade effect in Android ViewPager
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Credit goes to http://fiskur.eu/?p=752 | |
public class CrossfadePageTransformer implements ViewPager.PageTransformer { | |
@Override | |
public void transformPage(View page, float position) { | |
int pageWidth = page.getWidth(); | |
View backgroundView = page.findViewById(R.id.background); | |
View text = page.findViewById(R.id.content); | |
View phone = page.findViewById(R.id.phone); | |
View map = page.findViewById(R.id.map); | |
View mountain = page.findViewById(R.id.mountain); | |
View mountainNight = page.findViewById(R.id.mountain_night); | |
View rain = page.findViewById(R.id.rain); | |
View hands = page.findViewById(R.id.screenshot); | |
if (position <= 1) { | |
page.setTranslationX(pageWidth * -position); | |
} | |
if(position <= -1.0f || position >= 1.0f) { | |
} else if( position == 0.0f ) { | |
} else { | |
if(backgroundView != null) { | |
backgroundView.setAlpha(1.0f - Math.abs(position)); | |
} | |
//Text both translates in/out and fades in/out | |
if (text != null) { | |
text.setTranslationX(pageWidth * position); | |
text.setAlpha(1.0f - Math.abs(position)); | |
} | |
//Map + phone - map simple translate, phone parallax effect | |
if(map != null){ | |
map.setTranslationX(pageWidth * position); | |
} | |
if(phone != null){ | |
phone.setTranslationX((float)(pageWidth/1.2 * position)); | |
} | |
//Mountain day - fade in/out | |
if(mountain != null){ | |
mountain.setAlpha(1.0f - Math.abs(position)); | |
} | |
//Mountain night - fade in, but translate out, rain fades in but parallax translate out | |
if(mountainNight != null){ | |
if(position < 0){ | |
mountainNight.setTranslationX(pageWidth * position); | |
}else{ | |
mountainNight.setAlpha(1.0f - Math.abs(position)); | |
} | |
} | |
if(rain != null){ | |
if(position < 0){ | |
rain.setTranslationX((float)(pageWidth/1.2 * position)); | |
}else{ | |
rain.setAlpha(1.0f - Math.abs(position)); | |
} | |
} | |
//Long click device + hands - translate both way but only fade out | |
if(hands != null){ | |
hands.setTranslationX(pageWidth * position); | |
if(position < 0) { | |
hands.setAlpha(1.0f - Math.abs(position)); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment