forked from youth5201314/banner
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
140 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
app/src/main/java/com/test/banner/FivePagesOneScreenTransformer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.test.banner | ||
|
||
import android.view.View | ||
import androidx.viewpager2.widget.ViewPager2 | ||
|
||
/** | ||
* 一拼五页 | ||
* | ||
* @property scale Float | ||
* @property translationX Float | ||
* @constructor | ||
*/ | ||
class FivePagesOneScreenTransformer( | ||
private val scale: Float = DEFAULT_MIN_SCALE, | ||
private val translationX: Float = -120f) : ViewPager2.PageTransformer { | ||
|
||
companion object { | ||
/** | ||
* 默认的中心点 | ||
*/ | ||
private const val DEFAULT_CENTER = 0.5f | ||
|
||
/** | ||
* 默认的缩放比例 | ||
*/ | ||
private const val DEFAULT_MIN_SCALE = 0.85f | ||
} | ||
|
||
override fun transformPage(page: View, position: Float) { | ||
val pageWidth = page.width | ||
val pageHeight = page.height | ||
page.pivotX = (pageWidth / 2).toFloat() | ||
page.pivotY = (pageHeight / 2).toFloat() | ||
when { | ||
position < -2 -> { | ||
// This page is way off-screen to the left. | ||
page.scaleX = 0f | ||
page.scaleY = 0f | ||
page.pivotX = pageWidth.toFloat() | ||
} | ||
position < 0 -> { | ||
//缩放 | ||
((1 + position) * (1 - scale) + scale).also { | ||
page.scaleX = it | ||
page.scaleY = it | ||
} | ||
page.pivotX = pageWidth * (DEFAULT_CENTER + DEFAULT_CENTER * -position) | ||
//层级 | ||
page.translationZ = position | ||
} | ||
position < 3 -> { | ||
//缩放 | ||
((1 - position) * (1 - scale) + scale).also { | ||
page.scaleX = it | ||
page.scaleY = it | ||
} | ||
page.pivotX = pageWidth * ((1 - position) * DEFAULT_CENTER) | ||
//层级 | ||
page.translationZ = -position | ||
} | ||
else -> { | ||
page.scaleX = 0f | ||
page.scaleY = 0f | ||
page.pivotX = pageWidth.toFloat() | ||
} | ||
} | ||
page.translationX = translationX * position | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters