【问题标题】:Binding Shape and Size of Node to Clipping Mask in JavaFX在 JavaFX 中将节点的形状和大小绑定到剪贴蒙版
【发布时间】:2019-09-20 06:25:27
【问题描述】:

剪贴蒙版是否有一种方便的方法来绑定目标节点的任何形状/大小?考虑以下 Region 节点:

import javafx.application.Application;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class App extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Region content = new Region();
        content.setStyle(
            "-fx-background-color: #444444;" +
            "-fx-background-radius: 50px;" +
            "-fx-max-width: 150px;" +
            "-fx-max-height: 150px;");
        StackPane root = new StackPane(content);
        Scene scene = new Scene(root, 300, 300);
        stage.setScene(scene);
        stage.show();
    }
}

我怎样才能有一个可以相应地自动调整自己的形状和大小的面具?


编辑

这就是我实现这些东西的方式,但它太长且不方便:

import javafx.application.Application;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.Region;
import javafx.scene.Scene;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class App extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Region content1 = new Region();
        content1.setStyle(
            "-fx-background-color: teal;" +
            "-fx-background-radius: 50px;" +
            "-fx-pref-width: 150px;" +
            "-fx-pref-height: 150px;" +
            "-fx-translate-x: 30px;" +
            "-fx-translate-y: 30px;");

        Rectangle mask = new Rectangle();

        Region content2 = new Region() {{
            // TODO: Implement better clip mask size and shape handling
            mask.widthProperty().bind(widthProperty());
            mask.heightProperty().bind(heightProperty());
            backgroundProperty().addListener(((observable, oldBackground, newBackground) -> {
                for (BackgroundFill backgroundFill : newBackground.getFills()) {
                    double topLeftHRadius = backgroundFill.getRadii().getTopLeftHorizontalRadius();
                    double topLeftVRadius = backgroundFill.getRadii().getTopLeftVerticalRadius();
                    double topRightHRadius = backgroundFill.getRadii().getTopRightHorizontalRadius();
                    double topRightVRadius = backgroundFill.getRadii().getTopRightVerticalRadius();
                    double bottomLeftHRadius = backgroundFill.getRadii().getBottomLeftHorizontalRadius();
                    double bottomLeftVRadius = backgroundFill.getRadii().getBottomLeftVerticalRadius();
                    double bottomRightHRadius = backgroundFill.getRadii().getBottomRightHorizontalRadius();
                    double bottomRightVRadius = backgroundFill.getRadii().getBottomRightVerticalRadius();
                    mask.setArcWidth((topLeftHRadius + topRightHRadius + bottomLeftHRadius + bottomRightHRadius) / 2);
                    mask.setArcHeight((topLeftVRadius + topRightVRadius + bottomLeftVRadius + bottomRightVRadius) / 2);
                }
            }));
            setClip(mask);
            getChildren().add(content1);
        }};
        content2.setStyle(
            "-fx-background-color: cyan;" +
            "-fx-background-radius: 50px;" +
            "-fx-pref-width: 150px;" +
            "-fx-pref-height: 150px;" +
            "-fx-translate-x: 70px;" +
            "-fx-translate-y: 70px;");

        Region root = new Region() {{
            getChildren().add(content2);
        }};

        Scene scene = new Scene(root, 300, 300);

        stage.setScene(scene);
        stage.setResizable(false);
        stage.show();
    }
}

我正在寻找一种方便且更好的方法来执行此操作。另一方面,HTML + CSS overflow 属性可以轻松实现:

div {
  position: relative;
  background-color: cyan;
  border-radius: 50px;
  width: 150px;
  height: 150px;
  /* clipping property */
  overflow: hidden;
}

div:before {
  content: '';
  position: absolute;
  top: 30px;
  left: 30px;
  background-color: teal;
  border-radius: inherit;
  width: 100%;
  height: 100%;
}
<div></div>

【问题讨论】:

  • 与被剪裁的节点形状和大小相同的剪裁点是什么?我认为它根本不会取得任何成果。
  • @jewelsea 我的意思是简单地剪辑内容,或者类似overflowing 剪辑。在 HTML + CSS 中,这可以使用overflow 属性轻松实现。

标签: java javafx


【解决方案1】:

运行代码并尝试操纵窗口的大小。

    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.image.ImageView;
    import javafx.scene.layout.StackPane;
    import javafx.scene.shape.Circle;
    import javafx.stage.Stage;

    public class ClipApp extends Application {

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

        @Override
        public void start(Stage stage) throws Exception {
            ImageView imageView = new ImageView("https://hips.hearstapps.com/ghk.h-cdn.co/assets/18/01/2048x1024/landscape-1515004324-boston-terrier.jpg?resize=480:*");
            StackPane stackPane = new StackPane(imageView);
            Scene scene = new Scene(stackPane);
            stage.setScene(scene);
            stage.show();

            Circle circle = new Circle(60);
            circle.centerXProperty().bind(stackPane.widthProperty().divide(2.));
            circle.centerYProperty().bind(stackPane.heightProperty().divide(2.));
            stackPane.setClip(circle);
        }
    }

【讨论】:

  • 绑定属性是要走的路。但是,如果我希望 Shape 节点的大小和形状与目标节点完全相同,该怎么办?就像我的示例区域节点一样,克隆节点本身并将另一个节点设置为其剪贴蒙版可能会容易得多?
  • 我宁愿使用矩形而不是克隆区域。但你提出的整个概念是正确的。
  • 是的,由于某些原因,如果除Shape节点之外的任何节点都设置为掩码,则根本不会渲染整个节点。现在我正在尝试一种不同的方法,而不是剪贴蒙版来达到相同的效果。稍后会更新我的帖子...
  • "克隆节点本身可能会容易得多" => JavaFX 本身不支持节点克隆,因此可能很难实现。
  • @jewelsea 对不起,我选择了单词,但我的意思是同一节点的两个实例,并将另一个设置为另一个的剪辑蒙版
猜你喜欢
  • 1970-01-01
  • 2011-06-23
  • 1970-01-01
  • 2016-05-02
  • 2012-05-21
  • 2012-07-07
  • 1970-01-01
  • 2011-11-13
  • 2011-10-20
相关资源
最近更新 更多