Created
December 12, 2019 02:19
-
-
Save phileo/8ca2ac8ccfe4c68c2a236c2a73cc541c to your computer and use it in GitHub Desktop.
ZoomPageTransformer
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 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