JavaFX-水平字幕文本
问题内容:
我正在尝试实现类似于字幕的效果-
长行(在我的情况下)的行在水平轴上移动。我设法使它起作用,但我不能称其令人满意。
我的Controller
课如下:
@FXML
private Text newsFeedText;
(...)
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
TranslateTransition transition = TranslateTransitionBuilder.create()
.duration(new Duration(7500))
.node(newsFeedText)
.interpolator(Interpolator.LINEAR)
.cycleCount(Timeline.INDEFINITE)
.build();
GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
int width = gd.getDisplayMode().getWidth();
transition.setFromX(width);
transition.setToX(-width);
transition.play();
}
newsFeedText
绑定到动态更新的某些文本源,因此其中包含各种文本。
我的代码至少有两个缺点:
- 过渡从
-width
到+width
;width
是显示器的分辨率宽度
如果窗口未全屏显示,则有时文本根本不可见。如果文本较长且newsFeedText
宽度大于监视器的分辨率宽度,则过渡将消失“一半”(仍在屏幕上)。
- 目前
Duration
不依赖于的宽度newsFeedText
。
现在,这没什么大不了的,但是如果对转折fromX
和toX
进行动态计算,那么它将导致各种字幕的速度。
如何摆脱这些弊端?
问题答案:
我设法使其正常工作,只有在过渡停止后才能进行任何重新计算,因此我们无法将其设置cycleCount
为Timeline.INDEFINITE
。我的要求是我可以更改组件内部的文本,所以有fxml接线:
@FXML
private Text node; // text to marquee
@FXML
private 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));
}