java - JavaFX - horizontal marquee text -
i trying achieve effect similar marquee - line of long (in case) text moved in horizontal axis. managed work, can't call satisfactory.
my controller
class looks 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
binded text source dynamically updated, contains various amount of text.
my code has @ least 2 drawbacks:
- transition goes
-width
+width
;width
monitor's resolution width
there moments when text not visible @ if window not full-screened. if text longer , newsfeedtext
width greater monitor's resolution width transition disappear "in half" (still being on screen).
- currently
duration
not dependent on width ofnewsfeedtext
.
now, it's nothing worng, if transition's fromx
, tox
dynamically calculated result in various speeds of marquee.
how rid of these drawbacks?
i have managed work, recalculations can happen after transition stopped cannot set cyclecount
timeline.indefinite
. requirement change text inside component there fxml wirings:
@fxml private text node; // text marquee @fxml private pane parentpane; // pane on text placed
the code works is:
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();
where rerunanimation()
is:
private void rerunanimation() { transition.stop(); // if needed set different text on "node" recalculatetransition(); transition.playfromstart(); }
and 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)); }
Comments
Post a Comment