Created
December 12, 2019 02:20
-
-
Save phileo/bc7af6d7ff0ee2e3f61529b239b7ec32 to your computer and use it in GitHub Desktop.
DepthPageTransformer
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
class DepthPageTransformer: ViewPager.PageTransformer { | |
companion object { | |
private const val MIN_SCALE = 0.75f | |
} | |
override fun transformPage(view: View, position: Float) { | |
when { | |
position < -1 -> // [-Infinity,-1) | |
// This page is way off-screen to the left. | |
view.alpha = 0f | |
position <= 0 -> // [-1,0] | |
// Use the default slide transition when moving to the left page | |
view.apply { | |
alpha = 1f | |
translationX = 0f | |
scaleX = 1f | |
scaleY = 1f | |
} | |
position <= 1 -> // (0,1] | |
view.apply { | |
// Fade the page out. | |
alpha = 1 - position | |
// Counteract the default slide transition | |
translationX = width * -position | |
// Scale the page down (between MIN_SCALE and 1) | |
val scaleFactor = MIN_SCALE + (1 - MIN_SCALE) * (1 - Math.abs(position)) | |
scaleX = scaleFactor | |
scaleY = scaleFactor | |
} | |
else -> // (1,+Infinity] | |
// This page is way off-screen to the right. | |
view.alpha = 0f | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment