Javafx 在一侧缩短一条线

Javafx Shortening a line on 1 side

我试图在 javafx 上制作一条边缩短一行的动画。我希望起点留在原处,终点更接近起点。我找不到合适的过渡。所以我用了这段代码:

        PathTransition pt = new PathTransition(Duration.millis(1000), new Line(x1*3/4, y, x1, y), line);
        pt.play();
        ScaleTransition stBig = new ScaleTransition();
        stBig.setNode(line);
        stBig.setFromX(2);
        stBig.setToX(0.25);
        stBig.setDuration(new Duration(1000));
        stBig.play();

但是没有用。我可以在一种类型的过渡中做到这一点吗?或者有什么出路吗?

因为这条线是水平的,你只需要改变endX 属性,你可以用一个简单的Timeline:

import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Line;
import javafx.stage.Stage;
import javafx.util.Duration;

public class LineAnimation extends Application {

    @Override
    public void start(Stage primaryStage) {
        Line line = new Line(100, 200, 300, 200);

        Timeline animation = new Timeline(
            new KeyFrame(Duration.seconds(1), new KeyValue(line.endXProperty(), 100))    
        );
        animation.setCycleCount(Animation.INDEFINITE);
        animation.play();

        Pane root = new Pane(line);
        Scene scene = new Scene(root, 400, 400);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

即使您需要为 endXendY 属性设置动画,也不会复杂多少:

    Line line = new Line(100, 100, 300, 300);

    Timeline animation = new Timeline(
        new KeyFrame(Duration.seconds(1), 
                new KeyValue(line.endXProperty(), 100),
                new KeyValue(line.endYProperty(), 100))    
    );