Skip to content

Instantly share code, notes, and snippets.

@CodeK1988
Created April 28, 2019 01:35
Show Gist options
  • Save CodeK1988/bb7b51d70f382bb0c603bf28b1ec4cc8 to your computer and use it in GitHub Desktop.
Save CodeK1988/bb7b51d70f382bb0c603bf28b1ec4cc8 to your computer and use it in GitHub Desktop.
滑动缩放 CenterZoomLinearLayoutManager
class CenterZoomLinearLayoutManager(context: Context)
: LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false) {
override fun onLayoutCompleted(state: RecyclerView.State?) {
super.onLayoutCompleted(state)
scaleChildren()
}
override fun scrollHorizontallyBy(dx: Int, recycler: RecyclerView.Recycler?, state: RecyclerView.State?): Int {
return if (orientation == HORIZONTAL) {
super.scrollHorizontallyBy(dx, recycler, state).also { scaleChildren() }
} else {
0
}
}
/**
* 参考 https://www.jianshu.com/p/198748de8581
* https://stackoverflow.com/questions/32823713/how-to-scale-up-recycler-view-center-item-while-scrolling-in-android/54516315#54516315
* 300/375
*/
private fun scaleChildren() {
val midpoint = width / 2f
val d1 = 0.9f * midpoint
for (i in 0 until childCount) {
val child = getChildAt(i) as View
val d = Math.min(d1, Math.abs(midpoint - (getDecoratedRight(child) + getDecoratedLeft(child)) / 2f))
val scale = 1f - 0.1f * d / d1
child.scaleY = scale
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment