自定义平移处理程序Javafx

时间:2016-10-20 10:16:10

标签: java javafx drag scrollpane panning

你好,因为scrollpane的内置平移工具问题(运行缓​​慢,块状过渡而不是流畅)我试图创建一个自定义平移处理程序。以下是我的尝试,但if语句无法正常工作。

    mapScroll.addEventFilter(MouseEvent.MOUSE_PRESSED, e -> {
        startX = e.getX();
        startY = e.getY();
    });
    mapScroll.addEventFilter(MouseEvent.MOUSE_DRAGGED, e -> {
        endX = e.getX();
        endY = e.getY();
        Bounds viewBounds = mapScroll.getViewportBounds();
        Bounds mapBounds = mapScroll.getBoundsInParent();
        double xMin = viewBounds.getMinX();
        double xMax = viewBounds.getMaxX();
        double yMin = viewBounds.getMinY();
        double yMax = viewBounds.getMaxY();
        double startEndX = endX - startX;
        double startEndY = endY - startY;
        if(mapBounds.contains(xMin + startEndX, yMin + startEndY) && mapBounds.contains(xMin + startEndX, yMax + startEndY) && mapBounds.contains(xMax + startEndX, yMin + startEndY) && mapBounds.contains(xMax + startEndX, yMax + startEndY))
        {
            double fullWidth = mapScroll.getWidth();
            double fullHeight = mapScroll.getHeight();
            double hChange = startEndX/fullWidth;
            double vChange = startEndY/fullHeight;
            mapScroll.setHvalue(mapScroll.getHvalue() + hChange);
            mapScroll.setVvalue(mapScroll.getVvalue() + vChange);
        }
        endX = startX;
        endY = startY;
    });

1 个答案:

答案 0 :(得分:1)

您永远不会在计算中使用内容的宽度。这意味着无论内容的大小如何,您都必须将鼠标移动相同的距离才能从一侧滚动到另一侧。内容是否比视口大10个像素,或者内容是视口大小的10倍,这对您的代码没有任何影响。此外请注意

endX = startX;
endY = startY;

绝对无用,因为无论如何都会在事件过滤器的每次调用开始时覆盖值。你可能想要反过来做任务。

此外,您可以将滚动位置移动到与鼠标移动相同的方向。但是,如果您希望鼠标位置相对于内容保持相同,则应该向相反方向移动滚动位置。

这是一个例子(为简单起见,删除了边界测试):

private double startX;
private double startY;

@Override
public void start(Stage primaryStage) {
    ImageView image = new ImageView("https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/687px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg");
    ScrollPane mapScroll = new ScrollPane(image);
    Scene scene = new Scene(mapScroll, 400, 400);
    mapScroll.addEventFilter(MouseEvent.MOUSE_PRESSED, e -> {
        startX = e.getX();
        startY = e.getY();
    });
    mapScroll.addEventFilter(MouseEvent.MOUSE_DRAGGED, e -> {
        double endX = e.getX();
        double endY = e.getY();
        Bounds viewBounds = mapScroll.getViewportBounds();

        double startEndX = startX - endX;
        double startEndY = startY - endY;

        Bounds contentBounds = mapScroll.getContent().getLayoutBounds();

        double hChange = startEndX / (contentBounds.getWidth() - viewBounds.getWidth());
        double vChange = startEndY / (contentBounds.getHeight() - viewBounds.getHeight());
        mapScroll.setHvalue(mapScroll.getHvalue() + hChange);
        mapScroll.setVvalue(mapScroll.getVvalue() + vChange);

        startX = endX;
        startY = endY;
    });

    primaryStage.setScene(scene);
    primaryStage.show();
}