【问题标题】:JavaFX How do you bind an animation to a certain value?JavaFX 如何将动画绑定到某个值?
【发布时间】:2018-06-01 08:55:10
【问题描述】:

我使用 JavaFX 制作了一个应用程序,根据应用程序中的某些值,您可以在其中有一张带有动画内容的图片。我唯一的问题:我真的不知道如何将值绑定到动画,所以动画会根据输入的值移动。例如:如果值为 10,则动画应该快速移动。如果值为 1,则动画应该移动缓慢。这是动画的代码(它是烟雾,从房子里出来):

    smoke = new Image(HydroControl.class.getResource("/imgs/smoke.png").toExternalForm());
    smokeView = new ImageView(smoke);
    smokeView.setX(-190);
    smokeView.setY(60);

    Path path3 = new Path();
    path3.getElements().add(new MoveTo(1,-60));
    path3.getElements().add(new CubicCurveTo(0, 0, 0, 0, 0, 0));
    PathTransition pathTransition3 = new PathTransition();
    pathTransition3.setDuration(Duration.millis(2000));
    pathTransition3.setPath(path3);
    pathTransition3.setNode(smokeView);
    pathTransition3.setCycleCount(Timeline.INDEFINITE);
    pathTransition3.setAutoReverse(true);
    pathTransition3.play();

这是带有值的代码:

public class PresentationModel {
private final DoubleProperty waterValue = new SimpleDoubleProperty();
private final DoubleProperty powerValue = new SimpleDoubleProperty();
private final BooleanProperty isOnValue = new SimpleBooleanProperty();

public double getWaterValue() {
    return waterValue.get();
}

public DoubleProperty waterValueProperty() {
    return waterValue;
}

public void setWaterValue(double waterValue) {
    this.waterValue.set(waterValue);
}

public double getPowerValue() {
    return powerValue.get();
}

public DoubleProperty powerValueProperty() {
    return powerValue;
}

public void setPowerValue(double powerValue) {
    this.powerValue.set(powerValue);
}

public boolean isIsOnValue() {
    return isOnValue.get();
}

public BooleanProperty isOnValueProperty() {
    return isOnValue;
}

public void setIsOnValue(boolean isOnValue) {
    this.isOnValue.set(isOnValue);
}
}

如果你能帮助我,我会很高兴。谢谢。

【问题讨论】:

  • 尝试使您的动画持续时间 (pathTransition3.setDuration(Duration.millis(2000));) 取决于您的应用程序价值。
  • 将动画的rate 属性设置(或绑定)为根据输入计算的值。速率越高,动画越快。

标签: java animation javafx binding


【解决方案1】:

通过Bindings 类的计算将转换的属性绑定到模型的值。例如:

pathTransition3.durationProperty().bind(Bindings.multiply(Bindings.multiply(model.powerProperty(), model.waterValueProperty()), 500d));

model.isOnValueProperty().addListener((v, o, n) -> {
    if (n) {     
      pathTransition3.play(); 
    } else { 
      pathTransition3.pause(); 
    }
});

【讨论】:

  • 不知何故我无法使用 pathTransition3 即使它在同一个类中。
猜你喜欢
  • 1970-01-01
  • 2013-03-22
  • 1970-01-01
  • 2018-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-07
  • 1970-01-01
相关资源
最近更新 更多