onFinished多次发射

时间:2015-01-16 13:28:27

标签: java animation event-handling javafx-2

我在JavaFX 2.2中有一个简单的过渡。

    Duration onDuration = Duration.seconds(3);
    Duration offDuration = Duration.seconds(3);
    Duration transitionDuration = Duration.seconds(1);

    long start = System.nanoTime();

    SequentialTransition sequentialTransition = new SequentialTransition();

    FillTransition offOnTransition = new FillTransition(transitionDuration, rectangle, Color.RED, Color.BLUE);
    offOnTransition.setDelay(offDuration);
    offOnTransition.setOnFinished(event -> {
        System.out.println("off " + java.time.Duration.ofNanos(System.nanoTime() - start));
    });
    FillTransition onOffTransition = new FillTransition(transitionDuration, rectangle, Color.BLUE, Color.RED);
    onOffTransition.setDelay(onDuration);
    onOffTransition.setOnFinished(event -> {
        System.out.println("on " + java.time.Duration.ofNanos(System.nanoTime() - start));
    });
    sequentialTransition.getChildren().addAll(offOnTransition, onOffTransition);

    sequentialTransition.setCycleCount(Animation.INDEFINITE);
    sequentialTransition.play();

动画效果很好,但与off / on过渡相关的onFinished事件在每个周期中触发多次(第一个除外)。

off PT4.030136579S  
on PT8.029348018S  
off PT11.021809501S  
off PT11.023261246S  
off PT11.037672786S  
off PT11.053672008S  
off PT11.069759654S  
off PT11.085694536S  
off PT11.101688478S  
off PT11.11768704S  
off PT11.133684612S  
off PT11.149685153S  
off PT11.16570879S  
off PT11.181750244S  
off PT11.197694035S  
off PT11.213704474S  
off PT11.22973174S  
off PT11.245711825S  
off PT11.261728533S  
off PT11.27770004S  
off PT11.29370454S  
off PT11.30971201S  
off PT11.325711891S  
off PT11.341714742S  
off PT11.357717922S  
off PT11.373732321S  
off PT11.389718345S  
off PT11.405718226S  
off PT11.421730315S  
off PT11.437758571S  
off PT11.453762412S  
off PT11.469738867S  
off PT11.485735119S  
off PT11.501750178S  
off PT11.51774445S  
off PT11.53375027S  
off PT11.549748831S  
off PT11.565745083S  
off PT11.581747604S  
off PT11.59777487S  
off PT11.613758585S  
off PT11.629746918S  
off PT11.645779463S  
off PT11.661767137S  
off PT11.677768668S  
off PT11.693779767S  
off PT11.709779648S  
off PT11.725786458S  
off PT11.741792278S  
off PT11.75778655S  
off PT11.773784782S  
off PT11.789783343S  
off PT11.805769037S  
off PT11.821770568S  
off PT11.837772429S  
off PT11.853796066S  
off PT11.86983752S  
off PT11.885818594S  
off PT11.901811217S  
off PT11.917850031S  
off PT11.933827146S  
off PT11.949833297S  
off PT11.965829548S  
off PT11.981802044S  
off PT11.997842839S  
off PT12.013836781S  
off PT12.029932675S  
on PT16.029359566S  

当我将转换的顺序切换为:

sequentialTransition.getChildren().addAll(onOffTransition, offOnTransition);

这是触发多个事件的开/关转换。我该如何解决这个问题?

0 个答案:

没有答案