Skip to content

Instantly share code, notes, and snippets.

@Bloody-Badboy
Created January 9, 2022 18:05
Show Gist options
  • Save Bloody-Badboy/ef0ebaa21d85a202e49d396eb39fc133 to your computer and use it in GitHub Desktop.
Save Bloody-Badboy/ef0ebaa21d85a202e49d396eb39fc133 to your computer and use it in GitHub Desktop.
Stack Page Transformer (Supports RTL) https://giphy.com/gifs/8ykEAOx9uMmVl5Mrr7
package dev.arpan.motionlayout.playground
import android.view.View
import androidx.core.view.ViewCompat
import androidx.viewpager2.widget.ViewPager2
class StackPageTransformer : ViewPager2.PageTransformer {
override fun transformPage(page: View, position: Float) {
page.apply {
when {
position < -1 -> {
alpha = 0f
}
position <= 0 -> {
if (ViewCompat.getLayoutDirection(page) == ViewCompat.LAYOUT_DIRECTION_RTL) {
alpha = 1f
translationX = width * position
translationZ = -1f
} else {
alpha = 1f
translationX = 0f
translationZ = 0f
}
}
position in 0.0..1.0 -> {
if (ViewCompat.getLayoutDirection(page) == ViewCompat.LAYOUT_DIRECTION_RTL) {
alpha = 1f
translationX = 0f
translationZ = 0f
} else {
alpha = 1f
translationX = width * -position
translationZ = -1f
}
}
else -> {
alpha = 0f
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment