我在VBox中包含了一些标签,然后将VBox放入滚动窗格。我通过在Scroll Pane上调用setVvalue()来自动滚动Scroll Pane。所以我想要的是当Label对象开始接近滚动窗格的顶边或底边时,它应该在滚动窗格中移出视图时开始淡出(降低其不透明度)。我正在努力弄清楚如何实现这个逻辑,有没有办法找出一个对象在Scroll Pane中的位置?或者这会更复杂。任何帮助将不胜感激!
答案 0 :(得分:3)
我做了一些关于如何实现这一目标的研究,并提出了以下建议:
每次ChangeListener
滚动时,您都可以向vvalueProperty
的{{1}}添加scrollPane
。从这里我们需要检查是否有任何标签位于视图的顶部或底部边缘。我遇到了this article,它提供了一种获取子元素可见边界的方法。
通过一些反复试验,我发现最接近边缘的scrollPane
和minY
值的标签在接近边缘时接近于零。通过使用它,它们的不透明度可以根据它们与边缘的接近程度而改变。我设置了应该更改不透明度的阈值,但您也可以指定常量值。这是我最终得到的图像:
以下是所有代码:
主要课程:
maxY
控制器:
public class Main extends Application{
public static Stage primaryStage;
@Override
public void start(Stage primaryStage) throws Exception {
Main.primaryStage=primaryStage;
AnchorPane page = (AnchorPane) FXMLLoader.load(MainController.class.getResource("mainFXML.fxml"));
Scene scene = new Scene(page);
primaryStage.setTitle("Fade example");
primaryStage.setScene(scene);
// Stage positioning
Rectangle2D primaryScreenBounds = Screen.getPrimary().getVisualBounds();
// stage positioning
primaryStage.setX(primaryScreenBounds.getMaxX()*5/8);
primaryStage.setY(primaryScreenBounds.getMinY() + page.getPrefHeight()*2/4);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
和MainFXML.fxml:
public class MainController {
private static final double FADE_THRESHOLD = 80;
@FXML
private ResourceBundle resources;
@FXML
private URL location;
@FXML
private ScrollPane scrollPane;
@FXML
private AnchorPane rootPane;
@FXML
private VBox vBox;
@FXML
void initialize() {
assert vBox != null : "fx:id=\"vBox\" was not injected: check your FXML file 'MainFXML.fxml'.";
assert scrollPane != null : "fx:id=\"scrollPane\" was not injected: check your FXML file 'MainFXML.fxml'.";
assert rootPane != null : "fx:id=\"rootPane\" was not injected: check your FXML file 'MainFXML.fxml'.";
ArrayList<Label> labels = new ArrayList<>();
for (int i = 0; i < 300; i++) {
Label l = new Label("Label "+i);
l.setBackground(new Background(new BackgroundFill(Color.AQUA, CornerRadii.EMPTY, Insets.EMPTY)));
labels.add(l);
}
labels.forEach( e-> vBox.getChildren().add(e));
scrollPane.vvalueProperty().addListener(new ChangeListener<Number>()
{
@Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue)
{
//System.out.println("bounds: "+getVisibleBounds(labels.get(51)));
for(Label l: labels) {
Bounds bounds = getVisibleBounds(l);
double minY = bounds.getMinY();
double maxY = bounds.getMaxY();
if(Math.abs(minY) < FADE_THRESHOLD ) {
l.setOpacity(1-(FADE_THRESHOLD-Math.abs(minY))/FADE_THRESHOLD);
}else if(Math.abs(maxY) < FADE_THRESHOLD) {
l.setOpacity(1-(FADE_THRESHOLD-Math.abs(maxY))/FADE_THRESHOLD);
}else {
l.setOpacity(1);
}
}
}
});
}
public static Bounds getVisibleBounds(Node aNode)
{
// If node not visible, return empty bounds
if(!aNode.isVisible()) return new BoundingBox(0,0,-1,-1);
// If node has clip, return clip bounds in node coords
if(aNode.getClip()!=null) return aNode.getClip().getBoundsInParent();
// If node has parent, get parent visible bounds in node coords
Bounds bounds = aNode.getParent()!=null? getVisibleBounds(aNode.getParent()) : null;
if(bounds!=null && !bounds.isEmpty()) bounds = aNode.parentToLocal(bounds);
return bounds;
}
}
希望这有帮助。