如何使使用路径转换的javafx动画平滑? [英] How to make the javafx animation using path transition smooth?

查看:116
本文介绍了如何使使用路径转换的javafx动画平滑?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在的目标是制作一个小圆圈的动画,沿着大圆圈的路径前进.将来,我将需要使用滑块动态更改旋转速度,但是现在这不是问题.我不知道如何使该动画流畅?我的意思是说,我希望我的小圆圈在动画结束时不减速.使用路径转换可以做到这一点吗?还是应该使用其他方法来实现动画以使其流畅?

My goal for now is to make the animation of a little circle, going along the path of a big circle. In the future, I would need to dynamically change the speed of revolution using a slider, but that is not a problem now. I can't figure out how to make this animation smooth? By this I mean that I want my little circle to not slow down when it reaches the end of animation. Is this possible using path transition? Or should I use some other method to implement animation in order for it to be smooth?

这是我现在拥有的:

import javafx.animation.*;
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.layout.BorderPane;
    import javafx.scene.layout.Pane;
    import javafx.scene.paint.Color;
    import javafx.scene.shape.*;
    import javafx.stage.Stage;
    import javafx.util.Duration;

    public class SpinningCircles extends Application {

        BorderPane root = new BorderPane();
        int BigCircRad = 200;
        int BigCircX = 750;
        int BigCircY = 400;
        double duration = 1;

        public static void main(String[] args) {
            launch();
        }
        @Override
        public void start(Stage stage){
            Circle cir1 = new Circle(BigCircRad);
            Circle cir2 = new Circle(BigCircRad/10);

            cir1.setFill(Color.WHITE);
            cir2.setFill(Color.WHITE);

            cir1.setStroke(Color.BLUE);
            cir2.setStroke(Color.BLUE);

            cir1.setStrokeWidth(4);
            cir2.setStrokeWidth(4);

            cir1.setCenterX(BigCircX);
            cir2.setCenterX(BigCircX);

            cir1.setCenterY(BigCircY);
            cir2.setCenterY(BigCircY-BigCircRad);

            PathTransition pt = new PathTransition();
            pt.setNode(cir2);
            pt.setDuration(Duration.seconds(duration));
            pt.setPath(cir1);
            pt.setDelay(Duration.seconds(1));
            pt.setCycleCount(Animation.INDEFINITE);
            pt.play();

            root.getChildren().addAll(cir1, cir2);
            Scene scene = new Scene(root, 1000, 500);
            stage.setScene(scene);
            stage.setTitle("Spinning circles");
            stage.setMaximized(true);
            stage.setFullScreen(true);
            stage.show();
        }
    }

推荐答案

您需要在文档中,是插补器

控制每个加速和减速的时间 过渡周期.

Controls the timing for acceleration and deceleration at each Transition cycle.

默认插值器设置为Interpolator.EASE_BOTH.

Default interpolator is set to Interpolator.EASE_BOTH.

默认值为 Interpolator.EASE_BOTH

The default, Interpolator.EASE_BOTH

将使动画开始缓慢,然后以平滑的方式加速并再次减速直至结束.

will make an animation start slow, then accelerate and slow down again towards the end, all in a smooth manner.

相反,请使用 LINEAR 内插器:

Instead, use a LINEAR interpolator:

    pt.setInterpolator(Interpolator.LINEAR);

请注意,必须在开始动画之前设置插值器:

Note the interpolator must be set before the animation is started:

    PathTransition pt = new PathTransition();
    pt.setNode(cir2);
    pt.setDuration(Duration.seconds(duration));
    pt.setPath(cir1);
    pt.setDelay(Duration.seconds(1));
    pt.setCycleCount(Animation.INDEFINITE);

    pt.setInterpolator(Interpolator.LINEAR);

    pt.play();

这篇关于如何使使用路径转换的javafx动画平滑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