gpt4 book ai didi

java - javafx 中 SequentialTransition 内的反向转换

转载 作者:行者123 更新时间:2023-11-30 02:04:56 26 4
gpt4 key购买 nike

给定一个我想反向播放的转换,我使用代码

t.jumpTo(t.getTotalDuration());
t.setRate(-1);

这意味着我跳到过渡的末尾并向后播放。我的问题是,如果我将此转换作为顺序或并行转换的一部分播放,则会将速率更改为其绝对值,然后向前播放。有什么方法可以“真正”反转转换,即获得一个新的转换,但速率=1,因此在顺序和并行转换中它仍然是反向的?

最佳答案

对于转换来说,这很简单:只需使用一个 Interpolator 返回 1-fraction 处的值即可:

public class ReverseInterpolator extends Interpolator {

private final Interpolator reverse;

public ReverseInterpolator(Interpolator reverse) {
if (reverse == null) {
throw new IllegalArgumentException();
}
this.reverse = reverse;
}

@Override
protected double curve(double t) {
return reverse.interpolate(0d, 1d, 1 - t);
}


public static Interpolator reverse(Interpolator interpolator) {
return (interpolator instanceof ReverseInterpolator)
? ((ReverseInterpolator) interpolator).reverse
: new ReverseInterpolator(interpolator);
}
}
@Override
public void start(Stage primaryStage) throws Exception {
Rectangle rect1 = new Rectangle(0, 0, 100, 100);
rect1.setFill(Color.RED);

Rectangle rect2 = new Rectangle(0, 200, 100, 100);
rect2.setFill(Color.BLUE);

Pane root = new Pane(rect1, rect2);

TranslateTransition transition1 = new TranslateTransition(Duration.seconds(2), rect1);
transition1.setFromX(0);
transition1.setToX(400);

TranslateTransition transition2 = new TranslateTransition(Duration.seconds(2), rect2);
transition2.setFromX(0);
transition2.setToX(400);

ParallelTransition transition = new ParallelTransition(transition1, transition2);
transition.setOnFinished(evt -> {
transition2.setInterpolator(ReverseInterpolator.reverse(transition2.getInterpolator()));
transition.play();
});
transition.play();

primaryStage.setScene(new Scene(root, 500, 500));
primaryStage.show();
}

对于一般的动画来说,这有点棘手。您可以编写一个 Transition 来设置每一帧的动画时间:

public class AnimationTransition extends Transition {
private final Animation animation;

public AnimationTransition(Animation animation) {
this.animation = animation;
setInterpolator(Interpolator.LINEAR);
setCycleDuration(animation.getCycleDuration());
setCycleCount(animation.getCycleCount());
statusProperty().addListener((o, oldValue, newValue) -> {
switch(newValue) {
case PAUSED:
animation.pause();
break;
case STOPPED:
animation.stop();
}
});

// don't actually procede except for playfrom calls
animation.setRate(0);
}

@Override
protected void interpolate(double frac) {
animation.playFrom(animation.getCycleDuration().multiply(frac));
}

}
@Override
public void start(Stage primaryStage) throws Exception {
Rectangle rect1 = new Rectangle(0, 0, 100, 100);
rect1.setFill(Color.RED);

Rectangle rect2 = new Rectangle(0, 200, 100, 100);
rect2.setFill(Color.BLUE);

Pane root = new Pane(rect1, rect2);

TranslateTransition transition1 = new TranslateTransition(Duration.seconds(2), rect1);
transition1.setFromX(0);
transition1.setToX(400);


Timeline timeline = new Timeline(
new KeyFrame(Duration.ZERO, new KeyValue(rect2.translateXProperty(), 0d)),
new KeyFrame(Duration.seconds(2), new KeyValue(rect2.translateXProperty(), 400d))
);
AnimationTransition transition2 = new AnimationTransition(timeline);

ParallelTransition transition = new ParallelTransition(transition1, transition2);
transition.setOnFinished(evt -> {
transition2.setInterpolator(ReverseInterpolator.reverse(transition2.getInterpolator()));
transition.play();
});
transition.play();

primaryStage.setScene(new Scene(root, 500, 500));
primaryStage.show();
}

关于java - javafx 中 SequentialTransition 内的反向转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51648716/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com