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() {
val transformer = ParallaxPagerTransformer(R.id.player_image)
transformer.setSpeed(0.3f)
}
lifecycleScope.launchWhenStarted {
viewPager.setPageTransformer(false, transformer)
}
}
private fun updateLyrics() {
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
return (dipValue * m + 0.5f).toInt()
}
}

View file

@ -22,21 +22,28 @@ class CascadingPageTransformer : ViewPager.PageTransformer {
private var mScaleOffset = 40
override fun transformPage(page: View, position: Float) {
if (position <= 0.0f) {//被滑动的那页 position 是-下标~ 0
page.translationX = 0f
//旋转角度 45° * -0.1 = -4.5°
page.rotation = 45 * position
//X轴偏移 li: 300/3 * -0.1 = -10
page.translationX = page.width / 3 * position
} else {
//缩放比例
val scale = (page.width - mScaleOffset * position) / page.width.toFloat()
page.apply {
when {
position < -1 -> { // [-Infinity,-1)
alpha = 0f
}
position <= 0 -> {
alpha = 1f
rotation = 45 * position
translationX = width / 3 * position
}
else -> {
alpha = 1f
rotation = 0f
val scale = (width - mScaleOffset * position) / width.toFloat()
page.scaleX = scale
page.scaleY = scale
scaleX = scale
scaleY = scale
page.translationX = -page.width * position
page.translationY = mScaleOffset * 0.8f * position
translationX = -width * 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 {
override fun transformPage(page: View, position: Float) {
when {
position < -1 -> { // [-Infinity,-1)
// This page is way off-screen to the left.
page.alpha = 0f
}
position <= 0 -> { // [-1,0]
page.alpha = 1f
page.translationX = 0f
page.scaleX = 1f
page.scaleY = 1f
}
position <= 1 -> { // (0,1]
page.translationX = -position * page.width
page.alpha = 1 - abs(position)
page.scaleX = 1 - abs(position)
page.scaleY = 1 - abs(position)
}
else -> { // (1,+Infinity]
// This page is way off-screen to the right.
page.alpha = 0f
page.apply {
when {
position < -1 -> { // [-Infinity,-1)
// This page is way off-screen to the left.
alpha = 0f
}
position <= 0 -> { // [-1,0]
// Use the default slide transition when moving to the left page
alpha = 1f
translationX = 0f
scaleX = 1f
scaleY = 1f
}
position <= 1 -> { // (0,1]
// Fade the page out.
alpha = 1 - 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]
// This page is way off-screen to the right.
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 {
override fun transformPage(page: View, position: Float) {
page.apply {
translationX = -position * width
pivotX = 0f
pivotY = 0f
page.translationX = -position * page.width
page.pivotX = 0f
page.pivotY = 0f
when {
position < -1 -> { // [-Infinity,-1)
// This page is way off-screen to the left.
page.alpha = 0f
// The Page is off-screen but it may still interfere with
// click events of current page if
// it's visibility is not set to Gone
page.isVisible = false
}
position <= 0 -> { // [-1,0]
page.rotation = 90 * abs(position)
page.alpha = 1 - abs(position)
page.isVisible = true
}
position <= 1 -> { // (0,1]
page.rotation = 0f
page.alpha = 1f
page.isVisible = true
}
else -> { // (1,+Infinity]
// This page is way off-screen to the right.
page.alpha = 0f
page.isVisible = false
when {
position < -1 -> { // [-Infinity,-1)
// This page is way off-screen to the left.
alpha = 0f
// The Page is off-screen but it may still interfere with
// click events of current page if
// it's visibility is not set to Gone
isVisible = false
}
position <= 0 -> { // [-1,0]
rotation = 90 * abs(position)
alpha = 1 - abs(position)
isVisible = true
}
position <= 1 -> { // (0,1]
rotation = 0f
alpha = 1f
isVisible = true
}
else -> { // (1,+Infinity]
// This page is way off-screen to the right.
alpha = 0f
isVisible = false
}
}
}
}

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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