如何使用JavaFX中的CSS制作动画?

前端之家收集整理的这篇文章主要介绍了如何使用JavaFX中的CSS制作动画?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想通过改变其样式类来改变节点的风格.
  1. Button button = new Button();
  2. button.getStyleClass().add("class1")
  3. button.setOnMouseClicked(new EventHandler<MouseEvent>() {
  4. @Override
  5. public void handle(MouseEvent mouseEvent) {
  6. button.getStyleClass().add("class2");
  7. }
  8. });

是否有可能逐渐改变风格,进行转型?

@R_502_323@

Is it possible to change style gradually,for example make some transition?

是.

您将需要使用setStyle而不是样式类,因为类将是在css中定义的静态事件.在JavaFX css中没有直接的支持动画.您需要在Java代码中执行动画步骤来修改css样式.

当您想使用css执行转换时,我只会真正推荐此方法,因为没有相应的Java API可以轻松获得.

要处理动画,您可以使用标准的javafx动画Timeline来处理CSS样式属性所依赖的属性.

例如,将您的样式属性绑定到字符串.然后在时间轴中更改要更改的组件(在本例中为colorStringProperty).

  1. warningButton.styleProperty().bind(
  2. new SimpleStringProperty("-fx-base: ")
  3. .concat(colorStringProperty)
  4. .concat(";")
  5. .concat("-fx-font-size: 20px;")
  6. );

这是一个示例,它使用css闪烁一个按钮,当按下时,它的基本颜色从灰色逐渐变为红色.

  1. import javafx.animation.*;
  2. import javafx.application.Application;
  3. import javafx.beans.property.*;
  4. import javafx.beans.value.*;
  5. import javafx.event.*;
  6. import javafx.scene.Scene;
  7. import javafx.scene.control.Button;
  8. import javafx.scene.image.*;
  9. import javafx.scene.layout.StackPane;
  10. import javafx.scene.paint.Color;
  11. import javafx.stage.Stage;
  12. import javafx.util.Duration;
  13.  
  14. /** Shows how you can modify css styles dynamically using a timeline. */
  15. public class Warning extends Application {
  16.  
  17. private static final String BACKGROUND = "http://bobgreiner.tripod.com/1cc2ce10.jpg";
  18.  
  19. @Override
  20. public void start(Stage stage) throws Exception{
  21. final ObjectProperty<Color> warningColor = new SimpleObjectProperty<>(Color.GRAY);
  22. final StringProperty colorStringProperty = createWarningColorStringProperty(warningColor);
  23.  
  24. StackPane layout = new StackPane();
  25. layout.getChildren().addAll(
  26. new ImageView(new Image(BACKGROUND)),createWarningButton(
  27. warningColor,colorStringProperty
  28. )
  29. );
  30. stage.setScene(new Scene(layout));
  31. stage.show();
  32. }
  33.  
  34. private StringProperty createWarningColorStringProperty(final ObjectProperty<Color> warningColor) {
  35. final StringProperty colorStringProperty = new SimpleStringProperty();
  36. setColorStringFromColor(colorStringProperty,warningColor);
  37. warningColor.addListener(new ChangeListener<Color>() {
  38. @Override
  39. public void changed(ObservableValue<? extends Color> observableValue,Color oldColor,Color newColor) {
  40. setColorStringFromColor(colorStringProperty,warningColor);
  41. }
  42. });
  43.  
  44. return colorStringProperty;
  45. }
  46.  
  47. private Button createWarningButton(final ObjectProperty<Color> warningColor,StringProperty colorStringProperty) {
  48. final Button warningButton = new Button("Warning! Warning!");
  49. warningButton.styleProperty().bind(
  50. new SimpleStringProperty("-fx-base: ")
  51. .concat(colorStringProperty)
  52. .concat(";")
  53. .concat("-fx-font-size: 20px;")
  54. );
  55.  
  56. warningButton.setOnAction(new EventHandler<ActionEvent>() {
  57. @Override
  58. public void handle(ActionEvent actionEvent) {
  59. Timeline flash = new Timeline(
  60. new KeyFrame(Duration.seconds(0),new KeyValue(warningColor,Color.GRAY,Interpolator.LINEAR)),new KeyFrame(Duration.seconds(0.25),new KeyFrame(Duration.seconds(1),Color.RED,new KeyFrame(Duration.seconds(1.25),Interpolator.LINEAR))
  61. );
  62. flash.setCycleCount(6);
  63. flash.setAutoReverse(true);
  64. flash.play();
  65. }
  66. });
  67.  
  68. return warningButton;
  69. }
  70.  
  71. private void setColorStringFromColor(StringProperty colorStringProperty,ObjectProperty<Color> color) {
  72. colorStringProperty.set(
  73. "rgba("
  74. + ((int) (color.get().getRed() * 255)) + ","
  75. + ((int) (color.get().getGreen() * 255)) + ","
  76. + ((int) (color.get().getBlue() * 255)) + ","
  77. + color.get().getOpacity() +
  78. ")"
  79. );
  80. }
  81.  
  82. public static void main(String[] args) {
  83. launch(args);
  84. }
  85. }

猜你在找的Java相关文章