JavaFX中心视图的光标位置

时间:2015-07-07 16:03:49

标签: java image javafx

我创建了第一个显示图像的JavaFX应用程序。我希望它能够在鼠标按下时将图像缩放到全尺寸(以光标位置为中心)并在鼠标上重新调整图像。

一切正常,但我不知道如何以光标位置为中心。我的缩放方法目前看起来像这样:

List<List<Integer>>

我的想法是用translate设置偏移量。我不确定翻译是否是正确的方法。如果正确的方法如何计算正确的值? (centerx-cursorx)错了!

我在这里上传了代码:https://github.com/dermoritz/FastImageViewer(现在正在使用 - 请参阅我的回答)

2 个答案:

答案 0 :(得分:0)

我没有测试它,但我认为顺序错了。在设置拟合尺寸之前计算中心。通过设置适合大小,您的图像视图将更改其首选大小。因此,中心点不再匹配。

答案 1 :(得分:0)

我找到了一个解决方案,但我仍然不确定这是否是一个好方法:

private void zoom100(double x, double y) {
    double oldHeight = imageView.getBoundsInLocal().getHeight();
    double oldWidth = imageView.getBoundsInLocal().getWidth();

    boolean heightLarger = oldHeight>oldWidth;
    imageView.setFitHeight(-1);
    imageView.setFitWidth(-1);
    //calculate scale factor
    double scale=1;
    if(heightLarger){
        scale = imageView.getBoundsInLocal().getHeight()/oldHeight;
    }else {
        scale = imageView.getBoundsInLocal().getWidth()/oldWidth;
    }

    double centery = root.getLayoutBounds().getHeight() / 2;
    double centerx = root.getLayoutBounds().getWidth() / 2;

    double xOffset = scale * (centerx - x);
    double yOffset = scale *(centery - y);
    imageView.setTranslateX(xOffset);
    imageView.setTranslateY(yOffset);

}

图片必须在“root”中居中。我认为应该有更直接的东西 - 只使用imageview的属性?!

同时,该应用使用此代码:https://github.com/dermoritz/FastImageViewer

相关问题