我有一个动画列表,我希望能够通过单击“下一步”按钮来播放它们,并通过单击“上一步”按钮将它们回放。所以我可以播放第一个动画,然后播放第二个动画,再倒放第二个动画并达到与播放第一个动画之后相同的位置。
我的问题是动画完成后无法倒放。我知道可以设置autoReverse,但这样每个动画都会立即倒放。
以下是一个动画的示例:
我的问题是动画完成后无法倒放。我知道可以设置autoReverse,但这样每个动画都会立即倒放。
以下是一个动画的示例:
import javafx.animation.TranslateTransition;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.util.Duration;
public class AnimTest extends Application {
@Override
public void start(Stage stage) throws Exception {
Circle c = new Circle(5, Color.RED);
TranslateTransition move = new TranslateTransition(Duration.seconds(2), c);
move.setByX(10);
move.setByY(10);
Button next = new Button("Next");
Button previous = new Button("Previous");
next.setOnAction(e -> {
move.setRate(1);
move.play();
});
previous.setOnAction(e -> {
move.setRate(-1);
move.play();
});
Pane p = new Pane(c);
p.setPrefSize(50, 50);
HBox buttons = new HBox(next, previous);
VBox root = new VBox(p, buttons);
stage.setScene(new Scene(root));
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
在点击“下一页”后,我希望“上一页”将球移回原始位置(实际上是x减10,y减10),而不是倒放“下一页”的动画。
在实践中,我的动画会对场景图中的不同对象进行动画处理,它们可以是并行/顺序转换。对于列表,我保留一个当前位置索引i,并执行以下操作:
next.setOnAction(e -> {
Animation move = list.get(i);
move.setRate(1);
move.play();
i++;
});
previous.setOnAction(e -> {
i--;
Animation move = list.get(i);
move.setRate(-1);
move.play();
});
为了尝试反转之前的动画。
我该怎么做呢?
澄清一下,我的列表是 Animation
类型。 TranslateTransition
只是一个例子。
SequentialTransition
或ParallelTransition
的所有部分都是“绝对”的而不是“相对”的,就应该可以做到。autoReverse
和你的代码之间的区别在于,Transition
知道如何正确地反转自己,但是如果你想要完全控制动画播放的方向(RotateTransition
需要不同的反转逻辑,而TranslateTransition
则不同),您需要自行实现此逻辑。 - fabian