View PagingImageGallery_activateProminentChild.kt
private val prominentThreshold = context.resources.getDimensionPixelSize(R.dimen.prominent_threshold) | |
// In scaleChildren: | |
for (i in 0 until childCount) { | |
val distanceToCenter = ... | |
child.isActivated = distanceToCenter < prominentThreshold | |
} |
View PagingImageGallery_overlayableImageView.kt
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 { |
View PagingImageGallery_scrollToPosition.kt
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 { |
View PagingImageGallery_translateChildrenToVisuallyAnchor.kt
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 |
View PagingImageGallery_translateChildrenToAnchor.kt
val translationDirection = if (childCenter > containerCenter) -1 else 1 | |
val translationXFromScale = translationDirection * child.width * (1 - scale) / 2f | |
child.translationX = translationXFromScale |
View PagingImageGallery_scaleChildren.kt
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( |
View PagingImageGallery_initialPosition.kt
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 |
View PagingImageGallery_boundsOffsetDecoration.kt
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 |
View PagingImageGallery_childRatio.kt
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() |
View PagingImageGallery_parentDimensions.kt
private var hasInitParentDimensions = false | |
private var maxImageWidth: Int = 0 | |
private var maxImageHeight: Int = 0 | |
private var maxImageAspectRatio: Float = 1f | |
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH { | |
if (!hasInitParentDimensions) { | |
maxImageWidth = parent.width | |
maxImageHeight = parent.height | |
maxImageAspectRatio = maxImageWidth.toFloat() / maxImageHeight.toFloat() |
NewerOlder