我正在做一个项目,我想在其中显示带有叠加层的图像。
我的想法是创建一个 AncorPane,在其背景中放置一个 ImageView,然后在其上方放置几个形状。这确实有效。
由于图片很大,需要根据可用空间进行缩小。不用说,想要缩小所有覆盖形状,使它们相对于图像保持在同一位置。从视觉上看,这也可以通过对 AncorPane 应用缩放转换来实现,但我的问题来了。
AncorPane 从它的 child 那里获取它的大小。但即使它们按比例缩小,AncorPane 仍保持未转换图像的大小。这会产生大量空白。 我认为这是布局边界与不可调整大小节点的父级边界之间存在差异的问题。
有没有人对设置 AncorPane 矿石的实际大小有另一种摆脱这种不需要的空白的方法的建议。
例子:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.SceneBuilder;
import javafx.scene.control.ScrollPane;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import javafx.scene.shape.Rectangle;
import javafx.scene.transform.Scale;
import javafx.stage.Stage;
public class test extends Application
{
@Override
public void start(final Stage stage) throws Exception
{
final ScrollPane scroll = new ScrollPane();
final Scene scene = SceneBuilder.create().root(scroll).build();
final VBox box = new VBox();
final AnchorPane anchorPane = new AnchorPane();
final ImageView imageView = new ImageView();
final Shape overlayRectangle = new Rectangle(100, 100, 100, 100);
scroll.setContent(box);
box.getChildren().add(anchorPane);
anchorPane.getChildren().addAll(imageView, overlayRectangle);
overlayRectangle.toFront();
imageView.setImage(new Image("http://cdn.sstatic.net/stackoverflow/img/sprites.png"));
final Scale scale = new Scale(0.5, 0.5);
anchorPane.getTransforms().add(scale);
stage.setScene(scene);
stage.show();
}
public static void main(final String[] args)
{
launch();
}
}
运行该示例时,您可以看到只有当您将窗口大小扩展到图像大小的两倍时,滚动条才会消失。我知道我可以禁用滚动条。这可能适用于示例代码,但请记住,此示例已大大缩短。我什至删除了动态调整大小。
最佳答案
我将引用 ScrollPane documentation 中的一句话来回答我自己的问题以防有人发现。
ScrollPane layout calculations are based on the layoutBounds rather than the boundsInParent (visual bounds) of the scroll node. If an application wants the scrolling to be based on the visual bounds of the node (for scaled content etc.), they need to wrap the scroll node in a Group.
关于java - Pane 未调整到其转换后的尺寸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14253019/