Created
May 10, 2014 15:08
-
-
Save lzanita09/3b717464e981e02be137 to your computer and use it in GitHub Desktop.
Custom PageTransformer simulates Yahoo News Digest ViewPager animation.
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; | |
/** | |
* Custom PageTransformer simulates Yahoo News Digest ViewPager animation. | |
* | |
* | |
* Created by zhelu on 2/27/14. | |
*/ | |
public class ParallaxViewTransformer implements ViewPager.PageTransformer { | |
private int mImageId; | |
/** | |
* The constructor takes a target Id as param, later on we'll use the Id to get the target | |
* view and animate only that view instead of the entire page. | |
* | |
* @param mImageId Target View ID. | |
*/ | |
public ParallaxViewTransformer(int mImageId) { | |
this.mImageId = mImageId; | |
} | |
@Override | |
public void transformPage(View view, float position) { | |
int pageWidth = view.getWidth(); | |
// Find target View. | |
if (view.findViewById(mImageId) != null) { | |
view = view.findViewById(mImageId); | |
} | |
if (position <= 0) { // [-1,0] | |
if (view.getId() == mImageId) { | |
view.setTranslationX(pageWidth * -position / 1.4f); | |
} else { | |
// Use the default slide transition when moving to the left page if the target view | |
// is not found. | |
view.setTranslationX(0); | |
} | |
} else if (position <= 1) { // (0,1] | |
// Counteract the default slide transition | |
view.setTranslationX(pageWidth * -position / 1.4f); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this!
Where i have to put this code? In the activity which contains my viewpager? And how have i to use mImageId? Thanks