我希望在javafx中让标签在0.1秒内闪烁。该文本显示在后台运行的ImageView gif之上。我将如何进行此操作,或者您对最佳方法有任何建议?
由于
答案 0 :(得分:5)
尽管@ fabian的解决方案很不错,但在这种情况下,您可以使用FadeTransition。它改变了节点的不透明度,非常适合你的情况。
FadeTransition fadeTransition = new FadeTransition(Duration.seconds(0.1), label);
fadeTransition.setFromValue(1.0);
fadeTransition.setToValue(0.0);
fadeTransition.setCycleCount(Animation.INDEFINITE);
<强> MCVE 强>
import javafx.animation.Animation;
import javafx.animation.FadeTransition;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.Duration;
public class LabelBlink extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Label label = new Label("Blink");
FadeTransition fadeTransition = new FadeTransition(Duration.seconds(0.1), label);
fadeTransition.setFromValue(1.0);
fadeTransition.setToValue(0.0);
fadeTransition.setCycleCount(Animation.INDEFINITE);
fadeTransition.play();
Scene scene = new Scene(new StackPane(label));
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
Application.launch();
}
}
答案 1 :(得分:3)
使用时间表:
Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(0.05), evt -> label.setVisible(false)),
new KeyFrame(Duration.seconds( 0.1), evt -> label.setVisible(true)));
timeline.setCycleCount(Animation.INDEFINITE);
timeline.play();