在 JavaFX 中将 ImageView 缩放到特定高度
Scaling an ImageView to a specific height in JavaFX
我的菜单中有一个 Imageview
,它显示所选地图的预览。
但是这些地图的大小可能会有所不同,在我的菜单中我有足够的水平 space,但垂直我有限制。所以,我想要的是缩放 ImageView
使其高度变为 40 像素(我这里也有问题)并且它的宽度相应地以不变形的方式变化。
此外,如果有一种方法可以让高度自动适应它的线的高度,我将不胜感激。(其parent)
这是我的代码:
Label mapChooserLabel = new Label("This is the map you will play on, click to change");
try {
mapPreview = new ImageView(
new Image(getClass().getResource("/files/images/maps/" + chosenMapName + ".gif").toURI().toURL().toString())
);
mapPreview.setFitHeight(40);
mapPreview.setOnMouseClicked(event -> System.out.println("towerChooserPopup should open now!"));
} catch (MalformedURLException | URISyntaxException e) {
e.printStackTrace();
}
HBox mapChooserWrapper = new HBox(mapChooserLabel, mapPreview);
mapChooserWrapper.setId("lineWrapper");
这是我的 css:
#lineWrapper{
-fx-alignment: center;
-fx-spacing: 2px;
}
可以使用ImageView
的setPreserveRatio
方法:
Indicates whether to preserve the aspect ratio of the source image
when scaling to fit the image within the fitting bounding box.
If set to true, it affects the dimensions of this ImageView in the
following way *
- If only fitWidth is set, height is scaled to preserve ratio
- If only fitHeight is set, width is scaled to preserve ratio
- If both are set, they both may be scaled to get the best fit in a width by height rectangle while preserving the original aspect ratio
因此,在您的代码中:
mapPreview.setFitHeight(40);
mapPreview.setPreserveRatio(true);
或者您甚至可以使用 its constructor:
在加载时调整 Image
的大小
mapPreview = new ImageView(
new Image(getClass().getResource("/files/images/maps/" + chosenMapName + ".gif").toURI().toURL().toString(),
40, 200, true, true));
但在你的情况下,因为你想用它作为预览,以原始大小加载它并存储图像参考更合理,然后你可以在显示原始图像时使用它以避免重新加载图像。
我的菜单中有一个 Imageview
,它显示所选地图的预览。
但是这些地图的大小可能会有所不同,在我的菜单中我有足够的水平 space,但垂直我有限制。所以,我想要的是缩放 ImageView
使其高度变为 40 像素(我这里也有问题)并且它的宽度相应地以不变形的方式变化。
此外,如果有一种方法可以让高度自动适应它的线的高度,我将不胜感激。(其parent) 这是我的代码:
Label mapChooserLabel = new Label("This is the map you will play on, click to change");
try {
mapPreview = new ImageView(
new Image(getClass().getResource("/files/images/maps/" + chosenMapName + ".gif").toURI().toURL().toString())
);
mapPreview.setFitHeight(40);
mapPreview.setOnMouseClicked(event -> System.out.println("towerChooserPopup should open now!"));
} catch (MalformedURLException | URISyntaxException e) {
e.printStackTrace();
}
HBox mapChooserWrapper = new HBox(mapChooserLabel, mapPreview);
mapChooserWrapper.setId("lineWrapper");
这是我的 css:
#lineWrapper{
-fx-alignment: center;
-fx-spacing: 2px;
}
可以使用ImageView
的setPreserveRatio
方法:
Indicates whether to preserve the aspect ratio of the source image when scaling to fit the image within the fitting bounding box.
If set to true, it affects the dimensions of this ImageView in the following way *
- If only fitWidth is set, height is scaled to preserve ratio
- If only fitHeight is set, width is scaled to preserve ratio
- If both are set, they both may be scaled to get the best fit in a width by height rectangle while preserving the original aspect ratio
因此,在您的代码中:
mapPreview.setFitHeight(40);
mapPreview.setPreserveRatio(true);
或者您甚至可以使用 its constructor:
在加载时调整Image
的大小
mapPreview = new ImageView(
new Image(getClass().getResource("/files/images/maps/" + chosenMapName + ".gif").toURI().toURL().toString(),
40, 200, true, true));
但在你的情况下,因为你想用它作为预览,以原始大小加载它并存储图像参考更合理,然后你可以在显示原始图像时使用它以避免重新加载图像。