Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaFX的淡入淡出效果_Javafx_Transition_Opacity_Fade_Timeline - Fatal编程技术网

JavaFX的淡入淡出效果

JavaFX的淡入淡出效果,javafx,transition,opacity,fade,timeline,Javafx,Transition,Opacity,Fade,Timeline,我想实现一种特殊的时尚转换效果。但我不知道我该怎么办。对于某些节点,我希望从左到右增加不透明度(例如在Powerpoint中,您可以使用这种效果更改幻灯片)。下面是一个简单的矩形示例。但是第二个应该从左到右淡入淡出(不透明度应该在左侧比右侧更早增加)。对于时间线和关键值/关键帧,我也没有找到解决方案。 提前谢谢 矩形rec2 @Override public void start(Stage stage) { Group root = new Group(); Scene sce

我想实现一种特殊的时尚转换效果。但我不知道我该怎么办。对于某些节点,我希望从左到右增加不透明度(例如在Powerpoint中,您可以使用这种效果更改幻灯片)。下面是一个简单的矩形示例。但是第二个应该从左到右淡入淡出(不透明度应该在左侧比右侧更早增加)。对于时间线和关键值/关键帧,我也没有找到解决方案。 提前谢谢

矩形rec2

@Override
public void start(Stage stage) {
    Group root = new Group();
    Scene scene = new Scene(root, 400, 300, Color.BLACK);
    stage.setTitle("JavaFX Scene Graph Demo");       

    Pane pane = new Pane();
    Button btnForward = new Button();
    btnForward.setText(">");
    btnForward.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {
            FadeTransition ft = new FadeTransition(Duration.millis(2000), rec2);
            ft.setFromValue(0.);
            ft.setToValue(1.);
            ft.play();
        }
    });        
    Rectangle rec1 = new Rectangle(0, 0, 300,200);        
    rec1.setFill(Color.RED);
    rec2 = new Rectangle(100, 50, 100,100);        
    rec2.setFill(Color.GREEN);
    rec2.setOpacity(0.);
    pane.getChildren().addAll(rec1,rec2);
    root.getChildren().add(pane);
    root.getChildren().add(btnForward);
    stage.setScene(scene);
    stage.show();
}
@覆盖
公众假期开始(阶段){
组根=新组();
场景=新场景(根,400,300,颜色.黑色);
setTitle(“JavaFX场景图演示”);
窗格=新窗格();
按钮btnForward=新按钮();
btnForward.setText(“>”);
setOnAction(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent事件){
FADETRANSION ft=新的FADETRANSION(持续时间.毫秒(2000),记录2);
ft.setFromValue(0.);
ft.setToValue(1.);
ft.play();
}
});        
矩形rec1=新矩形(0,0,300200);
rec1.设置填充(颜色为红色);
rec2=新矩形(100,50,100100);
rec2.设置填充(颜色为绿色);
建议2.设置不透明度(0);
pane.getChildren().addAll(rec1,rec2);
root.getChildren().add(窗格);
root.getChildren().add(btnForward);
舞台场景;
stage.show();
}

使用css和线性渐变定义矩形的填充,该渐变引用矩形左右边缘的查找颜色。(这可以是内联的,也可以是外部样式表中的。)

定义两个表示左右边缘不透明度的
DoubleProperty
s

使用绑定到两个double属性的内联样式定义矩形或其父对象之一上的查找颜色

import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.Duration;

public class FadeInRectangle extends Application {

    @Override
    public void start(Stage primaryStage) {
        Group root = new Group();
        Scene scene = new Scene(root, 400, 300, Color.BLACK);
        primaryStage.setTitle("JavaFX Scene Graph Demo");       

        Pane pane = new Pane();
        Rectangle rec1 = new Rectangle(0, 0, 300,200);        
        rec1.setFill(Color.RED);
        Rectangle rec2 = new Rectangle(100, 50, 100,100);

        rec2.setStyle("-fx-fill: linear-gradient(to right, left-col, right-col);");

        final DoubleProperty leftEdgeOpacity = new SimpleDoubleProperty(0);
        final DoubleProperty rightEdgeOpacity = new SimpleDoubleProperty(0);

        root.styleProperty().bind(
            Bindings.format("left-col: rgba(0,128,0,%f); right-col: rgba(0,128,0,%f);", leftEdgeOpacity, rightEdgeOpacity)   
        );

        Button btnForward = new Button();
        btnForward.setText(">");
        btnForward.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                Timeline timeline = new Timeline(
                    new KeyFrame(Duration.ZERO, new KeyValue(leftEdgeOpacity, 0)),
                    new KeyFrame(Duration.ZERO, new KeyValue(rightEdgeOpacity, 0)),
                    new KeyFrame(Duration.millis(500), new KeyValue(rightEdgeOpacity, 0)),
                    new KeyFrame(Duration.millis(1500), new KeyValue(leftEdgeOpacity, 1)),
                    new KeyFrame(Duration.millis(2000), new KeyValue(rightEdgeOpacity, 1)),
                    new KeyFrame(Duration.millis(2000), new KeyValue(leftEdgeOpacity, 1))
                );
                timeline.play();
            }
        });        
        pane.getChildren().addAll(rec1,rec2);
        root.getChildren().add(pane);
        root.getChildren().add(btnForward);
        primaryStage.setScene(scene);
        primaryStage.show();

    }

    public static void main(String[] args) {
        launch(args);
    }
}
使用时间线更改不透明度属性的值

