Update page flip animation

This commit is contained in:
Koitharu
2022-01-05 15:37:13 +02:00
parent f0add59f99
commit 9a65e40be1
3 changed files with 42 additions and 50 deletions

View File

@@ -5,32 +5,27 @@ import androidx.viewpager2.widget.ViewPager2
class ReversedPageAnimTransformer : ViewPager2.PageTransformer {
override fun transformPage(page: View, position: Float) {
with(page) {
val pageWidth = width
when {
position > 1 -> alpha = 0f
position >= 0 -> {
alpha = 1f
translationX = 0f
translationZ = 0f
scaleX = 1 + FACTOR * position
scaleY = 1f
}
position >= -1 -> {
alpha = 1f
translationX = pageWidth * -position
translationZ = -1f
scaleX = 1f
scaleY = 1f
}
else -> alpha = 0f
override fun transformPage(page: View, position: Float) = with(page) {
translationX = -position * width
pivotX = width.toFloat()
pivotY = height / 2f
cameraDistance = 20000f
when {
position < -1f || position > 1f -> {
alpha = 0f
rotationY = 0f
translationZ = -1f
}
position <= 0f -> {
alpha = 1f
rotationY = 0f
translationZ = 0f
}
position > 0f -> {
alpha = 1f
rotationY = 120 * position
translationZ = 2f
}
}
}
private companion object {
const val FACTOR = 0.1f
}
}

View File

@@ -5,32 +5,27 @@ import androidx.viewpager2.widget.ViewPager2
class PageAnimTransformer : ViewPager2.PageTransformer {
override fun transformPage(page: View, position: Float) {
page.apply {
val pageWidth = width
when {
position < -1 -> alpha = 0f
position <= 0 -> { // [-1,0]
alpha = 1f
translationX = 0f
translationZ = 0f
scaleX = 1 + FACTOR * position
scaleY = 1f
}
position <= 1 -> { // (0,1]
alpha = 1f
translationX = pageWidth * -position
translationZ = -1f
scaleX = 1f
scaleY = 1f
}
else -> alpha = 0f
override fun transformPage(page: View, position: Float) = with(page) {
translationX = -position * width
pivotX = 0f
pivotY = height / 2f
cameraDistance = 20000f
when {
position < -1f || position > 1f -> {
alpha = 0f
rotationY = 0f
translationZ = -1f
}
position > 0f -> {
alpha = 1f
rotationY = 0f
translationZ = 0f
}
position <= 0f -> {
alpha = 1f
rotationY = 120 * position
translationZ = 2f
}
}
}
private companion object {
const val FACTOR = 0.1f
}
}

View File

@@ -133,6 +133,8 @@ fun View.resetTransformations() {
translationZ = 0f
scaleX = 1f
scaleY = 1f
rotationX = 0f
rotationY = 0f
}
inline fun RecyclerView.doOnCurrentItemChanged(crossinline callback: (Int) -> Unit) {