Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
class PageTransformer : ViewPager2.PageTransformer {
companion object {
private const val SCALE = 0.9f
}
override fun transformPage(view: View, position: Float) {
val translationOffset = calculateTranslationOffset(view, position)
with(view) {
when {
position < -2.5 -> {
alpha = 0f
scaleX = 1f
scaleY = 1f
}
position < -1 -> {
alpha = 1f
translationY = translationOffset
setScale(this, -1f)
}
position <= 0 -> {
translationY = translationOffset
setScale(this, position)
}
position > 0.9 -> {
alpha = 0f
translationY = translationOffset
}
position > 0 -> {
alpha = 1f
translationY = translationOffset
}
}
}
}
private fun setScale(view: View, position: Float) {
val scale = (SCALE + (1 - SCALE) * (1 - Math.abs(position)))
view.scaleY = scale
view.scaleX = scale
}
private fun calculateTranslationOffset(view: View, position: Float): Float {
val additionalOffsetScale = StrictMath.pow(1.0 / 10.0, -position.toDouble())
val additionalOffset = additionalOffsetScale * (view.height * 0.1)
return (additionalOffset + (position * -view.height * SCALE)).toFloat()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment