Skip to content

Instantly share code, notes, and snippets.

View dadouf's full-sized avatar

David Ferrand dadouf

View GitHub Profile
@dadouf
dadouf / keybase.md
Created June 6, 2022 15:47
Link Keybase-GitHub

Keybase proof

I hereby claim:

  • I am dadouf on github.
  • I am davidferrand (https://keybase.io/davidferrand) on keybase.
  • I have a public key ASC_UAUPovCvxb71rt6fTxbl9BzlHdVWS3-GcMxRaAk_vAo

To claim this, I am signing this object:

private val prominentThreshold = context.resources.getDimensionPixelSize(R.dimen.prominent_threshold)
// In scaleChildren:
for (i in 0 until childCount) {
val distanceToCenter = ...
child.isActivated = distanceToCenter < prominentThreshold
}
class OverlayableImageView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : ConstraintLayout(context, attrs, defStyleAttr) {
private val imageView: ImageView
private val sendButton: ImageButton
init {
vh.imageView.setOnClickListener {
val rv = vh.imageView.parent as RecyclerView
rv.smoothScrollToCenteredPosition(position)
}
fun RecyclerView.smoothScrollToCenteredPosition(position: Int) {
val smoothScroller = object : LinearSmoothScroller(context) {
override fun calculateDxToMakeVisible(view: View?,
snapPref: Int): Int {
var translationXForward = 0f
for (i in 0 until childCount) {
val translationXFromScale = ... // like before
child.translationX = translationXForward + translationXFromScale
translationXForward = 0f
if (translationXFromScale > 0 && i >= 1) {
// Edit previous child
val translationDirection = if (childCenter > containerCenter) -1 else 1
val translationXFromScale = translationDirection * child.width * (1 - scale) / 2f
child.translationX = translationXFromScale
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(
private fun initRecyclerViewPosition(position: Int) {
// This initial scroll will be slightly off because it doesn't
// respect the SnapHelper. Do it anyway so that the target view
// is laid out, then adjust onPreDraw.
layoutManager.scrollToPosition(position)
recyclerView.doOnPreDraw {
val targetView = layoutManager.findViewByPosition(position)
?: return@doOnPreDraw
class BoundsOffsetDecoration : ItemDecoration() {
override fun getItemOffsets(outRect: Rect,
view: View,
parent: RecyclerView,
state: RecyclerView.State) {
super.getItemOffsets(outRect, view, parent, state)
val itemPosition = parent.getChildAdapterPosition(view)
// It is crucial to refer to layoutParams.width
override fun onBindViewHolder(vh: VH, position: Int) {
val image = images[position]
// Resize view to respect aspect ratio
val imageAspectRatio = image.aspectRatio
val targetImageWidth: Int =
if (imageAspectRatio < maxImageAspectRatio) {
// Tall image: height = max, width adjusts
(maxImageHeight * imageAspectRatio).roundToInt()