android - 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())
}
解决方案
推荐阅读
- swift - 如何在代码中区分带有 Home 栏的新 iPadPro 和带有 Home 键的旧 iPad Pro?
- c# - 我在此方法中的 DataTables 生成的 csv 文件是它们包含的行数的两倍,为什么?
- kops - 如何从 yaml 文件更新 Kops 配置?
- angular - Angular 的 setter 和 getter
- python - 如何让我的 Python 程序对 int 使用 4 个字节而不是 24 个字节?
- java - 我可以在枚举中使用接口作为变量吗?
- javascript - 处理 JSON 格式响应的问题
- c++ - C++14 类函数错误:“对象没有属性‘值’”
- python-3.x - 如何将组转置为熊猫数据框中的列?
- powershell - 使用 Powershell 脚本在 .txt 管道分隔的平面上删除 CR LF 不正确的分割线