SpringAnimation弹簧动画简单使用(个人学习记录)

本文介绍了SpringAnimation的使用,包括如何创建SpringAnimation对象,并提供了实际操作的效果图和练习demo。
1.配置环境 
implementation 'com.android.support:support-dynamic-animation:27.1.1'  

2.创建SpringAnimation对象 (参数类型依次为 作用View , 动画类型 , 最终位置相对自己)

val anim = SpringAnimation(childView, SpringAnimation.TRANSLATION_Y, 0f)  

效果图:


练习demo:

class MainActivity : AppCompatActivity() {

    private var isShow = false

    private val animString = "Hello World!"

    private val showAnims = mutableListOf<SpringAnimation>()
    private val hideAnims = mutableListOf<SpringAnimation>()

    private val height by lazy { getHeiget() }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initView()
        initListener()
    }

    private fun initView() {

        animString.forEach {
            val childView = TextView(this@MainActivity)
            childView.textSize = 36f
            childView.text = it.toString()
            llRootView.addView(childView)
            childView.translationY = height.toFloat()


            val childViewShowAnim = SpringAnimation(childView, SpringAnimation.TRANSLATION_Y, 0f)
            showAnims.add(childViewShowAnim)
            childViewShowAnim.spring.stiffness = SpringForce.STIFFNESS_VERY_LOW
            childViewShowAnim.spring.dampingRatio = SpringForce.DAMPING_RATIO_MEDIUM_BOUNCY


            val childViewHideAnim = SpringAnimation(childView, SpringAnimation.TRANSLATION_Y, height.toFloat())
            hideAnims.add(childViewHideAnim)
            childViewShowAnim.spring.stiffness = SpringForce.STIFFNESS_MEDIUM
            childViewShowAnim.spring.dampingRatio = SpringForce.DAMPING_RATIO_LOW_BOUNCY
        }
    }

    private fun initListener() {
        btStart.setOnClickListener {
            if (isShow) setHideAnim() else setShowAnim()
            isShow = !isShow
        }
    }

    private fun setShowAnim() {
        showAnims.forEachIndexed { index, anim ->
            llRootView.postDelayed({
                anim.start()
            }, index * 80L)
        }
    }

    private fun setHideAnim() {
        for (i in hideAnims.size - 1 downTo 0) {
            llRootView.postDelayed({
                hideAnims[i].start()
            }, i * 60L)
        }
    }

    private fun getHeiget(): Int {
        val wm = this.getSystemService(Context.WINDOW_SERVICE)
        val p = Point()
        if (wm is WindowManager) {
            wm.defaultDisplay.getSize(p)
        }
        return p.y
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值