JavaFX - 水平选框文本 [英] JavaFX - horizontal marquee text
问题描述
我正在尝试实现类似于选框的效果 - 长线(在我的情况下)的文字是在水平轴上移动。我设法让它工作,但我不能称之为令人满意。
I am trying to achieve effect similar to marquee - line of long (in my case) text which is moved in horizontal axis. I managed to get it work, but I can't call it satisfactory.
我的控制器
类如下所示:
@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
是绑定的一些动态更新的文本源,因此它包含不同数量的文本。
newsFeedText
is binded to some text source which is dynamically updated, so it contains various amount of text.
我的代码至少有两个缺点:
My code has at least two drawbacks:
- 转换从
-width
转换为+ width
;width
是显示器的分辨率宽度
- Transition goes from
-width
to+width
;width
is monitor's resolution width
有时候文字不会是如果窗口没有经过全面筛选,则完全可见。
如果文本更长并且 newsFeedText
宽度将大于显示器的分辨率宽度,那么转换将消失一半(仍然在屏幕上)。
There will be moments when text will not be visible at all if window is not full-screened.
If text will be longer and newsFeedText
width will be greater than monitor's resolution width then transition will disappear "in half" (still being on a screen).
- 目前
持续时间
不依赖于newsFeedText的宽度
。
- Currently
Duration
is not dependent on a width ofnewsFeedText
.
现在,它没什么问题,但是如果转换的来自X
和 toX
是动态计算的,然后它将导致各种速度的选框。
Now, it's nothing worng, but if transition's fromX
and toX
were be dynamically calculated then it will result in various speeds of marquee.
如何摆脱这些弊端?
推荐答案
我有管理它工作,任何重新计算只能在转换停止后发生,所以我们不能将其 cycleCount
设置为 Timeline.INDEFINITE
。我的要求是我可以更改组件内的文本,因此有fxml接线:
I have managed it to work, any recalculations can happen only after transition is stopped so we cannot set its cycleCount
to Timeline.INDEFINITE
. My requirement was that I could change text inside component so there are fxml wirings:
@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()
is:
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));
}
这篇关于JavaFX - 水平选框文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!