以下代码应该在两个渐变之间平滑地淡入矩形:
public class FXTest extends Application {
@Override
public void start(Stage primaryStage) {
StackPane root = new StackPane();
Rectangle rect = new Rectangle(200, 100, new LinearGradient(0, 0, 1, 1, true, CycleMethod.REPEAT, new Stop(0, Color.BLACK), new Stop(1, Color.RED)));
root.getChildren().add(rect);
Scene scene = new Scene(root, 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
getFillTimeline(rect, new LinearGradient(0, 0, 1, 1, true, CycleMethod.REPEAT, new Stop(0, Color.RED), new Stop(1, Color.BLACK))).play();
}
private Timeline getFillTimeline(Shape node, Paint newPaint) {
Timeline t = new Timeline();
t.getKeyFrames().add(new KeyFrame(Duration.ZERO, new KeyValue(node.fillProperty(), node.getFill())));
t.getKeyFrames().add(new KeyFrame(Duration.seconds(3), new KeyValue(node.fillProperty(), newPaint)));
return t;
}
}
如果我在上面的例子中指定纯色,那么淡入淡出就没有问题,漂亮且平滑。但是,如果我使用渐变(如上面的代码所示),则不会发生淡入淡出,渐变只会在 3 秒后突然切换(或无论时间线的持续时间是多少。)
当使用渐变而不是简单的颜色时,如何实现逐渐淡入淡出?我是否缺少一些逻辑,为什么这不符合我的想法,或者这是一个错误?
无论哪种情况,实现此效果的最佳解决方法是什么?
最佳答案
我认为它不知道如何在根本不同的油漆之间进行平滑过渡。尝试将纯色设置为渐变,其中起始颜色和结束颜色都是相同的颜色。
关于java - 渐变之间平滑淡入淡出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14243116/