Last active
November 15, 2017 13:52
-
-
Save EmmanuelGuther/5ac219daaa443f55d56376df232b1a8d to your computer and use it in GitHub Desktop.
Android PageTransformer KOTLIN to use with 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
//Use like: vPager.setPageTransformer(true, ParallaxPageTransformer) | |
class ParallaxPageTransformer : ViewPager.PageTransformer { | |
override fun transformPage(view: View, position: Float) { | |
val pageWidth = view.width | |
when { | |
position < -1 -> view.alpha = 1f | |
position <= 1 -> dummyImageView.setTranslationX(-position * (pageWidth / 2)) //Half the normal speed | |
else -> view.alpha = 1f | |
} | |
} | |
} |
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
fun transformPage(view: View, position: Float) { | |
val pageWidth = view.getWidth() | |
if (position < -1) { // [-Infinity,-1) | |
// This page is way off-screen to the left. | |
view.setAlpha(0) | |
} else if (position <= 1) { // [-1,1] | |
mBlur.setTranslationX(((-(1 - position)).toDouble() * 0.5 * pageWidth.toDouble()).toFloat()) | |
mBlurLabel.setTranslationX(((-(1 - position)).toDouble() * 0.5 * pageWidth.toDouble()).toFloat()) | |
mDim.setTranslationX((-(1 - position) * pageWidth).toFloat()) | |
mDimLabel.setTranslationX((-(1 - position) * pageWidth).toFloat()) | |
mCheck.setTranslationX(((-(1 - position)).toDouble() * 1.5 * pageWidth.toDouble()).toFloat()) | |
mDoneButton.setTranslationX(((-(1 - position)).toDouble() * 1.7 * pageWidth.toDouble()).toFloat()) | |
// The 0.5, 1.5, 1.7 values you see here are what makes the view move in a different speed. | |
// The bigger the number, the faster the view will translate. | |
// The result float is preceded by a minus because the views travel in the opposite direction of the movement. | |
mFirstColor.setTranslationX(position * (pageWidth / 4)) | |
mSecondColor.setTranslationX(position * (pageWidth / 1)) | |
mTint.setTranslationX(position * (pageWidth / 2)) | |
mDesaturate.setTranslationX(position * (pageWidth / 1)) | |
// This is another way to do it | |
} else { // (1,+Infinity] | |
// This page is way off-screen to the right. | |
view.setAlpha(0) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment