简体   繁体   English

javafx定制ui组件FadeTransition无法正常工作

[英]javafx custom ui component FadeTransition doesn't working correctly

Fade transition doesn't working correctly 淡入淡出过渡无法正常工作

I created new javafx ui component and added FadeTransition, but unfortunately fade transition doesn't working. 我创建了新的javafx ui组件并添加了FadeTransition,但是不幸的是淡入淡出过渡不起作用。 When I was entered JFXButton background color changed but fade transition doesn't working. 当我输入JFXButton时,背景颜色已更改,但淡入过渡不起作用。 How can I fixed it ? 我该如何解决?

Here my code 这是我的代码

Launcher class 发射器类

public class Main extends Application {

public static void main(String[] args) {
    launch(args);
}

@Override
public void start(Stage primaryStage) throws Exception {
    AnimationButton animationButton = new AnimationButton();
    Scene scene = new Scene(animationButton);
    scene.getStylesheets().add(getClass().getResource("btn.css").toExternalForm());
    primaryStage.setScene(scene);
    primaryStage.setTitle("Custom Control");
    primaryStage.setWidth(300);
    primaryStage.setHeight(200);
    primaryStage.show();
}

AnimationButton.java AnimationButton.java

public class AnimationButton extends AnchorPane{

    private Duration fadeDuration = Duration.millis(1000);
    private FadeTransition fadeTransition;

    @FXML
    private JFXButton animationButton;

    public AnimationButton() {


        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("AnimationButton.fxml"));
        fxmlLoader.setRoot(new AnchorPane());
        fxmlLoader.setController(this);

        try {
            fxmlLoader.load();
        } catch (IOException exception) {
            throw new RuntimeException(exception);
        }

        animationButton.getStyleClass().add("animation-button");
        fadeDuration = new Duration(3000);
        fadeTransition = new FadeTransition(fadeDuration, animationButton);
        fadeTransition.setAutoReverse(true);
        fadeTransition.setFromValue(0);
        fadeTransition.setToValue(1);

    }

    @FXML
    public void mouseEntered(MouseEvent event) {
        fadeTransition.setCycleCount(1); // this way autoreverse wouldn't kick
        fadeTransition.playFromStart();
    }

    @FXML
    public void mouseExited(MouseEvent event) {

        fadeTransition.setCycleCount(2); // starting from autoreverse
        fadeTransition.playFrom(fadeDuration);
    }
    ...
}

Here my fxml file 这是我的fxml文件

<?xml version="1.0" encoding="UTF-8"?>

<?import com.jfoenix.controls.JFXButton?>
<?import javafx.scene.layout.*?>
<fx:root type="AnchorPane" xmlns="http://javafx.com/javafx/8.0.112" 
    xmlns:fx="http://javafx.com/fxml/1">
    <children>
        <JFXButton text="Enjoy it!" id="animationButton" onMouseEntered="#mouseEntered" onMouseExited="#mouseExited"/>
    </children>
</fx:root>

It isn't completely clear what about your current code isn't working, but I'm assuming the following: 目前尚不清楚您当前的代码不起作用,但我假设以下内容:

  1. You want your button to fade in when the mouse enters and fade out when the mouse exits. 您希望按钮在鼠标进入时淡入并在鼠标退出时淡出。
  2. The fade functionality isn't working quite the way you want. 淡入淡出功能无法按照您想要的方式工作。
    • Trying something similar to your setup I noticed the node won't fade out if the mouse exits before the animation has finished. 尝试与您的设置类似的操作,我发现如果在动画完成之前退出鼠标,该节点将不会淡出。

Problem 问题

In what appears to be an attempt to reverse the animation you are modifying the cycleCount property. 在试图反转动画的过程中,您正在修改cycleCount属性。 That property does not affect the direction of play but rather how many cycles the animation plays before stopping: 该属性不会影响播放的方向,但会影响动画在停止之前播放多少个周期:

Defines the number of cycles in this animation. 定义此动画中的循环数。 The cycleCount may be INDEFINITE for animations that repeat indefinitely, but must otherwise be > 0 . 对于无限期重复的动画, cycleCount可以为INDEFINITE ,但否则必须> 0

It is not possible to change the cycleCount of a running Animation . 无法更改正在运行的AnimationcycleCount If the value of cycleCount is changed for a running Animation , the animation has to be stopped and started again to pick up the new value. 如果为正在运行的Animation更改了cycleCount的值,则必须停止并重新启动动画以获取新值。

You combine setting cycleCount with seting autoReverse to true in the hopes that will reverse the animation when you set cycleCount to 2 . 你把设置cycleCount与塞汀autoReversetrue的,当你设置将扭转动画的希望cycleCount2 The autoReverse property: autoReverse属性:

