Fixed ViewPager transformers

This commit is contained in:
Prathamesh More 2022-04-02 15:50:34 +05:30
parent a67985d040
commit 998655fd48
12 changed files with 196 additions and 166 deletions

View file

@ -77,7 +77,10 @@ class PlayerAlbumCoverFragment : AbsMusicServiceFragment(R.layout.fragment_playe
fun removeSlideEffect() { fun removeSlideEffect() {
val transformer = ParallaxPagerTransformer(R.id.player_image) val transformer = ParallaxPagerTransformer(R.id.player_image)
transformer.setSpeed(0.3f) transformer.setSpeed(0.3f)
lifecycleScope.launchWhenStarted {
viewPager.setPageTransformer(false, transformer)
} }
}
private fun updateLyrics() { private fun updateLyrics() {
binding.lyricsView.setLabel(context?.getString(R.string.no_lyrics_found)) binding.lyricsView.setLabel(context?.getString(R.string.no_lyrics_found))

View file

@ -49,6 +49,4 @@ class CarousalPagerTransformer(context: Context) : ViewPager.PageTransformer {
val m = context.resources.displayMetrics.density val m = context.resources.displayMetrics.density
return (dipValue * m + 0.5f).toInt() return (dipValue * m + 0.5f).toInt()
} }
} }

View file

@ -22,21 +22,28 @@ class CascadingPageTransformer : ViewPager.PageTransformer {
private var mScaleOffset = 40 private var mScaleOffset = 40
override fun transformPage(page: View, position: Float) { override fun transformPage(page: View, position: Float) {
if (position <= 0.0f) {//被滑动的那页 position 是-下标~ 0 page.apply {
page.translationX = 0f when {
//旋转角度 45° * -0.1 = -4.5° position < -1 -> { // [-Infinity,-1)
page.rotation = 45 * position alpha = 0f
//X轴偏移 li: 300/3 * -0.1 = -10 }
page.translationX = page.width / 3 * position position <= 0 -> {
} else { alpha = 1f
//缩放比例 rotation = 45 * position
val scale = (page.width - mScaleOffset * position) / page.width.toFloat() translationX = width / 3 * position
}
else -> {
alpha = 1f
rotation = 0f
val scale = (width - mScaleOffset * position) / width.toFloat()
page.scaleX = scale scaleX = scale
page.scaleY = scale scaleY = scale
page.translationX = -page.width * position translationX = -width * position
page.translationY = mScaleOffset * 0.8f * position translationY = mScaleOffset * 0.8f * position
}
}
} }
} }
} }

View file

@ -0,0 +1,8 @@
package code.name.monkey.retromusic.transform
import android.view.View
import androidx.viewpager.widget.ViewPager
class DefaultTransformer : ViewPager.PageTransformer {
override fun transformPage(page: View, position: Float) {}
}

View file

@ -20,28 +20,40 @@ import kotlin.math.abs
class DepthTransformation : ViewPager.PageTransformer { class DepthTransformation : ViewPager.PageTransformer {
override fun transformPage(page: View, position: Float) { override fun transformPage(page: View, position: Float) {
page.apply {
when { when {
position < -1 -> { // [-Infinity,-1) position < -1 -> { // [-Infinity,-1)
// This page is way off-screen to the left. // This page is way off-screen to the left.
page.alpha = 0f alpha = 0f
} }
position <= 0 -> { // [-1,0] position <= 0 -> { // [-1,0]
page.alpha = 1f // Use the default slide transition when moving to the left page
page.translationX = 0f alpha = 1f
page.scaleX = 1f translationX = 0f
page.scaleY = 1f scaleX = 1f
scaleY = 1f
} }
position <= 1 -> { // (0,1] position <= 1 -> { // (0,1]
page.translationX = -position * page.width // Fade the page out.
page.alpha = 1 - abs(position) alpha = 1 - position
page.scaleX = 1 - abs(position)
page.scaleY = 1 - abs(position) // Counteract the default slide transition
translationX = width * -position
// Scale the page down (between MIN_SCALE and 1)
val scaleFactor = (MIN_SCALE + (1 - MIN_SCALE) * (1 - abs(position)))
scaleX = scaleFactor
scaleY = scaleFactor
} }
else -> { // (1,+Infinity] else -> { // (1,+Infinity]
// This page is way off-screen to the right. // This page is way off-screen to the right.
page.alpha = 0f alpha = 0f
}
}
}
}
} companion object {
} private const val MIN_SCALE = 0.5f
} }
} }

View file

