动画不会从onClick开始

时间:2019-01-24 13:38:18

标签: animation kotlin onclick android-animation

我有一个动画,想要在单击按钮时开始。如果我从onClickListener外部调用它,则效果很好。 onClickListener可与其他命令(例如println(“ xxx”))一起使用,但不会启动动画。知道为什么吗?

这是代码:

val mContentView: View = findViewByID(R.id.soul2)

    fun soulButler() {
        object : ViewTreeObserver.OnGlobalLayoutListener {
            override fun onGlobalLayout() {
                mContentView.viewTreeObserver.removeOnGlobalLayoutListener(this)

                val leftPos = mButtonView.left.toFloat()
                val rightPos = mButtonView.right.toFloat()
                val topPos = mButtonView.top.toFloat()
                val bottomPos = mButtonView.bottom.toFloat()

                val centerX = (leftPos + rightPos - mContentView.width) / 2
                val centerY = (topPos + bottomPos - mContentView.height) / 2

                val soultoX = ObjectAnimator.ofFloat(mContentView, "x", centerX).apply {
                    duration = 600
                }

                val soultoY = ObjectAnimator.ofFloat(mContentView, "y", centerY).apply {
                    duration = 600
                }

                val soultoheaven = ObjectAnimator.ofFloat(mContentView, "translationY", -130f).apply {
                    duration = 300
                }

                soultoX.interpolator = AccelerateInterpolator()
                soultoY.interpolator = AccelerateInterpolator(3f)
                soultoheaven.interpolator = DecelerateInterpolator()

                fun soulExit() = AnimatorSet().apply {
                    play(soultoX).with(soultoY)
                    play(soultoheaven).after(soultoY)
                    start()
                }

                soulExit()
            }
        }.run {
            mContentView.viewTreeObserver.addOnGlobalLayoutListener(this)
        }
        }
        selectLevelButton.setOnClickListener {
            mButtonView = findViewById(R.id.selectLevelButton)
            soulButler()
            }

0 个答案:

没有答案