作为图像处理应用程序的一部分,我需要创建具有缩放、滚动和 vector 叠加功能的简单查看器模块。图像相当大:~40000x20000,这使得 ImageView 上的操作变慢、缓冲等。在 JavaFX 中处理巨大图像时,改善用户体验的最佳选项是什么?
最佳答案
我有一个 8k x 4k 图像,通过缓存我放置的滚动 Pane 并使用此代码进行缩放,可以很好地缩放。滚动 Pane 和大图像的唯一问题是平移非常糟糕且缓慢,我正在尝试在当前的应用程序中找到解决方案。
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()));
}
});
关于java - 如何在JavaFX中有效地滚动和缩放大图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40190029/