Skip to content

Instantly share code, notes, and snippets.

@phileo
Created December 12, 2019 02:20
Show Gist options
  • Save phileo/bc7af6d7ff0ee2e3f61529b239b7ec32 to your computer and use it in GitHub Desktop.
Save phileo/bc7af6d7ff0ee2e3f61529b239b7ec32 to your computer and use it in GitHub Desktop.
DepthPageTransformer
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