【问题标题】:Is there any way to implement this CSS animation in JavaFX?有没有办法在 JavaFX 中实现这个 CSS 动画?
【发布时间】:2020-11-12 12:22:14
【问题描述】:

html和css代码:

我从this post 获取了此代码

div {
    position: relative;
    display: inline-block;
    padding: 15px 70px;
    border: 5px solid #B17461;
    color: #B17461;
    font-size: 30px;
    font-family: arial;
    background-image: linear-gradient(#B17461, #B17461);
    background-position: 50% 50%;
    background-repeat: no-repeat;
    background-size: 0% 100%;
    transition: background-size .5s, color .5s;
  }
  div:hover {
    background-size: 100% 100%;
    color: #fff;
  }
<div>NEXT</div>

我想知道是否可以在 JavaFX 中实现这个动画?我尝试在一个按钮后面放置一个进度条,这两个按钮都包含在 StackPane 中。进度条的 .track 的背景颜色设置为透明,按钮的背景颜色设置为透明。但这似乎是一种低效的做事方式,老实说实施起来相当乏味。那么还有其他更适合动画的方法吗?

【问题讨论】:

    标签: css javafx


    【解决方案1】:

    这个简单的应用程序与您展示的几乎相同:

    package example;
    
    import javafx.animation.Animation;
    import javafx.animation.Transition;
    import javafx.application.Application;
    import javafx.geometry.Insets;
    import javafx.scene.Scene;
    import javafx.scene.control.Label;
    import javafx.scene.layout.*;
    import javafx.scene.paint.Color;
    import javafx.scene.paint.Paint;
    import javafx.scene.text.Font;
    import javafx.scene.text.TextAlignment;
    import javafx.stage.Stage;
    import javafx.util.Duration;
    
    public class Main extends Application {
    
        public static void main(String[] args) {
            launch(Main.class, args);
        }
    
        @Override
        public void start(Stage stage) throws Exception {
            final Pane root = new Pane();
            // this color will be used for the Text-Color (not hovered), Border and the Background-Color (on hover)
            final Paint color = Color.web("#B17461");
    
            // basic stuff: text, font and text-color
            final Label label = new Label("NEXT");
            label.setFont(Font.font("Arial", 50.0));
            label.setTextFill(color);
            label.setTextAlignment(TextAlignment.CENTER);
            // same padding as you had
            label.setPadding(new Insets(15, 70, 15, 70));
            // border with the same settings as you had (5px solid)
            label.setBorder(new Border(new BorderStroke(color, BorderStrokeStyle.SOLID, CornerRadii.EMPTY, new BorderWidths(5))));
    
            // maybe there's a better way to do this, but this one is the one I know. Simple Transition-Animation with a cycle of .5s
            // the interpolate-Method will be called by JavaFX on every frame with the current "progress" (from 0.0 to 1.0), so we simply calculate the background size in there
            final Animation animation = new Transition() {
                {
                    setCycleDuration(Duration.millis(500L));
                }
    
                @Override
                protected void interpolate(double progress) {
                    final double total = label.getWidth() / 2.0;
                    final double current = (1.0 - progress) * total;
    
                    label.setBackground(new Background(new BackgroundFill(color, CornerRadii.EMPTY, new Insets(0.0, current, 0.0, current))));
                }
            };
    
            // "hover": change text color and start the animation
            label.setOnMouseEntered(event -> {
                label.setTextFill(Color.web("#fff"));
                animation.playFromStart();
            });
    
            // "unhover": stop animation, reset background and text color
            label.setOnMouseExited(event -> {
                animation.stop();
                label.setBackground(null);
                label.setTextFill(color);
            });
    
          
            root.getChildren().add(label);
    
            stage.setScene(new Scene(root));
            stage.sizeToScene();
            stage.show();
        }
    }
    
    

    基本上我只是在标签上以编程方式完成了您在 css 中的所有内容。 对于动画,我使用了一个简单的Transition 结合标签背景的Insets 值。 Insets 描述了背景不应该填充多少空间,它就像一个填充但仅用于背景。

    【讨论】:

    • 你没有插入颜色。
    • 非常感谢!通过一些调整,它按我想要的方式工作。
    【解决方案2】:

    类似这样的东西,或者您可以修改它以调整背景插图,例如 codeflush.dev 的答案...

    public class Main extends Application {
    
        public static void main(String[] args) {
            launch(args);
        }
    
        @Override
        public void start(Stage stage) throws Exception {
            Rectangle rect = new Rectangle(300, 30, Color.web("B17461"));
            rect.setScaleX(0);
            Button button = new Button("A Button");
            StackPane pane = new StackPane(rect, button);
            pane.setBackground(new Background(new BackgroundFill(Color.BLACK, CornerRadii.EMPTY, Insets.EMPTY)));
    
            TranslateTransition trans = new TranslateTransition(Duration.millis(500), rect);
            trans.setFromX(-150);
            trans.setToX(0);
            ScaleTransition scale = new ScaleTransition(Duration.millis(500), rect);
            scale.setFromX(0);
            scale.setToX(1);
            FillTransition fade = new FillTransition(Duration.millis(500), rect, Color.web("B17461"), Color.WHITE);
            Transition t = new ParallelTransition(trans, scale, fade);
    
            pane.setOnMouseEntered(me -> {
                t.stop();
                t.setRate(1);
                t.play();
            });
            pane.setOnMouseExited(me -> {
                t.stop();
                t.setRate(-1);
                t.play();
            });
    
            Scene scene = new Scene(pane);
            stage.setTitle("Transition");
            stage.setScene(scene);
            stage.setMinWidth(300);
            stage.setMinHeight(60);
            stage.show();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-11-09
      • 1970-01-01
      • 2020-03-19
      • 2022-01-06
      • 2012-07-10
      • 2014-06-12
      • 1970-01-01
      • 2022-10-15
      相关资源
      最近更新 更多