【发布时间】: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