[英]JavaFX animation restore to original state
我的目标是在节点上创建一些动画(例如淡入淡出过渡),以临时通知正在发生的事情。 我希望动画完全消失,就像动画结束时从未发生过一样。
下面的代码是我遇到的问题的一个示例。 在当前状态下,当按下按钮以停止该过程时,按钮仅停留在其当前的不透明度。 如果注释行未注释,则按钮不再停留在当前的不透明度,而是更新为看起来正确。 然后我的问题是,当再次按下该按钮时,默认样式表(JavaFX 8为Modena.css)的CSS不透明度不再生效。
我做错了什么吗,还是有更好的办法?
package gui.control.custom;
import javafx.animation.Animation;
import javafx.animation.FadeTransition;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.util.Duration;
public class Test extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Stage stage = new Stage();
HBox box = new HBox();
streamButton = new Button("Start");
streamButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
if (started) {
stopProcess();
} else {
startProcess();
}
}
});
box.getChildren().add(streamButton);
stage.setScene(new Scene(box));
stage.show();
}
FadeTransition ft;
Button streamButton;
boolean started = false;
private void startProcess() {
streamButton.setDisable(true);
new Thread() {
@Override
public void run() {
try {
Thread.sleep(3000);
} catch (InterruptedException ex) {
}
Platform.runLater(() -> {
started = true;
streamButton.setText("Stop");
streamButton.setDisable(false);
startButtonAnim();
});
}
}.start();
}
private void stopProcess() {
streamButton.setText("Start");
stopButtonAnim();
started = false;
}
private void startButtonAnim() {
ft = new FadeTransition(Duration.millis(500), streamButton);
ft.setFromValue(1.0);
ft.setToValue(0.3);
ft.setCycleCount(Animation.INDEFINITE);
ft.setAutoReverse(true);
ft.play();
}
private void stopButtonAnim() {
ft.stop();
//streamButton.setOpacity(1);
}
public static void main(String[] args) {
launch();
}
}
另一个想法:
我在javadoc中找到了此方法: getCurrentRate() ,它在反转时应该给您负面的结果,因此代码如下所示:
private void stopButtonAnim() {
while(ft.getCurrentRate>=0); //waiting till animation goes (skips if already reversing)
while(ft.getCurrentRate<=0); //and till reverse
ft.stop(); //then stop
streamButton.setOpacity(1); //make it 100% ;)
}
也许您必须将Thread.sleep(int)
添加到while
周期
我认为最好的解决方案是在停止Animation
之前立即使用jumpTo(Duration duration)
。 将持续Duration.ZERO
设置为Duration.ZERO
。
Circle circle2 = new Circle(250, 120, 80);
circle2.setFill(Color.RED);
circle2.setStroke(Color.BLACK);
FadeTransition fade = new FadeTransition();
fade.setDuration(Duration.millis(5000));
fade.setFromValue(10);
fade.setToValue(0.1);
fade.setCycleCount(1000);
fade.setAutoReverse(true);
fade.setNode(circle2);
fade.play();
Button btnStop = new Button("Stop");
btnStop.setOnAction((event) -> {
fade.jumpTo(Duration.ZERO);
fade.stop();
});
我会尝试简单的stop();
这个insetad stop();
这条线
setOnFinished(e->tryToStop());
并将此方法创建为:
public void tryToStop(){
if(!started)
fm.stop();
}
stopProcess()
方法更改了started
变量,因此在以下两种情况下它将停止:
和
未经测试,只是一个想法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.