Javafx 简明教程
JavaFX - Pause Transition
暂停转换是一种由 JavaFX 提供的转换,它会在继续按顺序执行其他转换前,暂停动画一段时间。
可以通过两种方式对 JavaFX 节点应用多个转换:顺序地或并行地。当顺序应用时,你还可以暂停动画一段时间,然后再应用另一个转换。这有助于在需要以更清晰的方式查看所应用转换的情况下。
让我们在本章中进一步了解这种转换。
Pause Transition
PauseTransition 类属于 javafx.animation 包,并在按顺序对 JavaFX 节点应用多个转换时使用。此类在每次转换后都会暂停动画指定持续时间。应用于该节点的所有转换都是此类及其节点属性的子转换(前提是未指定它们自己的节点属性)。
Example
以下是演示 JavaFX 中暂停转换的程序。将此代码保存在名为 PauseTransitionExample.java 的文件中。
import javafx.animation.PauseTransition;
import javafx.animation.ScaleTransition;
import javafx.animation.SequentialTransition;
import javafx.animation.TranslateTransition;
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 PauseTransitionExample 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(50.0f);
//Setting the color of the circle
circle.setFill(Color.BROWN);
//Setting the stroke width of the circle
circle.setStrokeWidth(20);
//Creating a Pause Transition
PauseTransition pauseTransition = new PauseTransition();
//Setting the duration for the transition
pauseTransition.setDuration(Duration.millis(1000));
//Creating Translate Transition
TranslateTransition translateTransition = new TranslateTransition();
//Setting the duration for the transition
translateTransition.setDuration(Duration.millis(1000));
//Setting the node of the transition
translateTransition.setNode(circle);
//Setting the value of the transition along the x axis
translateTransition.setByX(300);
//Setting the cycle count for the stroke
translateTransition.setCycleCount(5);
//Setting auto reverse value to true
translateTransition.setAutoReverse(false);
//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(5);
//Setting auto reverse value to true
scaleTransition.setAutoReverse(false);
//Applying Sequential transition to the circle
SequentialTransition sequentialTransition = new SequentialTransition(
circle, translateTransition, pauseTransition, scaleTransition );
//Playing the animation
sequentialTransition.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("Pause 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 --module-path %PATH_TO_FX% --add-modules javafx.controls PauseTransitionExample.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls PauseTransitionExample
执行后,上述程序会生成如下所示的 JavaFX 窗口。