中心裁剪图像JavaFX

时间:2017-04-27 21:53:39

标签: java image javafx scaletype

如何使用JavaFX完成Android ImageView myImage = new ImageView("https://example.com/image/my_image.png"); //maybe //myImage.scaleType = "centerCrop";

我有我的图片视图:

{{1}}

1 个答案:

答案 0 :(得分:2)

感谢@TravisF的帖子,我已经实现了最后一个解决方案,使图像始终具有相同的高度,widht和位置(中心)。

double newMeasure = (userImage.getImage().getWidth() < userImage.getImage().getHeight()) ? userImage.getImage().getWidth() : userImage.getImage().getHeight();
double x = (userImage.getImage().getWidth() - newMeasure) / 2;
double y = (userImage.getImage().getHeight() - newMeasure) / 2;

Rectangle2D rect = new Rectangle2D(x, y, newMeasure, newMeasure);
userImage.setViewport(rect);
userImage.setFitWidth(150);
userImage.setFitHeight(150);
userImage.setSmooth(true);