1

我使用 ViewPropertyAnimator 来创建 alpha 动画。withStartAction() 和 withEndAction() 都很好用,alpha 值改变了,但它们很快就改变了,看起来没有动画,只是改变了值。哪里有错?

private fun run(millisecondsPerPixel: Long) { //millisPerPixel=30
    pixelsToCenter = (_endHeight - _startHeight - emptyPlaceOffset) / 2.toLong() // 15
    val duration = millisecondsPerPixel * pixelsToCenter // 450
    val animator = if (opening) mainLayout!!.animate() else secondaryLayout!!.animate()
    animator.alpha(0.0F).setDuration(duration)
            .withStartAction {
                changing = true
                val resize = if (opening) ResizeAnimation(this@CustomToolbar, height, _endHeight) else
                    ResizeAnimation(this@CustomToolbar, height, _startHeight)
                resize.duration = millisecondsPerPixel * (_endHeight - _startHeight)
                startAnimation(resize) // this one works like a charm
            }
            .withEndAction {
                val backAnimator = if (opening) {
                    mainLayout!!.visibility = View.GONE
                    secondaryLayout!!.animate()
                } else {
                    secondaryLayout!!.visibility = View.GONE
                    mainLayout!!.animate()
                }
                backAnimator.alpha(1.0F).setDuration(duration)
                        .setStartDelay(millisecondsPerPixel * emptyPlaceOffset)
                        .withStartAction {
                            if (opening) secondaryLayout!!.visibility = View.VISIBLE else mainLayout!!.visibility = View.VISIBLE
                        }
                        .withEndAction { changing = false }
            }
}
4

0 回答 0