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进行动态计算,那么它将导致各种字幕的速度。
如何摆脱这些弊端?

浙公网安备 33010602011771号