【问题标题】:Timelines JavaFX时间线 JavaFX
【发布时间】:2020-12-17 17:57:41
【问题描述】:

如何在关键帧之间添加延迟?如果卡片不匹配,我希望用户在卡片关闭前能看到卡片正面 0.6 秒。这是代码 我试过底部的那个,但它不起作用。

KeyFrame start = new KeyFrame(
            Duration.ZERO,
            new KeyValue(imageView.scaleXProperty(), 1.0));
    KeyFrame middle = new KeyFrame(
            Duration.millis(150),
            e -> imageView.setImage(image),
            new KeyValue(imageView.scaleXProperty(), 0.0)
    );
    KeyFrame end = new KeyFrame(
            Duration.millis(300),
            new KeyValue(imageView.scaleXProperty(), 1.0));
    new Timeline(start, middle, end).play();



KeyFrame start = new KeyFrame(
            Duration.ZERO,
            new KeyValue(imageView.scaleXProperty(), 1.0));
    KeyFrame middle = new KeyFrame(
            Duration.millis(150),
            e -> imageView.setImage(image),
            new KeyValue(imageView.scaleXProperty(), 0.0)
    );
    KeyFrame delay = new KeyFrame(
            Duration.millis(600)
    );
    KeyFrame end = new KeyFrame(
            Duration.millis(300),
            new KeyValue(imageView.scaleXProperty(), 1.0));
    new Timeline(start, middle,delay, end).play();

【问题讨论】:

  • 只需添加另一个具有相同KeyValueKeyFrame
  • 我试图在它们之间添加它,但由于它没有任何操作,所以它不起作用
  • 它不应该需要一个动作来工作。很难想象哪个框架在这里做什么,因为这不是完整的代码。您可以发布您尝试的代码吗?
  • 我当然编辑了帖子
  • 所有的持续时间都是相对于时间线的开始计算的,所以上面的持续时间应该是不正确的。

标签: java user-interface javafx


【解决方案1】:

注意:以下假设卡片“缩小”和“缩小”。考虑到您在问题中使用Duration.ZERO,我不确定这实际上是您想要的。如果您希望卡片立即弹出,请删除缩小动画;只需显示卡片然后开始动画(PauseTransition 是顺序设置中的第一个)。


使用SequentialTranitionPauseTransitionScaleTransition 对您有利。例如:

ScaleTransition start = new ScaleTransition(Duration.millis(150));
start.setToX(1.0);

// plays after 'start' finishes
PauseTransition middle = new PauseTransition(Duration.millis(600));

// plays after 'middle' finishes
ScaleTransition end = new ScaleTransition(Duration.millis(300));
end.setToX(0.0);

// only set the node on the SequentialTransition
SequentialTransition animation = new SequentialTransition(imageView, start, middle, end);

如果您希望比例转换具有相同的持续时间,则可以将上述简化为:

ScaleTransition scale = new ScaleTransition(Duration.millis(150));
scale.setFromX(0.0);
scale.setToX(1.0);

// Set to half the desired time because of the auto-reverse configured below
PauseTransition pause = new PauseTransition(Duration.millis(300));

// Plays 'scale' forwards, followed by 'pause', then plays 'pause' backwards
// followed by `scale`. That's why 'pause' has its duration set to half the full
// desired duration (it's played forwards then immediately backwards for a total
// of 600 ms).
SequentialTransition animation = new SequentialAnimation(imageView, scale, pause);
animation.setAutoReverse(true);
animation.setCycleCount(2);

【讨论】:

  • 所以我在图片右侧添加了 MouseClick 侦听器,当我单击它们时它会打开。如果打开了 2 张卡片并且它们不相同,则关闭。我想在第二张卡打开后等待 0.6 秒,然后将其关闭。
  • 我尝试将暂停的位置切换到开始
  • 其实效果很好,谢谢楼主
  • 很高兴它为您工作。请注意,您还可以使用ParellelTransition,因为您同时隐藏了两张卡片(创建两个SequentialTransitions 并将它们放入ParellelTransition)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-28
  • 2014-04-24
  • 1970-01-01
  • 2013-08-20
  • 2018-05-19
  • 1970-01-01
相关资源
最近更新 更多