How to effectively scroll and zoom big images in JavaFX?

时光怂恿深爱的人放手 提交于 2020-01-03 04:28:12

问题


As a part of image processing application I need to create simple viewer module with zooming, scrolling and vector overlay features. Images are quite big: ~40000x20000 which makes operations on ImageView slow, buffering, etc. What are the best options to improve user experience when working with huge images in JavaFX?


回答1:


I have a 8k by 4k image that scales fine by caching the scroll pane I have have placed it in and using this code for zooming. Only problem with scroll pane and large images is that panning is horrible and slow which I'm trying to find a solution for in my current application.

final double SCALE_DELTA = 1.175;
private double SCALE_TOTAL = 1;

.

    //Adds functionality to scrolling
    mapScroll.addEventFilter(ScrollEvent.ANY, e ->{
        //Consumes the event
        e.consume();
        if(e.getDeltaY() == 0)
        {
            return;
        }
        double scaleFactor =
                  (e.getDeltaY() > 0)
                    ? SCALE_DELTA
                    : 1/SCALE_DELTA;
        //Ensures that you do not exceed the limits of the map
        if(scaleFactor * SCALE_TOTAL >= 1)
        {
            Bounds viewPort = mapScroll.getViewportBounds();
            Bounds contentSize = mapGrid.getBoundsInParent();

            double centerPosX = (contentSize.getWidth() - viewPort.getWidth()) * mapScroll.getHvalue() + viewPort.getWidth() / 2;
            double centerPosY = (contentSize.getHeight() - viewPort.getHeight()) * mapScroll.getVvalue() + viewPort.getHeight() / 2;

            mapGrid.setScaleX(mapGrid.getScaleX() * scaleFactor);
            mapGrid.setScaleY(mapGrid.getScaleY() * scaleFactor);
            SCALE_TOTAL *= scaleFactor;

            double newCenterX = centerPosX * scaleFactor;
            double newCenterY = centerPosY * scaleFactor;

            mapScroll.setHvalue((newCenterX - viewPort.getWidth()/2) / (contentSize.getWidth() * scaleFactor - viewPort.getWidth()));
            mapScroll.setVvalue((newCenterY - viewPort.getHeight()/2) / (contentSize.getHeight() * scaleFactor  -viewPort.getHeight()));
        }
    });


来源:https://stackoverflow.com/questions/40190029/how-to-effectively-scroll-and-zoom-big-images-in-javafx

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!