JavaFX - 暂停转换



暂停转换是 JavaFX 提供的一种转换类型,它会在暂停动画一段时间后,以顺序方式恢复另一个转换。

可以通过两种方式将多个转换应用于 JavaFX 节点:顺序和并行。顺序应用时,您还可以暂停动画一段时间,然后再应用另一个转换。这有助于在您需要更清晰地查看应用的转换的场景中。

让我们在本节中进一步了解这种转换。

暂停转换

PauseTransition 类属于 javafx.animation 包,在以顺序方式将多个转换应用于 JavaFX 节点时使用。此类会在每个转换后暂停动画一段时间。应用于节点的所有转换都是此类的子转换,它们继承其 node 属性(仅当未指定其自身的 node 属性时)。

示例

以下是演示 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 窗口。

Pause Transition
广告