【问题标题】:How to animate dashed line Javafx如何动画虚线Javafx
【发布时间】:2016-04-19 19:32:31
【问题描述】:

我想在这一行中移动(动画)破折号:

  Line l=new Line();
  l.getStrokeDashArray().addAll(25d, 20d, 5d, 20d);

【问题讨论】:

    标签: java animation line javafx-8


    【解决方案1】:

    动画线的stokeDashOffsetProperty 从值 0 到笔划虚线数组中项目的总长度。如果您想在相反方向制作动画,只需反向运行 timeline

    import javafx.animation.*;
    import javafx.application.Application;
    import javafx.scene.*;
    import javafx.scene.shape.Line;
    import javafx.stage.Stage;
    import javafx.util.Duration;
    
    public class LineStrokeAnimator extends Application {
        @Override public void start(Stage stage) {
            Line line = new Line(20, 100, 80, 20);
            line.getStrokeDashArray().setAll(25d, 20d, 5d, 20d);
            line.setStrokeWidth(2);
    
            final double maxOffset = 
                    line.getStrokeDashArray().stream()
                            .reduce(
                                    0d, 
                                    (a, b) -> a + b
                            );
    
            Timeline timeline = new Timeline(
                    new KeyFrame(
                            Duration.ZERO, 
                            new KeyValue(
                                    line.strokeDashOffsetProperty(), 
                                    0, 
                                    Interpolator.LINEAR
                            )
                    ),
                    new KeyFrame(
                            Duration.seconds(2), 
                            new KeyValue(
                                    line.strokeDashOffsetProperty(), 
                                    maxOffset, 
                                    Interpolator.LINEAR
                            )
                    )
            );
            timeline.setCycleCount(Timeline.INDEFINITE);
            timeline.play();
    
            stage.setScene(new Scene(new Group(line), 100, 120));
            stage.show();
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-11-04
      • 2011-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-15
      • 2020-05-21
      • 2012-04-04
      相关资源
      最近更新 更多