@ -21,35 +21,35 @@ import kotlin.math.abs
class HingeTransformation : ViewPager.PageTransformer { class HingeTransformation : ViewPager.PageTransformer {
override fun transformPage(page: View, position: Float) { override fun transformPage(page: View, position: Float) {
page.apply {
page.translationX = -position * page.width translationX = -position * width
page.pivotX = 0f pivotX = 0f
page.pivotY = 0f pivotY = 0f
when { when {
position < -1 -> { // [-Infinity,-1) position < -1 -> { // [-Infinity,-1)
// This page is way off-screen to the left. // This page is way off-screen to the left.
page.alpha = 0f alpha = 0f
// The Page is off-screen but it may still interfere with // The Page is off-screen but it may still interfere with
// click events of current page if // click events of current page if
// it's visibility is not set to Gone // it's visibility is not set to Gone
page.isVisible = false isVisible = false
} }
position <= 0 -> { // [-1,0] position <= 0 -> { // [-1,0]
page.rotation = 90 * abs(position) rotation = 90 * abs(position)
page.alpha = 1 - abs(position) alpha = 1 - abs(position)
page.isVisible = true isVisible = true
} }
position <= 1 -> { // (0,1] position <= 1 -> { // (0,1]
page.rotation = 0f rotation = 0f
page.alpha = 1f alpha = 1f
page.isVisible = true isVisible = true
} }
else -> { // (1,+Infinity] else -> { // (1,+Infinity]
// This page is way off-screen to the right. // This page is way off-screen to the right.
page.alpha = 0f alpha = 0f
page.isVisible = false isVisible = false
}
} }
} }
} }

View file

@ -22,7 +22,7 @@ import kotlin.math.abs
class HorizontalFlipTransformation : ViewPager.PageTransformer { class HorizontalFlipTransformation : ViewPager.PageTransformer {
override fun transformPage(page: View, position: Float) { override fun transformPage(page: View, position: Float) {
page.apply {
page.translationX = -position * page.width page.translationX = -position * page.width
page.cameraDistance = 20000f page.cameraDistance = 20000f
@ -32,7 +32,6 @@ class HorizontalFlipTransformation : ViewPager.PageTransformer {
page.isInvisible = true page.isInvisible = true
} }
when { when {
position < -1 -> { // [-Infinity,-1) position < -1 -> { // [-Infinity,-1)
// This page is way off-screen to the left. // This page is way off-screen to the left.
@ -52,4 +51,5 @@ class HorizontalFlipTransformation : ViewPager.PageTransformer {
} }
} }
} }
}
} }

View file

@ -25,39 +25,41 @@ import kotlin.math.max
class NormalPageTransformer : ViewPager.PageTransformer { class NormalPageTransformer : ViewPager.PageTransformer {
override fun transformPage(view: View, position: Float) { override fun transformPage(page: View, position: Float) {
val pageWidth = view.width page.apply {
val pageHeight = view.height val pageWidth = width
val pageHeight = height
when { when {
position < -1 -> { // [-Infinity,-1) position < -1 -> { // [-Infinity,-1)
// This page is way off-screen to the left. // This page is way off-screen to the left.
view.alpha = 1f alpha = 1f
view.scaleY = 0.7f scaleY = 0.7f
} }
position <= 1 -> { // [-1,1] position <= 1 -> { // [-1,1]
// Modify the default slide transition to shrink the page as well // Modify the default slide transition to shrink the page as well
val scaleFactor = max(MIN_SCALE, 1 - abs(position)) val scaleFactor = max(MIN_SCALE, 1 - abs(position))
val vertMargin = pageHeight * (1 - scaleFactor) / 2 val vertMargin = pageHeight * (1 - scaleFactor) / 2
val horzMargin = pageWidth * (1 - scaleFactor) / 2 val horzMargin = pageWidth * (1 - scaleFactor) / 2
if (position < 0) { translationX = if (position < 0) {
view.translationX = horzMargin - vertMargin / 2 horzMargin - vertMargin / 2
} else { } else {
view.translationX = -horzMargin + vertMargin / 2 -horzMargin + vertMargin / 2
} }
// Scale the page down (between MIN_SCALE and 1) // Scale the page down (between MIN_SCALE and 1)
view.scaleX = scaleFactor scaleX = scaleFactor
view.scaleY = scaleFactor scaleY = scaleFactor
// Fade the page relative to its size. // Fade the page relative to its size.
//view.setAlpha(MIN_ALPHA + (scaleFactor - MIN_SCALE) / (1 - MIN_SCALE) * (1 - MIN_ALPHA)); //setAlpha(MIN_ALPHA + (scaleFactor - MIN_SCALE) / (1 - MIN_SCALE) * (1 - MIN_ALPHA));
} }
else -> { // (1,+Infinity] else -> { // (1,+Infinity]
// This page is way off-screen to the right. // This page is way off-screen to the right.
view.alpha = 1f alpha = 1f
view.scaleY = 0.7f scaleY = 0.7f
}
} }
} }
} }

