Last active
December 11, 2020 10:33
-
-
Save dadouf/d48ea17f4e92d3e8d7906152abbd1bea to your computer and use it in GitHub Desktop.
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
internal class ProminentLayoutManager( | |
context: Context, | |
private val minScaleDistanceFactor: Float = 1.5f, | |
private val scaleDownBy: Float = 0.5f | |
) : LinearLayoutManager(context, HORIZONTAL, false) { | |
override fun onLayoutCompleted(state: RecyclerView.State?) = | |
super.onLayoutCompleted(state).also { scaleChildren() } | |
override fun scrollHorizontallyBy( | |
dx: Int, | |
recycler: RecyclerView.Recycler, | |
state: RecyclerView.State | |
) = super.scrollHorizontallyBy(dx, recycler, state).also { | |
if (orientation == HORIZONTAL) scaleChildren() | |
} | |
private fun scaleChildren() { | |
val containerCenter = width / 2f | |
// Any view further than this threshold will be fully scaled down | |
val scaleDistanceThreshold = minScaleDistanceFactor * containerCenter | |
for (i in 0 until childCount) { | |
val child = getChildAt(i)!! | |
val childCenter = (child.left + child.right) / 2f | |
val distanceToCenter = abs(childCenter - containerCenter) | |
val scaleDownAmount = (distanceToCenter / scaleDistanceThreshold).coerceAtMost(1f) | |
val scale = 1f - scaleDownBy * scaleDownAmount | |
child.scaleX = scale | |
child.scaleY = scale | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment