package com.gh.gamecenter.common.view.stacklayoutmanager2

import android.animation.TimeInterpolator
import android.view.View

class DefaultLayoutAnimation : StackAnimation2 {

    private val alphaInterceptor = TimeInterpolator { x ->
        x * x * x * x
    }

    override fun layoutAnimations(child: View, layoutPosition: Int, layoutManager: StackLayoutManagerV2) {
        // 相邻卡片间透明度差距变化
        val alphaSpace = 1f / layoutManager.visibleCount
        val alpha: Float
        var scale = 1f
        if (layoutPosition == layoutManager.selectedPosition) {
            alpha = 1 - alphaInterceptor.getInterpolation(layoutManager.scrollPercent)
        } else {
            alpha =
                1 - (layoutPosition - layoutManager.selectedPosition) * alphaSpace + alphaSpace * layoutManager.scrollPercent
            scale = 1 - (layoutPosition - layoutManager.selectedPosition) * 0.1f + 0.1f * layoutManager.scrollPercent
        }
        child.alpha = alpha
        child.pivotY = child.measuredHeight / 2f
        child.pivotX = child.width.toFloat()
        child.scaleX = scale
        child.scaleY = scale
    }
}