我有一个动画列表,我希望能够通过单击“下一个”按钮来播放它们,然后通过单击“上一个”按钮来播放它们。所以我可以播放第一个动画,然后播放第二个动画,然后向后播放第二个动画并到达只播放第一个动画后的位置。
我的问题是动画完成后无法反转。我知道我可以设置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只是一个例子。
相关分类