JavaFX 矩形宽度动画 [英] JavaFX rectangle width animation

查看:47
本文介绍了JavaFX 矩形宽度动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是一个问题...是否可以将矩形宽度的变化显示为动画?矩形本身是一个状态栏,所以在 x 方向上的缩放不能完成这项工作.

谢谢

解决方案

一种方法是在

<小时>

SSCCE:

public class RectangleWidthAnimation extends Application{@覆盖public void start(Stage primaryStage) 抛出异常 {矩形 statusBar = new Rectangle(300, 100);Button animationButton = new Button("动画宽度减少25");animationButton.setOnAction(event -> {System.out.println("动画开始:width = " + statusBar.getWidth());KeyValue widthValue = new KeyValue(statusBar.widthProperty(), statusBar.getWidth() - 25);KeyFrame frame = new KeyFrame(Duration.seconds(2), widthValue);时间线时间线 = 新时间线(帧);时间线.播放();timeline.setOnFinished(finishedEvent -> System.out.println("动画结束:width = " + statusBar.getWidth()));});VBox 容器 = new VBox(10, statusBar, animationButton);//使用这些对齐进行实验container.setAlignment(Pos.CENTER);//container.setAlignment(Pos.CENTER_LEFT);//container.setAlignment(Pos.CENTER_RIGHT);primaryStage.setScene(new Scene(container, 350, 150));primaryStage.show();}}

<小时>上述的替代方法可能是使用转换,您可以阅读关于 这里

just a question... is it possible to show the change of the width of a rectangle as an animation? The rectangle itself is a statusbar so a scale in both x directions does not do the job.

thx

解决方案

One method would be to animate the width modification within a Timeline as opposed to scaling in both directions. You can simulate the direction by modifying the alignment of the Rectangle's parent

Alignment values:

  • Pos.CENTER : Appears as though the change is occurring in both directions
  • Pos.CENTER_LEFT: Appears as though the change is occurring from the right
  • Pos.CENTER_RIGHT: Appears as though the change is occurring from the left

By experimenting with alignments you can choose the one that best fits your current status bar


Visual representation:

Order of alignment: Right | Center | Left


SSCCE:

public class RectangleWidthAnimation extends Application{
    @Override
    public void start(Stage primaryStage) throws Exception {
        Rectangle statusBar = new Rectangle(300, 100);
        Button animationButton = new Button("Animate width decrease by 25");
        animationButton.setOnAction(event -> {
            System.out.println("Animation start: width = " + statusBar.getWidth());
            KeyValue widthValue = new KeyValue(statusBar.widthProperty(), statusBar.getWidth() - 25);
            KeyFrame frame = new KeyFrame(Duration.seconds(2), widthValue);
            Timeline timeline = new Timeline(frame);
            timeline.play();
            timeline.setOnFinished(finishedEvent -> System.out.println("Animation end: width = " + statusBar.getWidth()));
        });

        VBox container = new VBox(10, statusBar, animationButton);

        //Experiment with these alignments
        container.setAlignment(Pos.CENTER);
        //container.setAlignment(Pos.CENTER_LEFT);
        //container.setAlignment(Pos.CENTER_RIGHT);

        primaryStage.setScene(new Scene(container, 350, 150));
        primaryStage.show();
    }
}


An alternative to the above could be to use transitions, which you can read about here

这篇关于JavaFX 矩形宽度动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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