JavaFX - 淡入淡出过渡



淡入淡出过渡是一种几何过渡,它改变对象的透明度属性。使用淡入淡出过渡,您可以降低或提高对象的透明度。这种过渡被称为几何过渡,因为它涉及对象的几何形状。

在 JavaFX 中,淡入淡出过渡用于在指定持续时间内将节点的透明度从起始值过渡到结束值。这可以通过使用属于 **javafx.animation** 包的 **FadeTransition** 类来完成。

JavaFX 中的淡入淡出过渡

您可以使用 FadeTransition 类的属性将淡入淡出过渡应用于 JavaFX 对象。您必须使用 *fromValue* 和 *toValue* 设置过渡的起始值和结束值。当未指定 fromValue 时,默认情况下会考虑节点的当前透明度值;当未指定 toValue 时,byValue 会添加到起始值。

**请注意过渡属性** - fromValue、toValue 和 byValue,在过渡运行期间不能更改。但是,如果您尝试在过渡运行期间更改属性,则该更改将被忽略。因此,必须停止并重新启动动画才能将新的属性值分配给过渡。也不会抛出任何异常。

FadeTransition 类包含以下属性:

  • **byValue** - 指定此 FadeTransition 从开始处的增量停止透明度值。

  • **duration** - 此 FadeTransition 的持续时间。

  • **fromValue** - 指定此 FadeTransition 的起始透明度值。

  • **node** - 此过渡的目标节点。

  • **toValue** - 指定此 FadeTransition 的停止透明度值。

示例

以下程序演示了 JavaFX 中的淡入淡出过渡。将此代码保存在名为 **FadeTransitionExample.java** 的文件中。

import javafx.animation.FadeTransition; 
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 FadeTransitionExample 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(100.0f); 
      
      //Setting the color of the circle 
      circle.setFill(Color.BROWN); 
      
      //Setting the stroke width of the circle 
      circle.setStrokeWidth(20); 
       
      //Creating the fade Transition 
      FadeTransition fadeTransition = new FadeTransition(Duration.millis(1000)); 
      
      //Setting the node for Transition 
      fadeTransition.setNode(circle); 
      
      //Setting the property fromValue of the transition (opacity) 
      fadeTransition.setFromValue(1.0); 
      
      //Setting the property toValue of the transition (opacity) 
      fadeTransition.setToValue(0.3); 
      
      //Setting the cycle count for the transition 
      fadeTransition.setCycleCount(50); 
      
      //Setting auto reverse value to false 
      fadeTransition.setAutoReverse(false); 
  
      //Playing the animation 
      fadeTransition.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("Fade 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 FadeTransitionExample.java 
java --module-path %PATH_TO_FX% --add-modules javafx.controls FadeTransitionExample

输出

执行后,上述程序将生成一个如下图所示的 JavaFX 窗口。

Fade Transition
广告

© . All rights reserved.