如何在 javafx 中调整 imageview 图像的大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27894945/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 05:14:36  来源:igfitidea点击:

How do I resize an imageview image in javafx?

javaimagejavafxresizeimageview

提问by Jeremy

I need to resize an image to specific dimensions, 100 by 100 pixels for example, in JavaFX.

我需要在 JavaFX 中将图像调整为特定尺寸,例如 100 x 100 像素。

How can I achieve that? Could the Image or the ImageView class be used for this purpose?

我怎样才能做到这一点?Image 或 ImageView 类可以用于此目的吗?

采纳答案by James_D

Yes, using an ImageView. Just call

是的,使用ImageView. 打电话就行

ImageView imageView = new ImageView("...");
imageView.setFitHeight(100);
imageView.setFitWidth(100);

By default, it will not preserve the width:heightratio: you can make it do so with

默认情况下,它不会保留width:height比例:您可以使用

imageView.setPreserveRatio(true);

Alternately you can resize the Imagedirectly on loading:

或者,您可以在加载时直接调整图像大小:

Image image = new Image("my/res/flower.png", 100, 100, false, false);

Resizing the image on loading is useful for things like thumbnails of larger images as the memory required is lower than storing the larger image data representation in memory.

在加载时调整图像大小对于诸如较大图像的缩略图之类的事情很有用,因为所需的内存低于将较大的图像数据表示存储在内存中。