JavaFX Transition - 悬停时使按钮变暗 [英] JavaFX Transition - Darken button on hover

查看:45
本文介绍了JavaFX Transition - 悬停时使按钮变暗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 JavaFX 的初学者.我正在尝试创建我自己的 Button 子类,该子类将具有鼠标进入和鼠标退出的动画.我试图实现的动画是一个简单的变暗"或变暗"过渡,当用户将鼠标悬停在按钮上时,它会使按钮背景的颜色变暗,并在鼠标退出按钮时恢复正常状态.

I'm a beginner in JavaFX. I'm trying to create my own Button subclass that would have its on animations for mouse enter and mouse exit. The animation I'm trying to achieve is a simple "darken" or "dim" transition that would darken the color of the button background when user hovers over the button , and would animate back to normal state when the mouse exits the button.

首先,我认为我可以通过 FillTransition 实现这一点,但为此我需要按钮的特定较深颜色,这取决于按钮颜色.所以现在我试图基本上淡入和淡出按钮顶部的低不透明度黑色矩形,但该矩形似乎根本没有出现.

First I thought I can achieve this with FillTransition, but for that I would need the specific darker color of the button, that depends on the button color. So now I'm trying to basically fade in and fade out a low-opacity black rectangle on top of the button, but the rectangle doesn't seem to appear at all.

这是我的按钮的代码:

public class FlatButton extends Button {

    private Rectangle dimRectangle;
    private Duration dimDuration = Duration.millis(250);
    private Color dimColor = new Color(0,0,0,0.11);

    public FlatButton(String text) {
        super(text);

        getStyleClass().addAll("flat-button-style");
        createEffect();
    }

    private void createEffect() 
    {
        dimRectangle = new Rectangle(this.getWidth(), this.getHeight(), dimColor);
        dimRectangle.setOpacity(1.0);
        dimRectangle.setX(this.get);


        FadeTransition enterTransition = new FadeTransition(dimDuration, this);
        enterTransition.setInterpolator(Interpolator.EASE_OUT);
        enterTransition.setFromValue(0.0);
        enterTransition.setToValue(1.0);

        FadeTransition exitTransition = new FadeTransition(dimDuration, this);
        exitTransition.setInterpolator(Interpolator.EASE_OUT);
        exitTransition.setFromValue(1.0);
        exitTransition.setToValue(0.0);


        this.setOnMouseEntered(new EventHandler<MouseEvent>(){

            public void handle(MouseEvent mouseEvent){
                enterTransition.play();
            }
        });

        this.setOnMouseExited(new EventHandler<MouseEvent>(){

            public void handle(MouseEvent mouseEvent){
                exitTransition.play();
            }
        });
    }

}

代码new FadeTransition(dimDuration, this);"中的部分应该是new FadeTransition(dimDuration, dimRectangle);".这只是我正在测试的东西.

The part in the code "new FadeTransition(dimDuration, this);" should be "new FadeTransition(dimDuration, dimRectangle);". It's just something I was testing.

我认为dimRectangle = new Rectangle(this.getWidth(), this.getHeight(), dimColor);"并没有真正起作用,但我还没有找到如何使矩形填充按钮尺寸的方法.

I figured that "dimRectangle = new Rectangle(this.getWidth(), this.getHeight(), dimColor);" is not really working , but I havent found a way yet how to make the rectangle fill the button dimensions.

推荐答案

您可以使用 ColorAdjust 效果并将其更改为 亮度 属性使用 时间轴.

You could use a ColorAdjust effect and change it's brightness property using a Timeline.

public class ButtonFadeDemo extends Application {

    @Override
    public void start(Stage primaryStage) {

        try {

            Pane root = new Pane();

            Button button = new Button("Click me!");

            ColorAdjust colorAdjust = new ColorAdjust();
            colorAdjust.setBrightness(0.0);

            button.setEffect(colorAdjust);

            button.setOnMouseEntered(e -> {

                Timeline fadeInTimeline = new Timeline(
                        new KeyFrame(Duration.seconds(0), 
                                new KeyValue(colorAdjust.brightnessProperty(), colorAdjust.brightnessProperty().getValue(), Interpolator.LINEAR)), 
                                new KeyFrame(Duration.seconds(1), new KeyValue(colorAdjust.brightnessProperty(), -1, Interpolator.LINEAR)
                                ));
                fadeInTimeline.setCycleCount(1);
                fadeInTimeline.setAutoReverse(false);
                fadeInTimeline.play();

            });

            button.setOnMouseExited(e -> {

                Timeline fadeOutTimeline = new Timeline(
                        new KeyFrame(Duration.seconds(0), 
                                new KeyValue(colorAdjust.brightnessProperty(), colorAdjust.brightnessProperty().getValue(), Interpolator.LINEAR)), 
                                new KeyFrame(Duration.seconds(1), new KeyValue(colorAdjust.brightnessProperty(), 0, Interpolator.LINEAR)
                                ));
                fadeOutTimeline.setCycleCount(1);
                fadeOutTimeline.setAutoReverse(false);
                fadeOutTimeline.play();

            });

            root.getChildren().addAll(button);

            Scene scene = new Scene(root, 800, 400);
            primaryStage.setScene(scene);
            primaryStage.show();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


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

这篇关于JavaFX Transition - 悬停时使按钮变暗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