我设法使其正常工作,只有在过渡停止后才能进行任何重新计算,因此我们无法将其设置
cycleCount为
Timeline.INDEFINITE。我的要求是我可以更改组件内部的文本,所以有fxml接线:
@FXMLprivate Text node; // text to marquee@FXMLprivate Pane parentPane; // pane on which text is placed
起作用的代码是:
transition = TranslateTransitionBuilder.create() .duration(new Duration(10)) .node(node) .interpolator(Interpolator.LINEAR) .cycleCount(1) .build();transition.setonFinished(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent actionEvent) { rerunAnimation(); }});rerunAnimation();在哪里
rerunAnimation():
private void rerunAnimation() { transition.stop(); // if needed set different text on "node" recalculateTransition(); transition.playFromStart();}并且
recalculateTransition()是:
private void recalculateTransition() { transition.setToX(node.getBoundsInLocal().getMaxX() * -1 - 100); transition.setFromX(parentPane.widthProperty().get() + 100); double distance = parentPane.widthProperty().get() + 2 * node.getBoundsInLocal().getMaxX(); transition.setDuration(new Duration(distance / SPEED_FACTOR));}


