JavaFX PauseTransition类

顾名思义,此过渡用于在按顺序应用于节点的多个动画之间暂停。在此转换的生命周期内,节点在屏幕上保持不可移动。

在 JavaFX 中,类javafx.animation.PauseTransition表示这种转换。我们只需要实例化这个类来创建一个合适的暂停过渡。

1 PauseTransition类的属性

属性 描述 setter方法
duration 它是 Duration 类的对象类型。它代表过渡的生命周期。 setDuration(Duration duration)

2 PauseTransition类的构造函数

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

  1. public PauseTransition() :使用默认参数创建PauseTransition的新实例。
  2. public PauseTransition(Duration duration) :创建具有指定持续时间的 PauseTransition 的新实例。

3 PauseTransition类的例子

在以下示例中,我们在多边形上应用了多个动画。我们在每次转换后给节点暂停了 1 秒。

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 Pause_Transition extends Application {
      @Override
      public void start(Stage primaryStage) throws Exception {
              //Creating polygon
              Polygon poly = new Polygon();
              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 Rotate Transition
               RotateTransition rt = new RotateTransition(Duration.millis(500),poly);
               rt.setByAngle(180);
               rt.setCycleCount(2);
               rt.setAutoReverse(true);

               //Setting Translate Transition
               TranslateTransition translate = new TranslateTransition(Duration.millis(500),poly);
               translate.setToX(-150f);
               translate.setCycleCount(2);  
               translate.setAutoReverse(true);

               //Setting Fade Transition
               FadeTransition fade = new FadeTransition(Duration.millis(500),poly);
               fade.setFromValue(1.0f);
               fade.setToValue(0.5f);
               fade.setCycleCount(2);
               fade.setAutoReverse(true);

               //Setting Sequential Transition and pause after each transition passed in the list
               SequentialTransition seqTransition = new SequentialTransition (fade,new PauseTransition(Duration.millis(2000)),rt,
               new PauseTransition(Duration.millis(2000)),translate);

               //Playing Sequential Transition
               seqTransition.play();

               //Setting Group and scene
               Group root = new Group();
               root.getChildren().addAll(poly);
               Scene scene = new Scene(root,490,300,Color.WHEAT);
               primaryStage.setScene(scene);
               primaryStage.setTitle("一点教程网:Pause Transition Example");
               primaryStage.show();
      }
      public static void main(String[] args) {
          launch(args);
      }
}  

输出结果为:

热门文章

优秀文章