开发手册 欢迎您!
软件开发者资料库

JavaFX - 动画

JavaFX动画 - 从简单和简单的步骤学习JavaFX,从基本到高级概念,包括概述,环境,架构,应用程序,2D形状,文本,效果,转换,动画,颜色,图像,3D形状,事件处理,UI控件,图表,布局窗格,CSS。

通常,动画对象意味着通过快速显示创建其运动的幻觉.在JavaFX中,可以通过随时间更改其属性来动画化节点. JavaFX提供了一个名为 javafx.animation 的包.此程序包包含用于为节点设置动画的类.动画是所有这些类的基类.

使用JavaFX,您可以应用动画(过渡),例如淡入淡出过渡填充过渡旋转过渡缩放过渡笔画过渡翻译过渡路径过渡 b>,顺序转换暂停转换并行转换

所有这些转换都是由包 javafx.animation 中的各个类表示.

要将特定动画应用于节点,您必须按照下面给出的步骤和减去;

  • 使用相应的类创建一个require节点.

  • 实例化要应用的相应转换(动画)类

  • 设置转换的属性和

  • 最后使用动画类的 play()方法播放过渡.

在这个ch我们将讨论基本过渡(旋转,缩放,翻译)的例子.

旋转过渡

以下是演示旋转过渡的程序在JavaFX中.将此代码保存在名为 RotateTransitionExample.java 的文件中.

import javafx.animation.RotateTransition; import javafx.application.Application; import static javafx.application.Application.launch; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.paint.Color; import javafx.scene.shape.Polygon; import javafx.stage.Stage; import javafx.util.Duration;          public class RotateTransitionExample extends Application {    @Override    public void start(Stage stage) {            //Creating a hexagon       Polygon hexagon = new Polygon();                    //Adding coordinates to the hexagon       hexagon.getPoints().addAll(new Double[]{                 200.0, 50.0,          400.0, 50.0,          450.0, 150.0,                   400.0, 250.0,          200.0, 250.0,                            150.0, 150.0,       });       //Setting the fill color for the hexagon       hexagon.setFill(Color.BLUE);              //Creating a rotate transition          RotateTransition rotateTransition = new RotateTransition();             //Setting the duration for the transition       rotateTransition.setDuration(Duration.millis(1000));             //Setting the node for the transition       rotateTransition.setNode(hexagon);                   //Setting the angle of the rotation       rotateTransition.setByAngle(360);             //Setting the cycle count for the transition       rotateTransition.setCycleCount(50);             //Setting auto reverse value to false       rotateTransition.setAutoReverse(false);             //Playing the animation       rotateTransition.play();                //Creating a Group object         Group root = new Group(hexagon);                //Creating a scene object       Scene scene = new Scene(root, 600, 300);               //Setting title to the Stage       stage.setTitle("Rotate transition example ");                //Adding scene to the stage       stage.setScene(scene);                //Displaying the contents of the stage       stage.show();    }         public static void main(String args[]){       launch(args);    } }

使用以下命令从命令提示符编译并执行保存的java文件.

 javac RotateTransitionExample.java  java RotateTransitionExample

执行时,上述程序生成一个JavaFX窗口,如下所示.

旋转过渡

缩放过渡

以下是在JavaFX中演示Scale Transition的程序.将此代码保存在名为 ScaleTransitionExample.java 的文件中.

import javafx.animation.ScaleTransition; import javafx.application.Application; import static javafx.application.Application.launch; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.paint.Color; import javafx.scene.shape.Circle; import javafx.stage.Stage; import javafx.util.Duration;          public class ScaleTransitionExample extends Application {     @Override    public void start(Stage stage) {            //Drawing a Circle       Circle circle = new Circle();             //Setting the position of the circle       circle.setCenterX(300.0f);       circle.setCenterY(135.0f);             //Setting the radius of the circle       circle.setRadius(50.0f);             //Setting the color of the circle       circle.setFill(Color.BROWN);             //Setting the stroke width of the circle       circle.setStrokeWidth(20);              //Creating scale Transition       ScaleTransition scaleTransition = new ScaleTransition();             //Setting the duration for the transition       scaleTransition.setDuration(Duration.millis(1000));             //Setting the node for the transition       scaleTransition.setNode(circle);             //Setting the dimensions for scaling       scaleTransition.setByY(1.5);       scaleTransition.setByX(1.5);             //Setting the cycle count for the translation       scaleTransition.setCycleCount(50);             //Setting auto reverse value to true       scaleTransition.setAutoReverse(false);             //Playing the animation       scaleTransition.play();                //Creating a Group object        Group root = new Group(circle);                //Creating a scene object        Scene scene = new Scene(root, 600, 300);             //Setting title to the Stage       stage.setTitle("Scale transition example");                //Adding scene to the stage       stage.setScene(scene);                //Displaying the contents of the stage       stage.show();    }         public static void main(String args[]){       launch(args);    } }

使用以下命令从命令提示符编译并执行保存的java文件.

 javac ScaleTransitionExample.java  java ScaleTransitionExample

执行时,上述程序生成一个JavaFX窗口,如下所示.

Scale Transition

翻译过渡

以下是演示JavaFX中的Translate Transition的程序.将此代码保存在名为 TranslateTransitionExample.java 的文件中.

import javafx.animation.TranslateTransition; import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.paint.Color; import javafx.scene.shape.Circle; import javafx.stage.Stage; import javafx.util.Duration;          public class TranslateTransitionExample extends Application {    @Override    public void start(Stage stage) {        //Drawing a Circle       Circle circle = new Circle();             //Setting the position of the circle       circle.setCenterX(150.0f);       circle.setCenterY(135.0f);             //Setting the radius of the circle       circle.setRadius(100.0f);             //Setting the color of the circle       circle.setFill(Color.BROWN);             //Setting the stroke width of the circle       circle.setStrokeWidth(20);              //Creating Translate Transition       TranslateTransition translateTransition = new TranslateTransition();             //Setting the duration of the transition        translateTransition.setDuration(Duration.millis(1000));             //Setting the node for the transition       translateTransition.setNode(circle);             //Setting the value of the transition along the x axis.       translateTransition.setByX(300);             //Setting the cycle count for the transition       translateTransition.setCycleCount(50);             //Setting auto reverse value to false       translateTransition.setAutoReverse(false);             //Playing the animation       translateTransition.play();                //Creating a Group object        Group root = new Group(circle);                //Creating a scene object       Scene scene = new Scene(root, 600, 300);              //Setting title to the Stage       stage.setTitle("Translate transition example");                //Adding scene to the stage       stage.setScene(scene);                //Displaying the contents of the stage       stage.show();    }         public static void main(String args[]){       launch(args);    } }

使用以下命令从命令提示符编译并执行保存的java文件.

 javac TranslateTransitionExample.java  java TranslateTransitionExample

执行时,上述程序生成一个JavaFX窗口,如下所示.

翻译过渡

除了这些,JavaFX提供了在节点上应用更多转换的类.以下是JavaFX支持的其他类型的转换.

  • 影响节点属性的转换淡化,填充,描边

  • 涉及多个基本过渡的过渡顺序,并行,暂停

  • 转换对象的转换指定的路径路径转换