我创建了第一个显示图像的 JavaFX 应用程序。我希望它在鼠标按下时将图像缩放到完整尺寸(以光标位置为中心),并在鼠标松开时重新调整图像。
一切正常,但我不知道如何将光标位置居中。我的缩放方法目前看起来像这样:
private void zoom100(double cursorx, double cursory){
double centery = imageView.getLayoutBounds().getHeight()/2;
double centerx = imageView.getLayoutBounds().getWidth()/2;
imageView.setFitHeight(-1); //zooms height to 100%
imageView.setFitWidth(-1); //zooms width to 100%
imageView.setTranslateX(centerx-cursorx); //moves x
imageView.setTranslateY(centery-cursory); //moves y
}
我的想法是通过翻译设置偏移量。我不确定翻译是否是正确的方法。如果这是正确的方法如何计算正确的值? (centerx-cursorx) 是错误的!
我在这里上传了代码:https://github.com/dermoritz/FastImageViewer (它现在正在工作 - 请参阅我的答案)
最佳答案
我没有测试过,但我认为顺序是错误的。在设置适合尺寸之前计算中心。通过设置适合尺寸,您的 ImageView 将更改其首选尺寸。因此中心点将不再匹配。
关于光标位置上的 JavaFX 中心 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31273799/