Resizing images to fit the parent node

前端 未结 6 1726
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-28 14:10

How do I get an image in an ImageView to automatically resize such that it always fits the parent node?

Here is a small code example:

@Override
publi         


        
6条回答
  •  忘掉有多难
    2020-11-28 14:36

    Fill the parent whit aspect ration, this fix the problem whit when parent height and width are not in proper ration like the image.

    Image image = new Image(getClass().getResource(%path%).toString());
    double ratio = image.getWidth() / image.getHeight();
    double width = stage.getScene().getWidth();
    
    ImageView imageView.setImage(image);
    imageView.setFitWidth(width);
    imageView.setFitHeight(width/ratio);
    imageView.setPreserveRatio(true);
    

提交回复
热议问题