Skip to content

Instantly share code, notes, and snippets.

@amalhanaja
Created June 5, 2018 09:27
Show Gist options
  • Save amalhanaja/c918bd4c9ab0eebb86d131be54b2e014 to your computer and use it in GitHub Desktop.
Save amalhanaja/c918bd4c9ab0eebb86d131be54b2e014 to your computer and use it in GitHub Desktop.
Parralax Page Transformer
class ParallaxPagerTransformer : ViewPager.PageTransformer {
override fun transformPage(page: View, position: Float) {
val pageWidth = page.width
val pageWidthTimesPosition = pageWidth.times(position)
val imageScreen = page.findViewById<ImageView>(R.id.img_screen)
val imageBgTop = page.findViewById<ImageView>(R.id.img_bg_top)
val staticPosition = position <= -1.0f || position >= 1.0f
val zeroPosition = position == 0.0f
if (!staticPosition && !zeroPosition) {
imageScreen?.let {
it.translationX = pageWidthTimesPosition.times(0.5f)
}
imageBgTop?.let {
it.translationX = pageWidthTimesPosition.times(0.3f)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment