Skip to content

Instantly share code, notes, and snippets.

@lzanita09
Created May 10, 2014 15:08
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save lzanita09/3b717464e981e02be137 to your computer and use it in GitHub Desktop.
Save lzanita09/3b717464e981e02be137 to your computer and use it in GitHub Desktop.
Custom PageTransformer simulates Yahoo News Digest ViewPager animation.
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);
}
}
}
@peppegiuseppe
Copy link

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

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