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.
My Controller
class looks as below:
@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
is binded to some text source which is dynamically updated, so it contains various amount of text.
My code has at least two drawbacks:
- Transition goes from
-width
to +width
; width
is monitor's resolution width
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).
- Currently
Duration
is not dependent on a width of newsFeedText
.
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.
How to get rid of these drawbacks?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…