JavaFX动画慢速移动图像

JavaFX动画慢速移动图像,javafx,javafx-8,Javafx,Javafx 8,我试图制作一个滚动背景;希望是把它从一端到尾部连接起来,这样它就可以绕着同一条“丝带”旋转,我把它放在游戏的后面 我一直在寻找正确的方法来实现动画效果。根据我的示例,我可以使用AnimationTimer,也可以使用时间线。但是,我希望将动画速度移动得慢得多,延迟对计时器的更新会导致抖动。我确实添加了一个int变量,如果传递的小于“x”毫秒,它就会返回,但看起来不太好 完成这项任务的更好方法是什么 import javafx.animation.AnimationTimer; import ja

我试图制作一个滚动背景;希望是把它从一端到尾部连接起来,这样它就可以绕着同一条“丝带”旋转,我把它放在游戏的后面

我一直在寻找正确的方法来实现动画效果。根据我的示例,我可以使用AnimationTimer,也可以使用时间线。但是,我希望将动画速度移动得慢得多,延迟对计时器的更新会导致抖动。我确实添加了一个int变量,如果传递的小于“x”毫秒,它就会返回,但看起来不太好

完成这项任务的更好方法是什么

import javafx.animation.AnimationTimer;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
import javafx.scene.layout.*;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;

class ImageContainer extends VBox {
    int w, h;
    int sectionScrollWidth = 1;
    int sections;
    int sectionCounter = 0;
    Image image;
    Canvas canvas;

public ImageContainer() {
    setVisible(true);
    load();
    w = (int) image.getWidth();
    h = (int) image.getHeight();
    canvas = new Canvas(w, h);
    getChildren().add(canvas);
    sections = w / sectionScrollWidth;
    GraphicsContext gc = canvas.getGraphicsContext2D();
    canvas.setVisible(true);
    gc.drawImage(image, 0, 0, w, h);
    setPrefSize(w, h);

    final long startNanoTime = System.nanoTime();

    new AnimationTimer() {
        public void handle(long currentNanoTime) {
            sectionCounter = sectionCounter - sectionScrollWidth;
            canvas.setTranslateX(sectionCounter);
        }
    }.start();

//        KeyValue kv1 = new KeyValue(canvas.translateXProperty(), 0);
//        KeyValue kv2 = new KeyValue(canvas.translateXProperty(), 2000);
//        KeyFrame kf1 = new KeyFrame(Duration.millis(3000), kv1, kv2);
//        Timeline translate = new Timeline();
//        translate.getKeyFrames().add(kf1);
//        translate.play();

}

public void load() {
    Path imagePath = Paths.get("./src/main/resources/ribbonImages/clouds.png");
    File f = imagePath.toFile();
    assert f.exists();
    image = new Image(f.toURI().toString());
}
}
if(((System.nanoTime()-startNanoTime)/100000000.0)
 if (((System.nanoTime()-startNanoTime)/1000000000.0)<0.05) {
                return;
            }
            else {
                startNanoTime = System.nanoTime();
            }