View file

@ -14,8 +14,6 @@
package code.name.monkey.retromusic.transform package code.name.monkey.retromusic.transform
import android.annotation.TargetApi
import android.os.Build
import android.view.View import android.view.View
import androidx.viewpager.widget.ViewPager import androidx.viewpager.widget.ViewPager
@ -27,22 +25,21 @@ class ParallaxPagerTransformer(private val id: Int) : ViewPager.PageTransformer
private var border = 0 private var border = 0
private var speed = 0.2f private var speed = 0.2f
@TargetApi(Build.VERSION_CODES.HONEYCOMB) override fun transformPage(page: View, position: Float) {
override fun transformPage(view: View, position: Float) { val parallaxView = page.findViewById<View>(id)
page.apply {
val parallaxView = view.findViewById<View>(id)
if (parallaxView != null) { if (parallaxView != null) {
if (position > -1 && position < 1) { if (position > -1 && position < 1) {
val width = parallaxView.width.toFloat() val width = parallaxView.width.toFloat()
parallaxView.translationX = -(position * width * speed) parallaxView.translationX = -(position * width * speed)
val sc = (view.width.toFloat() - border) / view.width val sc = (width - border) / width
if (position == 0f) { if (position == 0f) {
view.scaleX = 1f scaleX = 1f
view.scaleY = 1f scaleY = 1f
} else { } else {
view.scaleX = sc scaleX = sc
view.scaleY = sc scaleY = sc
}
} }
} }
} }

View file

@ -22,37 +22,35 @@ import kotlin.math.abs
class VerticalFlipTransformation : ViewPager.PageTransformer { class VerticalFlipTransformation : ViewPager.PageTransformer {
override fun transformPage(page: View, position: Float) { override fun transformPage(page: View, position: Float) {
page.apply {
page.translationX = -position * page.width translationX = -position * width
page.cameraDistance = 100000f cameraDistance = 100000f
if (position < 0.5 && position > -0.5) { if (position < 0.5 && position > -0.5) {
page.isVisible = true isVisible = true
} else { } else {
page.isInvisible = true isInvisible = true
} }
when { when {
position < -1 -> { // [-Infinity,-1) position < -1 -> { // [-Infinity,-1)
// This page is way off-screen to the left. // This page is way off-screen to the left.
page.alpha = 0f alpha = 0f
} }
position <= 0 -> { // [-1,0] position <= 0 -> { // [-1,0]
page.alpha = 1f alpha = 1f
page.rotationY = 180 * (1 - abs(position) + 1) rotationY = 180 * (1 - abs(position) + 1)
} }
position <= 1 -> { // (0,1] position <= 1 -> { // (0,1]
page.alpha = 1f alpha = 1f
page.rotationY = -180 * (1 - abs(position) + 1) rotationY = -180 * (1 - abs(position) + 1)
} }
else -> { // (1,+Infinity] else -> { // (1,+Infinity]
// This page is way off-screen to the right. // This page is way off-screen to the right.
page.alpha = 0f alpha = 0f
}
} }
} }
} }
} }

View file

@ -19,11 +19,14 @@ import androidx.viewpager.widget.ViewPager
class VerticalStackTransformer : ViewPager.PageTransformer { class VerticalStackTransformer : ViewPager.PageTransformer {
override fun transformPage(page: View, position: Float) { override fun transformPage(page: View, position: Float) {
page.apply {
if (position >= 0) { if (position >= 0) {
page.scaleX = (0.9f - 0.05f * position) scaleX = (0.9f - 0.05f * position)
page.scaleY = 0.9f scaleY = 0.9f
page.translationX = -page.width * position translationX = -width * position
page.translationY = -30 * position translationY = -30 * position
} }
} }
}
} }

View file

@ -8,6 +8,7 @@
<item>@string/horizontal_flip</item> <item>@string/horizontal_flip</item>
<item>@string/hinge</item> <item>@string/hinge</item>
<item>@string/stack</item> <item>@string/stack</item>
<item>@string/classic</item>
</array> </array>
<string-array name="pref_album_cover_transform_values"> <string-array name="pref_album_cover_transform_values">
@ -18,6 +19,7 @@
<item>4</item> <item>4</item>
<item>5</item> <item>5</item>
<item>6</item> <item>6</item>
<item>7</item>
</string-array> </string-array>
<string-array name="pref_lyrics_type_values"> <string-array name="pref_lyrics_type_values">