Created
December 29, 2020 14:01
-
-
Save RobertApikyan/fe38e82c84be42c2c5d9736ebdd989b7 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
package com.hfh.wellbe.smartwatch.utils | |
import android.content.Context | |
import androidx.recyclerview.widget.LinearLayoutManager | |
import androidx.recyclerview.widget.RecyclerView | |
import kotlin.math.abs | |
import kotlin.math.min | |
class CenterZoomLayoutManager : LinearLayoutManager { | |
private val mShrinkAmount = 0.36f | |
private val mShrinkDistance = 1f | |
private val factor = 2f | |
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 / factor | |
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)) / factor | |
val d = min(d1, 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 / factor | |
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)) / factor | |
val d = min(d1, 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