【发布时间】: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属性轻松实现。