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.