Update page flip animation

pull/81/head
Koitharu 4 years ago
parent f0add59f99
commit 9a65e40be1
No known key found for this signature in database
GPG Key ID: 8E861F8CE6E7CE27

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

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

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

Loading…
Cancel
Save