如何使用 JavaFX 制作离散颜色翻转动画?

发布于 2024-08-02 20:40:28 字数 107 浏览 0 评论 0原文

今天刚开始使用 JavaFX,不知道如何对对象进行离散颜色翻转。我可以设置一个具有两个关键帧的时间轴,并让它设置一个对象绑定到它的填充变量的颜色值...但是当我想要的是离散触发器时,我会得到插值颜色。

Just started with JavaFX today and can't figure out how to do a discrete color flip on an object. I can set up a Timeline with two keyframes and have it set a color value that an object binds to it's fill variable... but I get interpolated colors when what I want is a discrete flip flop.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

娇纵 2024-08-09 20:40:28

或者您可以在 KeyFrame 的 fillColor 键值上使用离散插值器。

Timeline {
    repeatCount: Timeline.INDEFINITE
    keyFrames: [
        at(0s) { fillColor => Color.BLUE tween Interpolator.DISCRETE; }
        at(1s) { fillColor => Color.RED tween Interpolator.DISCRETE; }
    ]
}

Or you could use a DISCRETE interpolator on KeyFrame's key value for fillColor.

Timeline {
    repeatCount: Timeline.INDEFINITE
    keyFrames: [
        at(0s) { fillColor => Color.BLUE tween Interpolator.DISCRETE; }
        at(1s) { fillColor => Color.RED tween Interpolator.DISCRETE; }
    ]
}
驱逐舰岛风号 2024-08-09 20:40:28

这可能更符合您的需求,但它不会无限期地重复 - 可能是一个错误?
另外,我怀疑有更简洁的方法可以做到这一点。

import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.shape.Rectangle;
import javafx.scene.paint.Color;
import javafx.animation.transition.PauseTransition;
import javafx.animation.transition.SequentialTransition;

import javafx.animation.Timeline;

var fillColor: Color;

Stage {
    title : "MyApp"
    scene: Scene {
        width: 400
        height: 200
        content: [
                Rectangle {
                x: 10, y: 10
                width: 140, height: 90
                fill: bind fillColor
            }
            ]
    }
}

var seqTransition = SequentialTransition {
    repeatCount: Timeline.INDEFINITE
      content: [
        PauseTransition {
            duration: 1s
            action: function():Void {
                fillColor = Color.BLUE;
            }
        },
        PauseTransition {
            duration: 1s
            action: function():Void {
                fillColor = Color.RED;
            }
        }
      ]
    }
    seqTransition.play();

This may be more what you're looking for, but it doesn't repeat indefinitely - might be a bug?
Also, I suspect that there are neater ways of doing this.

import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.shape.Rectangle;
import javafx.scene.paint.Color;
import javafx.animation.transition.PauseTransition;
import javafx.animation.transition.SequentialTransition;

import javafx.animation.Timeline;

var fillColor: Color;

Stage {
    title : "MyApp"
    scene: Scene {
        width: 400
        height: 200
        content: [
                Rectangle {
                x: 10, y: 10
                width: 140, height: 90
                fill: bind fillColor
            }
            ]
    }
}

var seqTransition = SequentialTransition {
    repeatCount: Timeline.INDEFINITE
      content: [
        PauseTransition {
            duration: 1s
            action: function():Void {
                fillColor = Color.BLUE;
            }
        },
        PauseTransition {
            duration: 1s
            action: function():Void {
                fillColor = Color.RED;
            }
        }
      ]
    }
    seqTransition.play();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文