[英]javafx custom ui component FadeTransition 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 ?
我该如何解决?
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();
}
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);
}
...
}
<?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: 目前尚不清楚您当前的代码不起作用,但我假设以下内容:
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 beINDEFINITE
for animations that repeat indefinitely, but must otherwise be> 0
.对于无限期重复的动画,
cycleCount
可以为INDEFINITE
,但否则必须> 0
。It is not possible to change the
cycleCount
of a runningAnimation
.无法更改正在运行的
Animation
的cycleCount
。 If the value ofcycleCount
is changed for a runningAnimation
, 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
与塞汀autoReverse
到true
的,当你设置将扭转动画的希望cycleCount
到2
。 The autoReverse
property: autoReverse
属性:
Defines whether this
Animation
reverses direction on alternating cycles.定义此
Animation
是否在交替循环时反转方向。 Iftrue
, theAnimation
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 runningAnimation
.不能更改正在运行的
Animation
的autoReverse
标志。 If the value ofautoReverse
is changed for a runningAnimation
, 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)
,但这不是执行所需操作的正确方法。
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 theAnimation
is to be played, while the sign ofrate
indicates the direction.rate
的绝对值指示Animation
播放的rate
,而rate
的符号指示方向。 A positive value ofrate
indicates forward play, a negative value indicates backward play and0.0
to stop a runningAnimation
.rate
的正值表示向前播放,负值表示向后播放,而0.0
表示正在播放的Animation
停止。Rate
1.0
is normal play,2.0 is
2time normal,
-1.0` is backwards, etc.速率
1.0
是正常播放,速率2.0 is
2time normal,
正常播放time normal,
-1.0`是倒数time normal,
依此类推。Inverting the
rate
of a runningAnimation
will cause theAnimation
to reverse direction in place and play back over the portion of theAnimation
that has already elapsed.反转正在运行的
Animation
的rate
将导致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: 请注意以下几点:
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
当鼠标未悬停(退出)。 autoReverse
flag remains false
. autoReverse
标志保持为false
。 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 byrate
.从
rate
指示的方向从当前位置播放Animation
。 If theAnimation
is running, it has no effect.如果正在运行
Animation
,则它无效。When rate
> 0
(forward play), if anAnimation
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 anAnimation
is positioned at the beginning.如果将
Animation
放在开头,则这也适用于向后(rate < 0
)循环。 However, if theAnimation
hascycleCount > 1
, following cycle(s) will be played as usual.但是,如果
Animation
cycleCount > 1
,则将照常播放以下循环。When the
Animation
reaches the end, theAnimation
is stopped and the play head remains at the end.当
Animation
结束时,Animation
停止,并且播放头保持在结尾处。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.