gpt4 book ai didi

JavaFX 动画恢复到原始状态

转载 作者:行者123 更新时间:2023-11-29 09:28:55 25 4
gpt4 key购买 nike

我的目标是在节点上制作一些动画(例如淡入淡出过渡),作为正在发生某事的临时通知。我希望动画完全消失,就像事情结束时从未发生过一样。

下面截取的代码是我遇到的问题的示例。在当前状态下,当按下按钮停止进程时,按钮将保持当前的不透明度。如果注释行未被注释,按钮将不再保持当前的不透明度,而是更新为看起来正确。我的问题是,当再次按下按钮时,默认样式表(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();
}

}

最佳答案

我认为最好的解决方案是使用 jumpTo(Duration duration)就在您停止 Animation 之前。将持续时间设置为 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();
});

关于JavaFX 动画恢复到原始状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31441279/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com