JavaFX SequentialTransition类

此转换用于按顺序(一个接一个)在节点上应用动画列表。顺序转换对于设计按顺序为其实体设置动画的游戏很重要。

在 JavaFX 中,类 javafx.animation.SequentialTransition 用于表示顺序转换。我们需要将多个过渡对象的列表传递给这个类的构造函数。这些动画将按顺序应用在节点上(按顺序,它们被传递到构造函数中)。

1 SequentialTransition类的属性

属性 描述 setter方法
node 它是类 Node 的对象类型属性。它表示要应用转换的节点。 setNode(Node node)

2 SequentialTransition类的构造函数

该类包含下面给出的四个构造函数。

  1. public SequentialTransition() :使用默认参数创建一个 SequentialTransition 实例。
  2. public SequentialTransition(Animation?children) :创建一个带有动画列表的 SequentialTransition 实例。
  3. public SequentialTransition(Node node) :使用将应用顺序转换的指定节点创建一个sequentialTransition 实例。
  4. public SequentialTransition(Node node, Animation?children) :使用指定的节点和动画列表创建一个 SequentialTransition 的实例。

3 SequentialTransition类的例子

在以下示例中,我们创建了一个多边形并按顺序对其应用了各种过渡。

package com.yiidian;

import javafx.animation.*;
import javafx.application.Application;
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 SequentialTransitionExample extends Application {
  
    @Override  
    public void start(Stage primaryStage) throws Exception {

         Polygon poly = new Polygon();
          
         //Setting points for the polyogn   
         poly.getPoints().addAll(new Double[] {320.0,270.0,270.0,220.0,270.0,270.0,320.0,120.0,370.0,270.0,370.0,220.0});  
          
         //Setting Color and Stroke properties for the polygon    
         poly.setFill(Color.LIMEGREEN);
         poly.setStroke(Color.BLACK);  
       
         //Setting durations for the transitions  
         Duration dur1 = Duration.millis(1000);
         Duration dur2 = Duration.millis(500);  
       
           
         //Setting the pause transition  
         PauseTransition pause = new PauseTransition(Duration.millis(1000));
           
         //Setting the fade transition   
         FadeTransition fade = new FadeTransition(dur2);
         fade.setFromValue(1.0f);  
         fade.setToValue(0.3f);  
         fade.setCycleCount(2);  
         fade.setAutoReverse(true);  
           
         //Setting Translate transition  
         TranslateTransition translate = new TranslateTransition(dur1);
         translate.setToX(-150f);  
         translate.setCycleCount(2);  
         translate.setAutoReverse(true);  
           
         //Setting Rotate Transition   
         RotateTransition rotate = new RotateTransition(dur2);
         rotate.setByAngle(180f);  
         rotate.setCycleCount(4);  
         rotate.setAutoReverse(true);  
           
         //Setting Scale Transition   
         ScaleTransition scale = new ScaleTransition(dur1);
         scale.setByX(1.5f);  
         scale.setByY(1.2f);  
         scale.setCycleCount(2);  
         scale.setAutoReverse(true);  
           
         //Instantiating Sequential Transition class by passing the list of transitions into its constructor  
         SequentialTransition seqT = new SequentialTransition (poly,rotate, pause, fade, translate,  scale);  
           
         //playing the transition   
         seqT.play();  
           
         //Configuring the group and scene   
         Group root = new Group();
         root.getChildren().addAll(poly);  
         Scene scene = new Scene(root,490,450,Color.WHEAT);
         primaryStage.setScene(scene);  
         primaryStage.setTitle("一点教程网:Sequential Transition Example");
         primaryStage.show();  
    }  
    public static void main(String[] args) {  
        launch(args);  
    }  
  
} 

输出结果为:

热门文章

优秀文章