Skip to content

Instantly share code, notes, and snippets.

@erdemtopak
Created January 13, 2020 10:08
Show Gist options
  • Save erdemtopak/44d6e024845c62dc9c4dc1ce849424f3 to your computer and use it in GitHub Desktop.
Save erdemtopak/44d6e024845c62dc9c4dc1ce849424f3 to your computer and use it in GitHub Desktop.
Zoom recyclerview layout manager
import android.content.Context
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import kotlin.math.abs
class ZoomRecyclerLayout: LinearLayoutManager {
private val mShrinkAmount = 0.15f
private val mShrinkDistance = 0.9f
constructor(context: Context) : super(context)
constructor(context: Context, orientation: Int, reverseLayout: Boolean) : super(
context,
orientation,
reverseLayout
)
override fun scrollVerticallyBy(
dy: Int,
recycler: RecyclerView.Recycler?,
state: RecyclerView.State?
): Int {
val orientation = orientation
if (orientation ==VERTICAL) {
val scrolled = super.scrollVerticallyBy(dy, recycler, state)
val midpoint = height / 2f
val d0 = 0f
val d1 = mShrinkDistance * midpoint
val s0 = 1f
val s1 = 1f - mShrinkAmount
for (i in 0 until childCount) {
val child = getChildAt(i)
val childMidpoint = (getDecoratedBottom(child!!) + getDecoratedTop(child)) / 2f
val d = d1.coerceAtMost(abs(midpoint - childMidpoint))
val scale = s0 + (s1 - s0) * (d - d0) / (d1 - d0)
child.scaleX = scale
child.scaleY = scale
}
return scrolled
} else {
return 0
}
}
override fun scrollHorizontallyBy(
dx: Int,
recycler: RecyclerView.Recycler?,
state: RecyclerView.State?
): Int {
val orientation = orientation
if (orientation == HORIZONTAL) {
val scrolled = super.scrollHorizontallyBy(dx, recycler, state)
val midpoint = width / 2f
val d0 = 0f
val d1 = mShrinkDistance * midpoint
val s0 = 1f
val s1 = 1f - mShrinkAmount
for (i in 0 until childCount) {
val child = getChildAt(i)
val childMidpoint = (getDecoratedRight(child!!) + getDecoratedLeft(child)) / 2f
val d = d1.coerceAtMost(abs(midpoint - childMidpoint))
val scale = s0 + (s1 - s0) * (d - d0) / (d1 - d0)
child.scaleX = scale
child.scaleY = scale
}
return scrolled
} else {
return 0
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment