首页 > 解决方案 > Android - 翻译动画第一次但不是第二次

问题描述

我有一个动画,它基本上通过翻译 X 值对编号的球进行排序。它第一次很好用,但是当我生成新数字并尝试再次排序时,它不能按预期工作。

第一个 gif 是它工作正常。

https://giphy.com/gifs/igyw5gvPtbmEOZqoBP

所以在这里我点击排序按钮,球被正确排序。

在下一个 gif 中,我生成了新数字,然后尝试排序,但正如您所见,我得到了一些奇怪的行为。

http://www.giphy.com/gifs/kAuH2ZnnNCRX6bPKto

几乎就像是在使用旧的 X 值进行翻译。

这是触发动画的排序方法

  private fun sort() {
        val animator = AnimatorSet()

        val sorted = balls.sortedBy { x -> x.value }

        sorted.forEachIndexed { index, ball ->
            if (balls[index] == ball) {
                return@forEachIndexed
            }

            val occupyingBall = balls[index]
            val startingPos = balls.indexOf(ball)

            // move starting ball to occupying ball
            val occupyingRect = calculateRectOnScreen(occupyingBall)
            val startingRect = calculateRectOnScreen(ball)

            val move = getMoveAnimation(ball, startingPos, balls.indexOf(occupyingBall), occupyingRect, startingRect)
            animator.play(move)

            animator.start()
        }

        balls.clear()
        balls.addAll(sorted)
    }

这是getMoveAnimation创建对象动画师的方法。

private fun getMoveAnimation(view: View, startingIndex: Int, finishingIndex: Int, rect1: RectF, rect2: RectF): ObjectAnimator {
        val distance = if (startingIndex > finishingIndex) {
            // Move left
            -Math.abs(rect1.right - rect2.right)
        } else {
            // Move right
            Math.abs(rect1.right - rect2.right)
        }

        return ObjectAnimator.ofFloat(view, "translationX", distance)
    }

这是calculateRectOnScreen计算视图之间距离的方法

 private fun calculateRectOnScreen(view: View): RectF {
        val location = IntArray(2)
        view.getLocationOnScreen(location)
        return RectF(location[0].toFloat(), location[1].toFloat(), (location[0] + view.measuredWidth).toFloat(), (location[1] + view.measuredHeight).toFloat())
    }

标签: androidandroid-animation

解决方案


推荐阅读