Fixed ViewPager transformers
This commit is contained in:
parent
a67985d040
commit
998655fd48
12 changed files with 196 additions and 166 deletions
|
@ -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))
|
||||||
|
|
|
@ -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()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -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) {}
|
||||||
|
}
|
|
@ -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) {
|
||||||
when {
|
page.apply {
|
||||||
position < -1 -> { // [-Infinity,-1)
|
when {
|
||||||
// This page is way off-screen to the left.
|
position < -1 -> { // [-Infinity,-1)
|
||||||
page.alpha = 0f
|
// This page is way off-screen to the left.
|
||||||
}
|
alpha = 0f
|
||||||
position <= 0 -> { // [-1,0]
|
}
|
||||||
page.alpha = 1f
|
position <= 0 -> { // [-1,0]
|
||||||
page.translationX = 0f
|
// Use the default slide transition when moving to the left page
|
||||||
page.scaleX = 1f
|
alpha = 1f
|
||||||
page.scaleY = 1f
|
translationX = 0f
|
||||||
}
|
scaleX = 1f
|
||||||
position <= 1 -> { // (0,1]
|
scaleY = 1f
|
||||||
page.translationX = -position * page.width
|
}
|
||||||
page.alpha = 1 - abs(position)
|
position <= 1 -> { // (0,1]
|
||||||
page.scaleX = 1 - abs(position)
|
// Fade the page out.
|
||||||
page.scaleY = 1 - abs(position)
|
alpha = 1 - position
|
||||||
}
|
|
||||||
else -> { // (1,+Infinity]
|
|
||||||
// This page is way off-screen to the right.
|
|
||||||
page.alpha = 0f
|
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -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 {
|
||||||
|
translationX = -position * width
|
||||||
|
pivotX = 0f
|
||||||
|
pivotY = 0f
|
||||||
|
|
||||||
page.translationX = -position * page.width
|
when {
|
||||||
page.pivotX = 0f
|
position < -1 -> { // [-Infinity,-1)
|
||||||
page.pivotY = 0f
|
// This page is way off-screen to the left.
|
||||||
|
alpha = 0f
|
||||||
|
// The Page is off-screen but it may still interfere with
|
||||||
when {
|
// click events of current page if
|
||||||
position < -1 -> { // [-Infinity,-1)
|
// it's visibility is not set to Gone
|
||||||
// This page is way off-screen to the left.
|
isVisible = false
|
||||||
page.alpha = 0f
|
}
|
||||||
// The Page is off-screen but it may still interfere with
|
position <= 0 -> { // [-1,0]
|
||||||
// click events of current page if
|
rotation = 90 * abs(position)
|
||||||
// it's visibility is not set to Gone
|
alpha = 1 - abs(position)
|
||||||
page.isVisible = false
|
isVisible = true
|
||||||
}
|
}
|
||||||
position <= 0 -> { // [-1,0]
|
position <= 1 -> { // (0,1]
|
||||||
page.rotation = 90 * abs(position)
|
rotation = 0f
|
||||||
page.alpha = 1 - abs(position)
|
alpha = 1f
|
||||||
page.isVisible = true
|
isVisible = true
|
||||||
}
|
}
|
||||||
position <= 1 -> { // (0,1]
|
else -> { // (1,+Infinity]
|
||||||
page.rotation = 0f
|
// This page is way off-screen to the right.
|
||||||
page.alpha = 1f
|
alpha = 0f
|
||||||
page.isVisible = true
|
isVisible = false
|
||||||
}
|
}
|
||||||
else -> { // (1,+Infinity]
|
|
||||||
// This page is way off-screen to the right.
|
|
||||||
page.alpha = 0f
|
|
||||||
page.isVisible = false
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,33 +22,33 @@ 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.cameraDistance = 20000f
|
||||||
|
|
||||||
page.translationX = -position * page.width
|
if (position < 0.5 && position > -0.5) {
|
||||||
page.cameraDistance = 20000f
|
page.isVisible = true
|
||||||
|
} else {
|
||||||
if (position < 0.5 && position > -0.5) {
|
page.isInvisible = true
|
||||||
page.isVisible = true
|
|
||||||
} else {
|
|
||||||
page.isInvisible = true
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
when {
|
|
||||||
position < -1 -> { // [-Infinity,-1)
|
|
||||||
// This page is way off-screen to the left.
|
|
||||||
page.alpha = 0f
|
|
||||||
}
|
}
|
||||||
position <= 0 -> { // [-1,0]
|
|
||||||
page.alpha = 1f
|
when {
|
||||||
page.rotationX = 180 * (1 - abs(position) + 1)
|
position < -1 -> { // [-Infinity,-1)
|
||||||
}
|
// This page is way off-screen to the left.
|
||||||
position <= 1 -> { // (0,1]
|
page.alpha = 0f
|
||||||
page.alpha = 1f
|
}
|
||||||
page.rotationX = -180 * (1 - abs(position) + 1)
|
position <= 0 -> { // [-1,0]
|
||||||
}
|
page.alpha = 1f
|
||||||
else -> { // (1,+Infinity]
|
page.rotationX = 180 * (1 - abs(position) + 1)
|
||||||
// This page is way off-screen to the right.
|
}
|
||||||
page.alpha = 0f
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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]
|
|
||||||
// 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
|
|
||||||
}
|
}
|
||||||
|
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)
|
// 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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 (position > -1 && position < 1) {
|
||||||
if (parallaxView != null) {
|
val width = parallaxView.width.toFloat()
|
||||||
if (position > -1 && position < 1) {
|
parallaxView.translationX = -(position * width * speed)
|
||||||
val width = parallaxView.width.toFloat()
|
val sc = (width - border) / width
|
||||||
parallaxView.translationX = -(position * width * speed)
|
if (position == 0f) {
|
||||||
val sc = (view.width.toFloat() - border) / view.width
|
scaleX = 1f
|
||||||
if (position == 0f) {
|
scaleY = 1f
|
||||||
view.scaleX = 1f
|
} else {
|
||||||
view.scaleY = 1f
|
scaleX = sc
|
||||||
} else {
|
scaleY = sc
|
||||||
view.scaleX = sc
|
}
|
||||||
view.scaleY = sc
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 {
|
||||||
|
translationX = -position * width
|
||||||
|
cameraDistance = 100000f
|
||||||
|
|
||||||
page.translationX = -position * page.width
|
if (position < 0.5 && position > -0.5) {
|
||||||
page.cameraDistance = 100000f
|
isVisible = true
|
||||||
|
} else {
|
||||||
if (position < 0.5 && position > -0.5) {
|
isInvisible = true
|
||||||
page.isVisible = true
|
|
||||||
} else {
|
|
||||||
page.isInvisible = true
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
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.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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -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) {
|
||||||
if (position >= 0) {
|
page.apply {
|
||||||
page.scaleX = (0.9f - 0.05f * position)
|
if (position >= 0) {
|
||||||
page.scaleY = 0.9f
|
scaleX = (0.9f - 0.05f * position)
|
||||||
page.translationX = -page.width * position
|
scaleY = 0.9f
|
||||||
page.translationY = -30 * position
|
translationX = -width * position
|
||||||
|
translationY = -30 * position
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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">
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue