首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用动画更改对象的属性

使用动画更改对象的属性
EN

Stack Overflow用户
提问于 2019-08-18 22:07:09
回答 1查看 294关注 0票数 1
代码语言:javascript
复制
import javafx.animation.FillTransition;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.ParallelTransition;
import javafx.animation.Timeline;
import javafx.scene.Scene;
import javafx.scene.SceneAntialiasing;
import javafx.scene.effect.BoxBlur;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.util.Duration;

public class NewFXMain extends javafx.application.Application {

    @Override
    public void start(Stage primaryStage) {
        var pane = new Pane();
        var rectangle = new Rectangle(300, 100, 400, 400);
        var text = new Text(100, 100, "Java");
        rectangle.setOnMouseEntered((event) -> {
            var fill = new FillTransition(Duration.millis(1000), rectangle, (Color) rectangle.getFill(), Color.GREEN);
            var timeline = new Timeline(new KeyFrame(Duration.millis(1000), new KeyValue(text.effectProperty(), new BoxBlur(4, 4, 4))));
            var transition = new ParallelTransition(fill, timeline);
            transition.play();
        });
        pane.getChildren().addAll(rectangle, text);
        Scene scene = new Scene(pane, 1000, 720, true, SceneAntialiasing.BALANCED);
        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

此代码应创建一个动画以更改矩形的颜色并模糊文本。

然而,虽然FillTransition工作得很好,但BoxBlur效果在1秒后没有动画就会发生。

如何正确地编写effectProperty以创建动画?

请帮帮忙。

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-08-18 22:59:52

在JavaFX 12中,没有一个JavaFX类实现Interpolatable。这意味着动画不能从无效果逐渐过渡到结束效果,因此动画从null跳转到BoxBlur。您应该动画BoxBlur实例的属性,而不是effect属性本身。

下面是一个例子:

代码语言:javascript
复制
import javafx.animation.FillTransition;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.ParallelTransition;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.effect.BoxBlur;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.util.Duration;

public final class App extends Application {

    @Override
    public void start(Stage primaryStage) {
        var rectangle = new Rectangle(150, 100);

        var text = new Text("Hello, World!");
        text.setFill(Color.WHITE);

        var effect = new BoxBlur(0, 0, 1);
        text.setEffect(effect);

        var animation = new ParallelTransition(
                new FillTransition(Duration.seconds(1), rectangle, Color.BLACK, Color.GREEN),
                new Timeline(
                        new KeyFrame(Duration.ZERO,
                                new KeyValue(effect.widthProperty(), 0),
                                new KeyValue(effect.heightProperty(), 0)
                        ),
                        new KeyFrame(Duration.seconds(1),
                                new KeyValue(effect.widthProperty(), 10),
                                new KeyValue(effect.heightProperty(), 10)
                        )
                )
        );
        rectangle.setOnMouseEntered(event -> {
            event.consume();
            animation.setRate(1);
            animation.play();
        });
        rectangle.setOnMouseExited(event -> {
            event.consume();
            animation.setRate(-1);
            animation.play();
        });

        primaryStage.setScene(new Scene(new StackPane(rectangle, text), 500, 300));
        primaryStage.show();
    }

}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57548716

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档