Defines whether this Animation reverses direction on alternating cycles. 定义此Animation是否在交替循环时反转方向。 If true , the Animation will proceed forward on the first cycle, then reverses on the second cycle, and so on. 如果为true ,则Animation将在第一个周期前进,然后在第二个周期后退,依此类推。 Otherwise, animation will loop such that each cycle proceeds forward from the start. 否则,动画将循环播放,以使每个循环都从头开始。 It is not possible to change the autoReverse flag of a running Animation . 不能更改正在运行的AnimationautoReverse标志。 If the value of autoReverse is changed for a running Animation , the animation has to be stopped and started again to pick up the new value. 如果为正在运行的Animation更改了autoReverse的值,则必须停止并重新启动动画以获取新值。

This setup may be working somewhat, especially with the use of playFromStart() and playFrom(fadeDuration) , but is not the correct way to do what you want. 此设置可能在某种程度上起作用,尤其是在使用playFromStart()playFrom(fadeDuration) ,但这不是执行所需操作的正确方法。


Solution

What you want is to modify the rate property based on whether the mouse has entered or exited. 您要根据鼠标是进入还是退出来修改rate属性。 The rate property: rate属性:

Defines the direction/speed at which the Animation is expected to be played. 定义预期播放Animation的方向/速度。

The absolute value of rate indicates the speed at which the Animation is to be played, while the sign of rate indicates the direction. rate的绝对值指示Animation播放的rate ,而rate的符号指示方向。 A positive value of rate indicates forward play, a negative value indicates backward play and 0.0 to stop a running Animation . rate的正值表示向前播放,负值表示向后播放,而0.0表示正在播放的Animation停止。

Rate 1.0 is normal play, 2.0 is 2 time normal, -1.0` is backwards, etc. 速率1.0是正常播放,速率2.0 is 2 time normal,正常播放time normal, -1.0`是倒数time normal,依此类推。

Inverting the rate of a running Animation will cause the Animation to reverse direction in place and play back over the portion of the Animation that has already elapsed. 反转正在运行的Animationrate将导致Animation在原处反向,并在已经过去的Animation部分上播放。

Here's a small example. 这是一个小例子。 It uses Button instead of JFXButton because I didn't feel like pulling in the dependency. 它使用Button而不是JFXButton因为我不想JFXButton依赖项。 Also, it uses the hover property but is functionally equivalent to using mouse-entered/mouse-exited handlers. 此外,它使用hover属性,但在功能上等同于使用鼠标输入/鼠标退出的处理程序。

import javafx.animation.FadeTransition;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.Duration;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) {
        Button button = new Button("Click me!");
        button.setOnAction(event -> {
            event.consume();
            System.out.println("Button clicked!");
        });

        installAnimation(button);

        primaryStage.setScene(new Scene(new StackPane(button), 300.0, 150.0));
        primaryStage.setTitle("Animation Example");
        primaryStage.show();
    }

    private void installAnimation(Button button) {
        FadeTransition transition = new FadeTransition(Duration.millis(250.0), button);
        transition.setFromValue(0.2);
        transition.setToValue(1.0);

        button.hoverProperty().addListener((obs, wasHover, isHover) -> {
            transition.setRate(isHover ? 1.0 : -1.0);
            transition.play();
        });
        button.setOpacity(transition.getFromValue());
    }

}

Notice the following: 请注意以下几点:

  • The rate is set to 1.0 when the mouse is hovering (entered) and -1.0 when the mouse is not hovering (exited). rate被设定为1.0 ,当鼠标悬停 (输入)和-1.0当鼠标悬停(退出)。
  • The autoReverse flag remains false . autoReverse标志保持为false
  • The cycleCount is kept at 1 . cycleCount保持为1
  • I call play() , not playFromStart() or playFrom(Duration) . 我叫play() ,不是playFromStart()playFrom(Duration) This is important because play : 这很重要,因为play

    Plays Animation from current position in the direction indicated by rate . rate指示的方向从当前位置播放Animation If the Animation is running, it has no effect. 如果正在运行Animation ,则它无效。

    When rate > 0 (forward play), if an Animation is already positioned at the end, the first cycle will not be played, it is considered to have already finished. 当rate > 0 (向前播放)时,如果Animation已经定位在末尾,则第一个循环将不会播放,则认为已结束。 This also applies to a backward ( rate < 0 ) cycle if an Animation is positioned at the beginning. 如果将Animation放在开头,则这也适用于向后( rate < 0 )循环。 However, if the Animation has cycleCount > 1 , following cycle(s) will be played as usual. 但是,如果Animation cycleCount > 1 ,则将照常播放以下循环。

    When the Animation reaches the end, the Animation is stopped and the play head remains at the end. Animation结束时, Animation停止,并且播放头保持在结尾处。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM