Android第二次翻译动画不一样
Android translate animation is different the second time
我创建了一个非常简单的平移动画,当第一次按下按钮时,图像视图将向左移动,第二次将向右移动。距离完全一样
但第二次图像移动得更多,实际上并没有回到原来的位置。更准确地说,第二次图像移动了两倍的距离。为什么?
private var direction = 1
fun move() {
val animatorX = ObjectAnimator.ofFloat(draggableImage1, "translationX", direction*(-100f))
val animatorY = ObjectAnimator.ofFloat(draggableImage1, "translationY", 0f)
animatorX.duration = (500)
animatorY.duration = (500)
val animationSet = AnimatorSet()
animationSet.playTogether(animatorX, animatorY)
animationSet.start()
if (direction == 1) {direction = -1} else {direction = 1}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main2)
button.setOnClickListener{move()}
}
您可以使用下面的动画代码,试试这个更新后的答案。
val animatorX: ObjectAnimator?
if (direction == 1) {
direction = -1
animatorX = ObjectAnimator.ofFloat(star, "translationX", -100f)
} else {
direction = 1
animatorX = ObjectAnimator.ofFloat(star, "translationX", 200f)
}
animatorX.duration = 500
animatorX.start()
the second time the image goes twice the distance
因为在第一次动画之后它的 x 位置是 (initial x - 100) 并且第二次你将 x 动画化为 100 。所以第二次它从 -100 到 100 并且它与第一次的两倍距离
要向右移动,请将 translationX 设置为 0
private var direction = 1
fun move() {
var distance = -100f
if (direction == -1){
distance = 0f
}
val animatorX = ObjectAnimator.ofFloat(draggableImage1, "translationX", distance)
val animatorY = ObjectAnimator.ofFloat(draggableImage1, "translationY", 0f)
animatorX.duration = (500)
animatorY.duration = (500)
val animationSet = AnimatorSet()
animationSet.playTogether(animatorX, animatorY)
animationSet.start()
if (direction == 1) {direction = -1} else {direction = 1}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main2)
button.setOnClickListener{move()}
}
我确实希望有一种方法可以重置动画师,但如果没有,我确实找到了解决方法。你应该得到初始坐标,然后用它来计算平移:
val animatorX = ObjectAnimator.ofFloat(draggableImage1, "translationX", (draggableImage1.x - initalLeft) -(direction*100f))
我创建了一个非常简单的平移动画,当第一次按下按钮时,图像视图将向左移动,第二次将向右移动。距离完全一样
但第二次图像移动得更多,实际上并没有回到原来的位置。更准确地说,第二次图像移动了两倍的距离。为什么?
private var direction = 1
fun move() {
val animatorX = ObjectAnimator.ofFloat(draggableImage1, "translationX", direction*(-100f))
val animatorY = ObjectAnimator.ofFloat(draggableImage1, "translationY", 0f)
animatorX.duration = (500)
animatorY.duration = (500)
val animationSet = AnimatorSet()
animationSet.playTogether(animatorX, animatorY)
animationSet.start()
if (direction == 1) {direction = -1} else {direction = 1}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main2)
button.setOnClickListener{move()}
}
您可以使用下面的动画代码,试试这个更新后的答案。
val animatorX: ObjectAnimator?
if (direction == 1) {
direction = -1
animatorX = ObjectAnimator.ofFloat(star, "translationX", -100f)
} else {
direction = 1
animatorX = ObjectAnimator.ofFloat(star, "translationX", 200f)
}
animatorX.duration = 500
animatorX.start()
the second time the image goes twice the distance
因为在第一次动画之后它的 x 位置是 (initial x - 100) 并且第二次你将 x 动画化为 100 。所以第二次它从 -100 到 100 并且它与第一次的两倍距离
要向右移动,请将 translationX 设置为 0
private var direction = 1
fun move() {
var distance = -100f
if (direction == -1){
distance = 0f
}
val animatorX = ObjectAnimator.ofFloat(draggableImage1, "translationX", distance)
val animatorY = ObjectAnimator.ofFloat(draggableImage1, "translationY", 0f)
animatorX.duration = (500)
animatorY.duration = (500)
val animationSet = AnimatorSet()
animationSet.playTogether(animatorX, animatorY)
animationSet.start()
if (direction == 1) {direction = -1} else {direction = 1}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main2)
button.setOnClickListener{move()}
}
我确实希望有一种方法可以重置动画师,但如果没有,我确实找到了解决方法。你应该得到初始坐标,然后用它来计算平移:
val animatorX = ObjectAnimator.ofFloat(draggableImage1, "translationX", (draggableImage1.x - initalLeft) -(direction*100f))