个性化阅读
专注于IT技术分析

JavaFX淡入淡出过渡

本文概述

JavaFX淡入淡出过渡

它为节点的不透明度设置动画, 以使节点的填充颜色变暗。这可以通过在指定的持续时间内不断减小填充颜色的不透明度来达到目标​​不透明度值来完成。

在JavaFX中, 类javafx.animation.FadeTransition表示FadeTransition。我们需要实例化此类, 以创建适当的淡入淡出过渡。

物产

下表描述了类的属性及其设置方法。

属性 描述 设置方法
byValue 这是一个双精度类型的属性。它代表淡入淡出过渡的增加的停止不透明度值。 setByValue(double property)
Duration 这是Duration类的对象类型属性。它表示此淡入淡出过渡的持续时间。 setDuration(Duration duration)
fromValue 这是一个双精度类型的属性。它代表淡入淡出过渡的开始不透明度。 setFromValue(double value)
node 这是Node类的对象类型属性。这表示要在其上应用过渡的节点。 setNode(Node node)
toValue 这是一个双精度类型的属性。这表示淡入淡出过渡的不透明度的停止值。 setToValue(double value)

建设者

该类包含三个构造函数。

  1. public TranslateTransition():使用默认参数创建TranslateTransition的新实例。
  2. public TranslateTransition(Duration duration):使用指定的持续时间创建TranslateTransition的新实例。
  3. public TranslateTransition(Duration duration, Node node):使用指定的持续时间和节点创建Translate Transition的新实例。

在下面的示例中, 圆圈颜色的不透明度从10降低到0.1。

package application;
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 Fade_Transition extends Application{

    @Override
    public void start(Stage primaryStage) throws Exception {
        // TODO Auto-generated method stub
        //Creating the circle 
        Circle cir = new Circle(250, 120, 80);
        
        //setting color and stroke of the circle 
        cir.setFill(Color.RED);
        cir.setStroke(Color.BLACK);
        
        //Instantiating FadeTransition class 
        FadeTransition fade = new FadeTransition();
        
        
        //setting the duration for the Fade transition 
        fade.setDuration(Duration.millis(5000));
        
        //setting the initial and the target opacity value for the transition 
        fade.setFromValue(10);
        fade.setToValue(0.1);
        
        //setting cycle count for the Fade transition 
        fade.setCycleCount(1000);
        
        //the transition will set to be auto reversed by setting this to true 
        fade.setAutoReverse(true);
        
        //setting Circle as the node onto which the transition will be applied
        fade.setNode(cir);
        
        
        //playing the transition 
        fade.play();
        
        //Configuring Group and Scene 
        Group root = new Group();
        root.getChildren().addAll(cir);
        Scene scene = new Scene(root, 500, 250, Color.WHEAT);
        primaryStage.setScene(scene);
        primaryStage.setTitle("Translate Transition example");
        primaryStage.show();
        
    }
    public static void main(String[] args) {
        launch(args);
    }
}

输出:

JavaFX淡入淡出过渡
赞(0)
未经允许不得转载:srcmini » JavaFX淡入淡出过渡

相关推荐

评论 抢沙发

评论前必须登录!