import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.Duration;

public class FadeInRectangle extends Application {

    @Override
    public void start(Stage primaryStage) {
        Group root = new Group();
        Scene scene = new Scene(root, 400, 300, Color.BLACK);
        primaryStage.setTitle("JavaFX Scene Graph Demo");       

        Pane pane = new Pane();
        Rectangle rec1 = new Rectangle(0, 0, 300,200);        
        rec1.setFill(Color.RED);
        Rectangle rec2 = new Rectangle(100, 50, 100,100);

        rec2.setStyle("-fx-fill: linear-gradient(to right, left-col, right-col);");

        final DoubleProperty leftEdgeOpacity = new SimpleDoubleProperty(0);
        final DoubleProperty rightEdgeOpacity = new SimpleDoubleProperty(0);

        root.styleProperty().bind(
            Bindings.format("left-col: rgba(0,128,0,%f); right-col: rgba(0,128,0,%f);", leftEdgeOpacity, rightEdgeOpacity)   
        );

        Button btnForward = new Button();
        btnForward.setText(">");
        btnForward.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                Timeline timeline = new Timeline(
                    new KeyFrame(Duration.ZERO, new KeyValue(leftEdgeOpacity, 0)),
                    new KeyFrame(Duration.ZERO, new KeyValue(rightEdgeOpacity, 0)),
                    new KeyFrame(Duration.millis(500), new KeyValue(rightEdgeOpacity, 0)),
                    new KeyFrame(Duration.millis(1500), new KeyValue(leftEdgeOpacity, 1)),
                    new KeyFrame(Duration.millis(2000), new KeyValue(rightEdgeOpacity, 1)),
                    new KeyFrame(Duration.millis(2000), new KeyValue(leftEdgeOpacity, 1))
                );
                timeline.play();
            }
        });        
        pane.getChildren().addAll(rec1,rec2);
        root.getChildren().add(pane);
        root.getChildren().add(btnForward);
        primaryStage.setScene(scene);
        primaryStage.show();

    }

    public static void main(String[] args) {
        launch(args);
    }
}
导入javafx.animation.KeyFrame;
导入javafx.animation.KeyValue;
导入javafx.animation.Timeline;
导入javafx.application.application;
导入javafx.beans.binding.Bindings;
导入javafx.beans.property.DoubleProperty;
导入javafx.beans.property.SimpleDoubleProperty;
导入javafx.event.ActionEvent;
导入javafx.event.EventHandler;
导入javafx.scene.Group;
导入javafx.scene.scene;
导入javafx.scene.control.Button;
导入javafx.scene.layout.Pane;
导入javafx.scene.paint.Color;
导入javafx.scene.shape.Rectangle;
导入javafx.stage.stage;
导入javafx.util.Duration;
公共类FadeInRectangle扩展了应用程序{
@凌驾
公共无效开始(阶段primaryStage){
组根=新组();
场景=新场景(根,400,300,颜色.黑色);
setTitle(“JavaFX场景图演示”);
窗格=新窗格();
矩形rec1=新矩形(0,0,300200);
rec1.设置填充(颜色为红色);
矩形rec2=新矩形(100,50,100100);
rec2.setStyle(“-fx填充:线性渐变(向右、向左列、向右列);”;
最终DoubleProperty LeftedgePocity=新的SimpleDoubleProperty(0);
最终DoubleProperty rightEdgeOpacity=新的SimpleDoubleProperty(0);
root.styleProperty().bind(
格式(“左列:rgba(0128,0,%f);右列:rgba(0128,0,%f);”,leftEdgeOpacity,righedgeopacity)
);
按钮btnForward=新按钮();
btnForward.setText(“>”);
setOnAction(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent事件){
时间线=新时间线(
新关键帧(Duration.ZERO,新关键值(LeftedgePocity,0)),
新关键帧(Duration.ZERO,新关键帧值(rightEdgeOpacity,0)),
新关键帧(持续时间.millis(500),新关键值(rightEdgeOpacity,0)),
新关键帧(Duration.millis(1500),新关键值(LeftedgePocity,1)),
新关键帧(Duration.millis(2000),新关键值(rightEdgeOpacity,1)),
新关键帧(Duration.millis(2000),新关键值(leftEdgeOpacity,1))
);
timeline.play();
}
});        
pane.getChildren().addAll(rec1,rec2);
root.getChildren().add(窗格);
root.getChildren().add(btnForward);
初级阶段。场景(场景);
primaryStage.show();
}
公共静态void main(字符串[]args){
发射(args);
}
}