Android ViewPager2: Multiple Transform (Kotlin)

August 3, 2019
use CompositePageTransformer
val animTransformer = ViewPager2.PageTransformer { page, position ->
    page.apply {
        val r = 1 - abs(position)
        page.alpha = 0.25f + r
        page.scaleY = 0.85f + r * 0.15f
    }
}

val pageMarginPx = 10.dpToPx(resources.displayMetrics)

val marginTransformer = MarginPageTransformer(pageMarginPx)
val transformer = CompositePageTransformer().apply {
    addTransformer(animTransformer)
    addTransformer(marginTransformer)
}

viewPager.setPageTransformer(transformer)

NOTE: Convert Dp to Pixel - dpToPx

This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.