ObjectAnimator,如何移动对象两次?
ObjectAnimator, how to move object twice?
我想用 ObjectAnimator 平移视图两次,但是 "X" 位置在再次按下按钮后又回到起始位置,我该如何让它继续,在这种情况下进一步向右移动?
final ObjectAnimator animation = ObjectAnimator.ofFloat(button, "translationX", 100f);
animation.setDuration(2000);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
animation.start();
}
});
button.animate().translationX(button.translationX + 100f).setDuration(1000).start()
很有魅力
ofFloat()
方法的文档中介绍了按钮跳回其起始位置的原因:
A single value implies that that value is the one being animated to, in which case the start value will be derived from the property being animated and the target object when start()
is called for the first time.
因为您每次都重复使用相同的 ObjectAnimator
实例,所以 translationX
属性 每次都从其原始 (0) 动画到传入的参数 (100) .
但是,您不能只更改它来创建一个新的 ObjectAnimator
并让它解决所有问题。假设您将代码更改为:
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final ObjectAnimator animation = ObjectAnimator.ofFloat(button, "translationX", 100f);
animation.setDuration(2000);
animation.start();
}
});
那会改变发生的事情,但仍然不能给你你想要的。现在按钮第一次会从 0 滑到 100,但之后每次都会保持静止。
为什么?
因为在第一个动画之后,translationX
属性 是 100。所以现在你在 100 到 100 之间(而不是 0 到 100)制作动画...这不行任何东西。
解决方案是从视图的 current translationX
动画到当前值 + 100。每次都这样做,而不是重复使用相同的ObjectAnimator
一遍又一遍。
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
float end = button.getTranslationX() + 100;
ObjectAnimator animation = ObjectAnimator.ofFloat(button, "translationX", end);
animation.setDuration(2000);
animation.start();
}
});
我想用 ObjectAnimator 平移视图两次,但是 "X" 位置在再次按下按钮后又回到起始位置,我该如何让它继续,在这种情况下进一步向右移动?
final ObjectAnimator animation = ObjectAnimator.ofFloat(button, "translationX", 100f);
animation.setDuration(2000);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
animation.start();
}
});
button.animate().translationX(button.translationX + 100f).setDuration(1000).start()
很有魅力
ofFloat()
方法的文档中介绍了按钮跳回其起始位置的原因:
A single value implies that that value is the one being animated to, in which case the start value will be derived from the property being animated and the target object when
start()
is called for the first time.
因为您每次都重复使用相同的 ObjectAnimator
实例,所以 translationX
属性 每次都从其原始 (0) 动画到传入的参数 (100) .
但是,您不能只更改它来创建一个新的 ObjectAnimator
并让它解决所有问题。假设您将代码更改为:
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final ObjectAnimator animation = ObjectAnimator.ofFloat(button, "translationX", 100f);
animation.setDuration(2000);
animation.start();
}
});
那会改变发生的事情,但仍然不能给你你想要的。现在按钮第一次会从 0 滑到 100,但之后每次都会保持静止。
为什么?
因为在第一个动画之后,translationX
属性 是 100。所以现在你在 100 到 100 之间(而不是 0 到 100)制作动画...这不行任何东西。
解决方案是从视图的 current translationX
动画到当前值 + 100。每次都这样做,而不是重复使用相同的ObjectAnimator
一遍又一遍。
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
float end = button.getTranslationX() + 100;
ObjectAnimator animation = ObjectAnimator.ofFloat(button, "translationX", end);
animation.setDuration(2000);
animation.start();
}
});