JavaFX如何将动画绑定到某个值?

时间:2018-06-01 08:55:10

标签: java animation javafx binding

我已经使用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);
}
}

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

1 个答案:

答案 0 :(得分:0)

使用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(); 
    }
});