java - 使用动画更改对象的属性
问题描述
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
正确书写以创建动画?
请帮忙。
谢谢
解决方案
Effect
从 JavaFX 12 开始,没有一个类实现Interpolatable
. 这意味着动画不能逐渐从无效果到结束效果,因此您的动画会从null
跳到BoxBlur
。您应该为实例的属性设置动画BoxBlur
,而不是effect
属性本身。
这是一个例子:
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();
}
}
推荐阅读
- python - 多处理模块 OpenCV 应用程序(Blank Camera Feed)
- sql - 如何使用 SQL 计算所有 NULL 值,没有列名?
- json - 我如何将表单组数据存储到角度 12 的 json 文件中
- reactjs - React 组件重新渲染
- python - 尝试在使用命令时让机器人记录一些东西 discord.py
- python - 具有多个输入和目标的 TensorFlow 数据集
- javascript - React:为什么运行 .html 文件后没有输出?
- delphi - 传递带有“const”的 DLL 函数参数是否等同于指针?
- javascript - AngularCLI 中的继承模板
- python - MinMaxScaler 没有正确缩放