有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java获取在控件的类定义中添加自定义控件的场景大小

我有一个涟漪按钮,一旦点击,就会播放涟漪效果动画。 在按钮的皮肤内部,有一个Circle,在程序开始执行时,它的不透明度被设置为0,单击按钮后,不透明度被设置为1,按钮的半径变大

有一种DoubleBinding结合,其定义如下:

DoubleBinding circleRippleRadius = new DoubleBinding() {
            {
                bind(heightProperty(), widthProperty()); // Sets the parameters of the bind method.
            }

            @Override // Overrides the computeValue method, which computes the value of the binding.
            protected double computeValue() {
                return Math.max(heightProperty().get(), widthProperty().get()); // Return the greatest of both numbers
            }
        };

我希望使用添加按钮的场景的高度和宽度,而不是使用按钮的heightPropertywidthProperty属性,因为我希望按钮单击后出现的圆圈填充整个屏幕

我怎样才能做到这一点

更新:以下是定义动画组件值和动画本身的代码:

    private void createRippleEffect(Circle circleRipple) {

            circleRipple.setOpacity(1.0); // Sets the opacity of the circleRipple to 0, since it must not be showed yet.

            /*Fade Transition*/
            FadeTransition fadeTransition = new FadeTransition(RIPPLE_DURATION, circleRipple);
            fadeTransition.setInterpolator(Interpolator.EASE_OUT);
            fadeTransition.setFromValue(1.0); // Sets the opacity to %100
            fadeTransition.setToValue(1.0); // The opacity doesn't change. 

            /*Scale Transition*/
            Timeline scaleRippleTimeline = new Timeline();

            NumberBinding circleRippleRadius = 
                    Bindings.max(Bindings.selectDouble(sceneProperty(), "width"),
                    Bindings.selectDouble(sceneProperty(), "height"));

            circleRippleRadius.addListener((ObservableValue<? extends Number> observable, Number oldValue, Number newValue) -> { // Each time it changes
                KeyValue scaleValue = new KeyValue(circleRipple.radiusProperty(), newValue, Interpolator.EASE_OUT);
                KeyFrame scaleFrame = new KeyFrame(RIPPLE_DURATION, scaleValue);
                scaleRippleTimeline.getKeyFrames().add(scaleFrame);
            });

private void createRippleEffect(Circle circleRipple) {

        circleRipple.setOpacity(1.0); // Sets the opacity of the circleRipple to 0, since it must not be showed yet.

        /*Fade Transition*/
        FadeTransition fadeTransition = new FadeTransition(RIPPLE_DURATION, circleRipple);
        fadeTransition.setInterpolator(Interpolator.EASE_OUT);
        fadeTransition.setFromValue(1.0); // Sets the opacity to %100
        fadeTransition.setToValue(1.0); // The opacity doesn't change. 

        /*Scale Transition*/
        Timeline scaleRippleTimeline = new Timeline();

        NumberBinding circleRippleRadius = 
                Bindings.max(Bindings.selectDouble(sceneProperty(), "width"),
                Bindings.selectDouble(sceneProperty(), "height"));

        circleRippleRadius.addListener((ObservableValue<? extends Number> observable, Number oldValue, Number newValue) -> { // Each time it changes
            KeyValue scaleValue = new KeyValue(circleRipple.radiusProperty(), newValue, Interpolator.EASE_OUT);
            KeyFrame scaleFrame = new KeyFrame(RIPPLE_DURATION, scaleValue);
            scaleRippleTimeline.getKeyFrames().add(scaleFrame);
        });

        SequentialTransition rippleTransition = new SequentialTransition(); // The circle must change its opacity and scale at the same time
        rippleTransition.getChildren().addAll(
                scaleRippleTimeline,
                fadeTransition
        );

        ParallelTransition parallelTransition = new ParallelTransition();

        getStyleClass().addListener((ListChangeListener.Change<? extends String> c) -> { // Don't pay attention to this. The style changes if
                                                                                         // the CSS file has "toggle" or "flat" inside its list,
                                                                                         // but these are never added so it doesn't matter.
            if (c.getList().indexOf("flat") == -1 && c.getList().indexOf("toggle") == -1) {
                setMinWidth(88);
                setEffect(new DropShadow(BlurType.GAUSSIAN, Color.rgb(0, 0, 0, 0.30), 5, 0.10, 0, 2));
                parallelTransition.getChildren().addAll(rippleTransition); // parallelTransition is basically the same as rippleTransition, since
                                                                           // "toggle" and "flat" are never added to the CSS's list.
            } else {

                parallelTransition.getChildren().addAll(rippleTransition);
                setMinWidth(USE_COMPUTED_SIZE);
                setEffect(null);
            }
        });

        this.addEventHandler(MouseEvent.MOUSE_PRESSED, event -> {
            parallelTransition.stop(); // In case that the parallelTransition is already running, stop it.
            circleRipple.setOpacity(0.0); // Sets the opacity of the circle to 0, since the animation must play from he beginning.
            circleRipple.setRadius(0.1); // Sets the radius to 0.1 for the same reason as the circle's opacity.
            circleRipple.setCenterX(event.getX()); // The center of the circle is the location in which the mouse was clicked.
            circleRipple.setCenterY(event.getY()); // The center of the circle is the location in which the mouse was clicked.
            parallelTransition.playFromStart(); // Plays the animation.

        });
    }

这就是定义按钮外观的方法:

@Override
    public Skin<?> createDefaultSkin() {
        ButtonSkin buttonSkin = getButtonSkin();
        if (buttonSkin == null) {
            buttonSkin = new ButtonSkin(this);
            Circle circleRipple = new Circle(0.1, RIPPLE_COLOR);
            buttonSkin.getChildren().add(0, circleRipple);
            setSkin(buttonSkin);

            createRippleEffect(circleRipple);
            getStyleClass().add("ripple-button"); // What the CSS does is changing the button's color and text size, nothing important.
        }
        return buttonSkin;
    }

提前谢谢

解决方案: 问题是circleRippleRadius的值比舞台的值短,这可能是因为舞台的大小与场景的大小不同。我不知道,现在我知道了

为了让按钮的圆圈填满整个屏幕,我所要做的就是将stage的widthPropertyheightProperty属性作为参数传递给按钮的构造函数

在button的类中,我为宽度和高度创建了两个ReadOnlyDoubleProperty属性,它们在创建按钮之前是空的;在这种情况下,定义的ReadOnlyDoubleProperty属性的值将被作为参数传递的widthPropertyheightProperty属性的值覆盖

这样做之后,我所要做的就是为每个属性添加一个侦听器,并在其中一个属性每次更改时将circleRippleRadius的值更改为更大的属性值


共 (1) 个答案

  1. # 1 楼答案

    您不应该扩展双重绑定。相反,您应该从现有的工厂方法创建绑定:

    NumberBinding circleRippleRadius =
        Bindings.max(widthProperty(), heightProperty());
    
    // Optional
    DoubleExpression circleRippleRadiusAsDouble =
        DoubleExpression.doubleExpression(circleRippleRadius);
    

    要绑定到场景属性,需要使用Bindings.selectDouble,它可以处理按钮最初为空的场景属性:

    NumberBinding size =
        Bindings.max(
            Bindings.selectDouble(button.sceneProperty(), "width"),
            Bindings.selectDouble(button.sceneProperty(), "height"));