Last active
September 10, 2015 05:50
-
-
Save johncordeiro/21f8154c8420598bf935 to your computer and use it in GitHub Desktop.
PagerTransformer for Parallax views.
This file contains 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
import android.support.v4.view.ViewPager; | |
import android.view.View; | |
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* Parallax transformer for ViewPagers that let you set different parallax | |
* effects for each view in your Fragments. | |
* | |
* Created by Marcos Trujillo (#^.^#) on 1/10/14. | |
*/ | |
public class ParallaxPageTransformer implements ViewPager.PageTransformer { | |
private List<ParallaxTransformInformation> mViewsToParallax = new ArrayList<ParallaxTransformInformation>(); | |
public ParallaxPageTransformer() { | |
} | |
public ParallaxPageTransformer(List<ParallaxTransformInformation> viewsToParallax) { | |
mViewsToParallax = viewsToParallax; | |
} | |
public ParallaxPageTransformer addViewToParallax(ParallaxTransformInformation viewInfo) { | |
if (mViewsToParallax != null) { | |
mViewsToParallax.add(viewInfo); | |
} | |
return this; | |
} | |
public void transformPage(View view, float position) { | |
int pageWidth = view.getWidth(); | |
if (position < -1) { | |
// This page is way off-screen to the left. | |
view.setAlpha(0); | |
} else if (position <= 1 && mViewsToParallax != null) { // [-1,1] | |
view.setAlpha(1); | |
for (ParallaxTransformInformation parallaxTransformInformation : mViewsToParallax) { | |
applyParallaxEffect(view, position, pageWidth, parallaxTransformInformation, | |
position >= 0); | |
} | |
} else { | |
// This page is way off-screen to the right. | |
view.setAlpha(0); | |
} | |
} | |
private void applyParallaxEffect(View view, float position, int pageWidth, | |
ParallaxTransformInformation information, boolean isEnter) { | |
if (information.isValid()) { | |
View viewFromResource = getViewFromResource(view, information); | |
if(viewFromResource != null) { | |
if (isEnter && !information.isEnterDefault()) { | |
viewFromResource | |
.setTranslationX(-position * (pageWidth / information.parallaxEnterEffect)); | |
} else if (!isEnter && !information.isExitDefault()) { | |
viewFromResource | |
.setTranslationX(-position * (pageWidth / information.parallaxExitEffect)); | |
} | |
} | |
} | |
} | |
private View getViewFromResource(View view, ParallaxTransformInformation information) { | |
View viewFromResource; | |
Object tag = view.getTag(information.resource); | |
if(tag != null) { | |
viewFromResource = (View) tag; | |
} else { | |
viewFromResource = view.findViewById(information.resource); | |
view.setTag(information.resource, viewFromResource); | |
} | |
return viewFromResource; | |
} | |
/** | |
* Information to make the parallax effect in a concrete view. | |
* | |
* parallaxEffect positive values reduces the speed of the view in the translation | |
* ParallaxEffect negative values increase the speed of the view in the translation | |
* Try values to see the different effects. I recommend 2, 0.75 and 0.5 | |
*/ | |
public static class ParallaxTransformInformation { | |
public static final float PARALLAX_EFFECT_DEFAULT = -101.1986f; | |
int resource = -1; | |
float parallaxEnterEffect = 1f; | |
float parallaxExitEffect = 1f; | |
public ParallaxTransformInformation(int resource, float parallaxEnterEffect, | |
float parallaxExitEffect) { | |
this.resource = resource; | |
this.parallaxEnterEffect = parallaxEnterEffect; | |
this.parallaxExitEffect = parallaxExitEffect; | |
} | |
public boolean isValid() { | |
return parallaxEnterEffect != 0 && parallaxExitEffect != 0 && resource != -1; | |
} | |
public boolean isEnterDefault() { | |
return parallaxEnterEffect == PARALLAX_EFFECT_DEFAULT; | |
} | |
public boolean isExitDefault() { | |
return parallaxExitEffect == PARALLAX_EFFECT_DEFAULT; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment