Javafx 2 时间线不会正确渲染不透明属性。瞬间消失而不是消失。
我偶然发现了一个非常奇怪的行为(在下面的代码中演示了-只需复制粘贴,您就可以亲眼目睹了!)。 按下播放按钮时。。主菜单应淡出,选项菜单应出现。按下“后退”按钮,主菜单再次出现! 现在的问题是:再次按下播放按钮。。菜单不会按预期消失,但会立即消失,然后选项菜单会按预期显示。。 换句话说,第二次触发动画时,动画渲染不正确 出于演示目的,我添加了一个“选项”按钮,该按钮与“播放”按钮的功能完全相同,但动画稍有不同(不是淡出主菜单,而是将其缩小)。此动画始终正常工作 有没有JavaFX2专家能够解释这种异端行为?这是一只虫子吗。。还是我错过了什么 提前谢谢你!这是密码Javafx 2 时间线不会正确渲染不透明属性。瞬间消失而不是消失。,javafx-2,Javafx 2,我偶然发现了一个非常奇怪的行为(在下面的代码中演示了-只需复制粘贴,您就可以亲眼目睹了!)。 按下播放按钮时。。主菜单应淡出,选项菜单应出现。按下“后退”按钮,主菜单再次出现! 现在的问题是:再次按下播放按钮。。菜单不会按预期消失,但会立即消失,然后选项菜单会按预期显示。。 换句话说,第二次触发动画时,动画渲染不正确 出于演示目的,我添加了一个“选项”按钮,该按钮与“播放”按钮的功能完全相同,但动画稍有不同(不是淡出主菜单,而是将其缩小)。此动画始终正常工作 有没有JavaFX2专家能够解释这种
package strangeFx;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application{
@Override
public void start(Stage stage) throws Exception {
Scene scene = new Scene(new StartScreen());
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
和主窗口类
package strangeFx;
import javafx.animation.*;
import javafx.event.*;
import javafx.scene.Group;
import javafx.scene.control.Button;
import javafx.scene.layout.*;
import javafx.util.Duration;
import cls.island.utils.TimeLineExt;
public class StartScreen extends Group {
private static final double ANIM_DURATION = 200D;
VBox mainBtns = new VBox();
VBox optionBtns = new VBox();
/**
* Initializes two group of buttons. the "main" group and "options" group.
* Initially only the menu group is displayed. By pressing the "options" button
* the menu group will be removed and replaced by the options with a small animation!
* When the "play" button is pressed the menu group will be removed and replaced by
* the options button, using a different animation
*
*/
public StartScreen() {
mainBtns.setFillWidth(true);
createOptionButtonGroup();
createStartButton(mainBtns);
createoptionsButton(mainBtns);
createQuitButton(mainBtns);
getChildren().add(mainBtns);
}
private void createOptionButtonGroup() {
Button button = new Button("Back");
button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
goToMain();
}
});
button.setMaxWidth(Double.MAX_VALUE);
optionBtns.getChildren().add(button);
}
private void createStartButton(Pane buttonGroup) {
Button button = new Button("Play");
button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
fadeAndGoToOptions();
}
});
button.setMaxWidth(Double.MAX_VALUE);
buttonGroup.getChildren().add(button);
}
private void createQuitButton(Pane buttonGroup) {
Button button = new Button("Quit");
button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.exit(0);
}
});
button.setMaxWidth(Double.MAX_VALUE);
buttonGroup.getChildren().add(button);
}
private void createoptionsButton(Pane buttonGroup) {
Button button = new Button("Options");
button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
goToOptions();
}
});
button.setMaxWidth(Double.MAX_VALUE);
buttonGroup.getChildren().add(button);
}
public void goToOptions() {
Timeline timeline = new Timeline();
timeline.getKeyFrames().add(new KeyFrame(new Duration(ANIM_DURATION), new KeyValue(mainBtns.scaleXProperty(), 0),
new KeyValue(mainBtns.scaleYProperty(),0)));
timeline.setOnFinished(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
StartScreen.this.getChildren().remove(mainBtns);
mainBtns.scaleXProperty().set(1);
mainBtns.scaleYProperty().set(1);
Timeline timeline = new Timeline();
optionBtns.scaleXProperty().set(0);
optionBtns.scaleYProperty().set(0);
StartScreen.this.getChildren().add(optionBtns);
timeline.getKeyFrames().add(new KeyFrame(new Duration(ANIM_DURATION), new KeyValue(optionBtns.scaleXProperty(), 1),
new KeyValue(optionBtns.scaleYProperty(),1)));
timeline.play();
}
});
timeline.play();
}
public void goToMain() {
TimeLineExt timeline = new TimeLineExt();
timeline.getKeyFrames().add(new KeyFrame(new Duration(ANIM_DURATION), new KeyValue(optionBtns.scaleXProperty(), 0),
new KeyValue(optionBtns.scaleYProperty(),0)));
timeline.setOnFinished(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
StartScreen.this.getChildren().remove(optionBtns);
optionBtns.scaleXProperty().set(1);
optionBtns.scaleYProperty().set(1);
Timeline timeline = new Timeline();
mainBtns.scaleXProperty().set(0);
mainBtns.scaleYProperty().set(0);
StartScreen.this.getChildren().add(mainBtns);
timeline.getKeyFrames().add(new KeyFrame(new Duration(ANIM_DURATION), new KeyValue(mainBtns.scaleXProperty(), 1),
new KeyValue(mainBtns.scaleYProperty(),1)));
timeline.play();
}
});
timeline.play();
}
/**
* This is where the problematic behavior occurs, but only when this animation is triggered
* for second time! The first time this animation runs and animates properly!
* The correct animation is to 1.
*/
public void fadeAndGoToOptions() {
Timeline timeline = new Timeline();
timeline.getKeyFrames().add(new KeyFrame(new Duration(1000), new KeyValue(this.opacityProperty(),0)));
timeline.setOnFinished(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
getChildren().remove(mainBtns);
StartScreen.this.opacityProperty().set(100);
Timeline timeline = new Timeline();
optionBtns.scaleXProperty().set(0);
optionBtns.scaleYProperty().set(0);
StartScreen.this.getChildren().add(optionBtns);
timeline.getKeyFrames().add(new KeyFrame(new Duration(ANIM_DURATION), new KeyValue(optionBtns.scaleXProperty(), 1),
new KeyValue(optionBtns.scaleYProperty(),1)));
timeline.play();
}
});
timeline.play();
}
}
fx;
导入javafx.animation.*;
导入javafx.event.*;
导入javafx.scene.Group;
导入javafx.scene.control.Button;
导入javafx.scene.layout.*;
导入javafx.util.Duration;
导入cls.island.utils.TimeLineExt;
公共类StartScreen扩展组{
私人静态最终双动画持续时间=200D;
VBox mainBtns=新的VBox();
VBox optionBtns=新的VBox();
/**
*初始化两组按钮。“主”组和“选项”组。
*最初仅显示菜单组。按“选项”按钮
*菜单组将被删除并替换为带有小动画的选项!
*当按下“播放”按钮时,菜单组将被删除并替换为
*“选项”按钮,使用不同的动画
*
*/
公共StartScreen(){
mainBtns.setFillWidth(真);
createOptionButtonGroup();
createStartButton(主BTN);
创建选项按钮(MainBTN);
createQuitButton(mainBtns);
getChildren().add(mainBtns);
}
私有void createOptionButtonGroup(){
按钮按钮=新按钮(“后退”);
setOnAction(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent事件){
goToMain();
}
});
按钮。设置最大宽度(双倍最大值);
optionBtns.getChildren().add(按钮);
}
私有void createStartButton(窗格按钮组){
按钮按钮=新按钮(“播放”);
setOnAction(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent事件){
fadeAndGoToOptions();
}
});
按钮。设置最大宽度(双倍最大值);
buttonGroup.getChildren().add(按钮);
}
私有void createQuitButton(窗格按钮组){
按钮按钮=新按钮(“退出”);
setOnAction(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent事件){
系统出口(0);
}
});
按钮。设置最大宽度(双倍最大值);
buttonGroup.getChildren().add(按钮);
}
私有void CreateOptions按钮(窗格按钮组){
按钮按钮=新按钮(“选项”);
setOnAction(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent事件){
gotoopions();
}
});
按钮。设置最大宽度(双倍最大值);
buttonGroup.getChildren().add(按钮);
}
public void gotoopions(){
时间线=新时间线();
timeline.getKeyFrames().add(新关键帧(新持续时间)(动画持续时间),新键值(mainBtns.scaleXProperty(),0),
新的键值(mainBtns.scaleYProperty(),0));
timeline.setOnFinished(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent事件){
StartScreen.this.getChildren().remove(mainBtns);
mainBtns.scaleProperty().set(1);
mainBtns.scaleYProperty().set(1);
时间线=新时间线();
optionBtns.scaleProperty().set(0);
optionBtns.scaleYProperty().set(0);
StartScreen.this.getChildren().add(optionBtns);
timeline.getKeyFrames().add(新关键帧(新持续时间(动画持续时间))和新键值(optionBtns.ScaleProperty(),1),
新的键值(optionBtns.scaleYProperty(),1));
timeline.play();
}
});
timeline.play();
}
公共无效的goToMain(){
TimeLineExt timeline=新的TimeLineExt();
timeline.getKeyFrames().add(新关键帧(新持续时间(动画持续时间))和新键值(optionBtns.ScaleProperty(),0),
新的键值(optionBtns.scaleYProperty(),0));
timeline.setOnFinished(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent事件){
StartScreen.this.getChildren().remove(optionBtns);
optionBtns.scaleProperty().set(1);
optionBtns.scaleYProperty().set(1);
时间线=新时间线();
mainBtns.scaleProperty().set(0);
mainBtns.scaleYProperty().set(0);
StartScreen.this.getChildren().add(mainBtns);
timeline.getKeyFrames().add(新的关键帧(新的持续时间(动画持续时间)),新的键值(mainBtns.scaleXProperty(),1),
新的键值(mainBtns.scaleYProperty(),1));
timeline.play();
}
});
timeline.play();
}
/**
*这就是问题行为发生的地方,但仅当触发此动画时
*这是第二次!这是第一次正确运行和设置动画!
*正确的动画设置为1。
*/
公共无效fadeAndGoToOptions(){
时间线=新时间线();
timeline.getKeyFrames().add(新的关键帧(新的持续时间(1000),新的键值(this.opacityProperty(),0));
timeline.setOnFinished(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent事件){
getChildren().remove(mainBtns);
StartScreen.this.opac