Skip to content

Instantly share code, notes, and snippets.

@phileo
Created December 12, 2019 02:19
Show Gist options
  • Save phileo/8ca2ac8ccfe4c68c2a236c2a73cc541c to your computer and use it in GitHub Desktop.
Save phileo/8ca2ac8ccfe4c68c2a236c2a73cc541c to your computer and use it in GitHub Desktop.
ZoomPageTransformer
class ZoomPageTransformer: ViewPager.PageTransformer {
companion object {
private const val MIN_SCALE = 0.85f
private const val MIN_ALPHA = 0.5f
}
override fun transformPage(view: View, position: Float) {
val pageWidth = view.width
val pageHeight = view.height
when {
position < -1 -> view.alpha = 0f
position <= 1 -> {
val scaleFactor = Math.max(MIN_SCALE, 1 - Math.abs(position))
val vertMargin = pageHeight * (1 - scaleFactor) / 2
val horzMargin = pageWidth * (1 - scaleFactor) / 2
view.apply {
if (position < 0) {
translationX = horzMargin - vertMargin / 2
} else {
translationX = -horzMargin + vertMargin / 2
}
scaleX = scaleFactor
scaleY = scaleFactor
alpha = MIN_ALPHA + (scaleFactor - MIN_SCALE) / (1 - MIN_SCALE) * (1 - MIN_ALPHA)
}
}
else -> view.alpha = 0f
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment