Animation 带有PathTransition的javafx动画不平滑

Animation 带有PathTransition的javafx动画不平滑,animation,javafx,Animation,Javafx,我正在学习javafx.animation。我创建了一个椭圆和一个矩形,并使矩形沿着椭圆的边界移动。我让这个动画无限期地重复。问题是动画圈之间的过渡不平滑。这意味着在第一轮动画完成后,下一轮动画需要相当长的时间才能开始。这看起来很难看。有没有办法让它更平滑 先谢谢你 import javafx.application.Application; import javafx.event.EventHandler; import javafx.scene.input.MouseEvent; impor

我正在学习javafx.animation。我创建了一个椭圆和一个矩形,并使矩形沿着椭圆的边界移动。我让这个动画无限期地重复。问题是动画圈之间的过渡不平滑。这意味着在第一轮动画完成后,下一轮动画需要相当长的时间才能开始。这看起来很难看。有没有办法让它更平滑

先谢谢你

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Ellipse;
import javafx.scene.shape.Rectangle;
import javafx.animation.PathTransition;
import javafx.util.Duration;
import javafx.scene.paint.Color;
import javafx.animation.Timeline;

public class TestAnimation extends Application {
    @Override
    public void start(Stage stage){
        //create pane and nodes
        Pane pane = new Pane();
        Ellipse ellipse = new Ellipse(300, 300, 100, 200);
        ellipse.setFill(Color.BLACK);
        Rectangle rect = new Rectangle(50, 80);
        rect.setFill(Color.CRIMSON);
        pane.getChildren().addAll(ellipse, rect);

        //create animation
        PathTransition path = new PathTransition(Duration.millis(2400), ellipse, rect);
        path.setCycleCount(Timeline.INDEFINITE);
        //path.setAutoReverse(true);
        path.play();

        //define mouse event
        ellipse.setOnMousePressed(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                path.pause();
            }
        });

        ellipse.setOnMouseReleased(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                path.play();
            }
        });

        //set up stage
        Scene scene = new Scene(pane, 600, 600);
        stage.setScene(scene);
        stage.show();
    }
}
导入javafx.application.application;
导入javafx.event.EventHandler;
导入javafx.scene.input.MouseEvent;
导入javafx.stage.stage;
导入javafx.scene.scene;
导入javafx.scene.layout.Pane;
导入javafx.scene.shape.eliple;
导入javafx.scene.shape.Rectangle;
导入javafx.animation.PathTransition;
导入javafx.util.Duration;
导入javafx.scene.paint.Color;
导入javafx.animation.Timeline;
公共类测试扩展了应用程序{
@凌驾
公众假期开始(阶段){
//创建窗格和节点
窗格=新窗格();
椭圆=新椭圆(300300100200);
椭圆。设置填充(颜色。黑色);
矩形rect=新矩形(50,80);
直毛(颜色深红色);
pane.getChildren().addAll(椭圆,矩形);
//创建动画
PathTransition路径=新的PathTransition(持续时间.毫秒(2400),椭圆,矩形);
path.setCycleCount(Timeline.unfinite);
//path.setAutoReverse(true);
path.play();
//定义鼠标事件
setOnMousePressed(新的EventHandler(){
@凌驾
公共无效句柄(MouseeEvent事件){
path.pause();
}
});
setOnMouseReleased(新的EventHandler(){
@凌驾
公共无效句柄(MouseeEvent事件){
path.play();
}
});
//搭建舞台
场景=新场景(窗格,600600);
舞台场景;
stage.show();
}
}

这是由插值器引起的,插值器通过加速和减速运动来平滑动画的开始和停止。设置path.setInterpolator(Interpolator.LINEAR)应该已经改变了这一点,但由于某些原因没有改变。也许其他人可以告诉我们这是为什么:)这对我来说很好…@johnRW,这对我来说确实有效。我设置了path.setInterpolator(Interpolator.LINEAR),现在它非常平滑。非常感谢。@JohnRW我想你应该把这篇文章作为回答。