Skip to content

Instantly share code, notes, and snippets.

@dadouf
Last active December 11, 2020 10:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dadouf/d48ea17f4e92d3e8d7906152abbd1bea to your computer and use it in GitHub Desktop.
Save dadouf/d48ea17f4e92d3e8d7906152abbd1bea to your computer and use it in GitHub Desktop.
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