Created
January 19, 2016 23:51
-
-
Save deepak786/c50829f0a7861dcc1036 to your computer and use it in GitHub Desktop.
Vertical View Pager with Depth Page transformation
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
import android.content.Context; | |
import android.support.v4.view.ViewPager; | |
import android.util.AttributeSet; | |
import android.view.MotionEvent; | |
import android.view.View; | |
public class VerticalViewPager extends ViewPager { | |
public VerticalViewPager(Context context) { | |
super(context); | |
init(); | |
} | |
public VerticalViewPager(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
init(); | |
} | |
private void init() { | |
// The majority of the magic happens here | |
setPageTransformer(true, new VerticalPageTransformer()); | |
// The easiest way to get rid of the overscroll drawing that happens on the left and right | |
setOverScrollMode(OVER_SCROLL_NEVER); | |
} | |
private class VerticalPageTransformer implements ViewPager.PageTransformer { | |
private static final float MIN_SCALE = 0.75f; | |
@Override | |
public void transformPage(View view, float position) { | |
if (position < -1) { // [-Infinity,-1) | |
// This page is way off-screen to the left. | |
view.setAlpha(0); | |
} else if (position <= 0) { // [-1,0] | |
// Use the default slide transition when moving to the left page | |
view.setAlpha(1); | |
// Counteract the default slide transition | |
view.setTranslationX(view.getWidth() * -position); | |
//set Y position to swipe in from top | |
float yPosition = position * view.getHeight(); | |
view.setTranslationY(yPosition); | |
view.setScaleX(1); | |
view.setScaleY(1); | |
} else if (position <= 1) { // [0,1] | |
view.setAlpha(1 - position); | |
// Counteract the default slide transition | |
view.setTranslationX(view.getWidth() * -position); | |
// Scale the page down (between MIN_SCALE and 1) | |
float scaleFactor = MIN_SCALE | |
+ (1 - MIN_SCALE) * (1 - Math.abs(position)); | |
view.setScaleX(scaleFactor); | |
view.setScaleY(scaleFactor); | |
} else { // (1,+Infinity] | |
// This page is way off-screen to the right. | |
view.setAlpha(0); | |
} | |
} | |
} | |
/** | |
* Swaps the X and Y coordinates of your touch event. | |
*/ | |
private MotionEvent swapXY(MotionEvent ev) { | |
float width = getWidth(); | |
float height = getHeight(); | |
float newX = (ev.getY() / height) * width; | |
float newY = (ev.getX() / width) * height; | |
ev.setLocation(newX, newY); | |
return ev; | |
} | |
@Override | |
public boolean onInterceptTouchEvent(MotionEvent ev) { | |
boolean intercepted = super.onInterceptTouchEvent(swapXY(ev)); | |
swapXY(ev); // return touch coordinates to original reference frame for any child views | |
return intercepted; | |
} | |
@Override | |
public boolean onTouchEvent(MotionEvent ev) { | |
return super.onTouchEvent(swapXY(ev)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment