如何提高ScrollPane JavaFX的滚动速度

时间:2019-06-24 15:38:32

标签: java javafx javafx-8

嗨,有什么方法可以提高滚动速度。我找到了一些解决方案,但没有一个适合我。 那就是我尝试过的:

@FXML
private ScrollPane scrollPane;

@FXML
public void initialize() {
    Platform.runLater(() -> setFasterScroller(scrollPane));
}

private static void setFasterScroller(ScrollPane scrollPane) {
    ScrollBar verticalScrollbar = (ScrollBar) scrollPane.lookup(".scroll-bar:vertical");
    double defaultUnitIncrement = verticalScrollbar.getUnitIncrement();
    verticalScrollbar.setUnitIncrement(defaultUnitIncrement * 3);
}

或者使用CSS:

.scroll-pane .scroll-bar:vertical {
    -fx-unit-increment: 10 ;
    -fx-block-increment: 50 ;
}

.scroll-pane .scroll-bar:horizontal {
    -fx-unit-increment: 5 ;
    -fx-block-increment: 20 ;
}

还有其他方法吗? 编辑: 上面的解决方案都没有用,我尝试使用疯狂的大数字,但滚动速度仍然相同

1 个答案:

答案 0 :(得分:1)

应用程序应“更快”地呈现滚动窗格内容,以提高滚动速度。 您可以实现它减少要渲染的对象数量或/和减少“重新绘制”方法调用的数量(JFX中的layoutChildren)。据我了解的代码片段,您将要减少重新绘制的次数,因为更改增量参数可以使内容重新绘制的次数更少。

  • blockIncrement(如果单击了栏的轨道)
  • unitIncrement(如果单击了上,下,右,左按钮)

单击滚动窗格按钮时,将调用方法ScrollPane.layoutChildren()。 它使ScrollBar重置增量(ScrollPaneSkin.updateVerticalSB(), ScrollPaneSkin.updateHorizontalSB())。 因此,您需要存储以前的增量值并过滤“开箱即用”的滚动事件。

来源:

public class UnitIncrementScrollPaneSkin extends ScrollPaneSkin {

    private double prevVerticalBlockIncrement;
    private double prevHorizontalBlockIncrement;
    private double prevVerticalUnitIncrement;
    private double prevHorizontalUnitIncrement;

    public UnitIncrementScrollPaneSkin(final ScrollPane scrollPane) {
        super(scrollPane);
        filterScrollEvents();
    }

    @Override
    protected void layoutChildren(final double x, final double y, final double w, final double h) {
        prevVerticalUnitIncrement = getVerticalScrollBar().getUnitIncrement();
        prevHorizontalUnitIncrement = getHorizontalScrollBar().getUnitIncrement();
        prevVerticalBlockIncrement = getVerticalScrollBar().getBlockIncrement();
        prevHorizontalBlockIncrement = getHorizontalScrollBar().getBlockIncrement();
        super.layoutChildren(x, y, w, h);
        getVerticalScrollBar().setUnitIncrement(prevVerticalUnitIncrement);
        getHorizontalScrollBar().setUnitIncrement(prevHorizontalUnitIncrement);
        getVerticalScrollBar().setBlockIncrement(prevVerticalBlockIncrement);
        getHorizontalScrollBar().setBlockIncrement(prevHorizontalBlockIncrement);
    }

    private void filterScrollEvents() {
        getSkinnable().addEventFilter(ScrollEvent.SCROLL, event -> {

            if (event.getDeltaX() < 0) {
                getHorizontalScrollBar().increment();
            } else if (event.getDeltaX() > 0) {
                getHorizontalScrollBar().decrement();
            }

            if (event.getDeltaY() < 0) {
                getVerticalScrollBar().increment();
            } else if (event.getDeltaY() > 0) {
                getVerticalScrollBar().decrement();
            }

            event.consume();
        });
    }

    public ScrollBar getVerticalScrollBar() {
        return vsb;
    }

    public ScrollBar getHorizontalScrollBar() {
        return hsb;
    }
}

用法:

@FXML
public void initialize() {
    final double initialInc = 0.2;
    UnitIncrementScrollPaneSkin skin = new UnitIncrementScrollPaneSkin(scrollPane);
    skin.getVerticalScrollBar().setUnitIncrement(initialInc);
    skin.getVerticalScrollBar().setBlockIncrement(initialInc);
    skin.getHorizontalScrollBar().setUnitIncrement(initialInc);
    skin.getHorizontalScrollBar().setBlockIncrement(initialInc);
    scrollPane.setSkin(skin);
}

测试应用:

<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="226.0" prefWidth="215.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.stackoverflow.app.scrollpane.SpeedUpScrollPaneController">
   <children>
      <ScrollPane fx:id="scrollPane" fitToHeight="true" fitToWidth="true" hbarPolicy="ALWAYS" prefHeight="200.0" prefWidth="200.0" vbarPolicy="ALWAYS">
         <content>
            <GridPane>
              <rowConstraints>
                <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
              </rowConstraints>
            </GridPane>
         </content>
      </ScrollPane>
      <HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0">
         <children>
            <Label text="V:" />
            <Spinner fx:id="spinnerVertical" prefHeight="25.0" prefWidth="85.0">
                 <valueFactory>
                    <SpinnerValueFactory.DoubleSpinnerValueFactory amountToStepBy="0.1" max="1.0" min="0" />
                 </valueFactory>
            </Spinner>
         </children>
      </HBox>
      <HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0">
         <children>
            <Label text="H:" />
            <Spinner fx:id="spinnerHorizontal" prefHeight="25.0" prefWidth="85.0">
                <valueFactory>
                    <SpinnerValueFactory.DoubleSpinnerValueFactory amountToStepBy="0.1" max="1.0" min="0" />
                 </valueFactory>
            </Spinner>
         </children>
      </HBox>
      <ListView fx:id="listViewLog" prefHeight="200.0" prefWidth="200.0" />
   </children>
</VBox>
public class SpeedUpScrollPaneController {

    @FXML
    private ListView<String> listViewLog;
    @FXML
    private Spinner<Double> spinnerVertical;
    @FXML
    private Spinner<Double> spinnerHorizontal;
    @FXML
    private ScrollPane scrollPane;

    @FXML
    public void initialize() {
        mockScrollPane();
        initScrollPane();
    }

    private void mockScrollPane() {
        for (int i = 0; i < 100; i++) {
            for (int j = 0; j < 100; j++) {
                final Label label = new Label(i + "," + j);
                label.setMinWidth(60);
                label.setMinHeight(30);
                ((GridPane) scrollPane.getContent()).add(label, i, j);
            }
        }
    }

    private void initScrollPane() {
        final UnitIncrementScrollPaneSkin skin = new UnitIncrementScrollPaneSkin(scrollPane);
        scrollPane.setSkin(skin);
        initIncrement(0.2, spinnerHorizontal, skin.getHorizontalScrollBar());
        initIncrement(0.2, spinnerVertical, skin.getVerticalScrollBar());
    }

    private void initIncrement(final double increment, final Spinner<Double> spinner, final ScrollBar scrollBar) {
        scrollBar.unitIncrementProperty().addListener((source, prev, curr) -> {
            final String vLog = String.format("%tr: %s unitIncrement=%f", Calendar.getInstance(),
                    scrollBar.getOrientation(), curr);
            listViewLog.getItems().add(vLog);
            listViewLog.scrollTo(vLog);
        });
        scrollBar.blockIncrementProperty().addListener((source, prev, curr) -> {
            final String vLog = String.format("%tr: %s blockIncrement=%f", Calendar.getInstance(),
                    scrollBar.getOrientation(), curr);
            listViewLog.getItems().add(vLog);
            listViewLog.scrollTo(vLog);
        });
        spinner.valueProperty().addListener((source, prev, curr) -> {
            scrollBar.setUnitIncrement(curr);
            scrollBar.setBlockIncrement(curr);

        });

        Platform.runLater(() -> {
            scrollBar.setUnitIncrement(increment);
            scrollBar.setBlockIncrement(increment);
            spinner.getValueFactory().setValue(scrollBar.getUnitIncrement());
        });
    }
}
  • 0.0->不滚动
  • 0.1->滚动10%
  • 0.5->滚动50%
  • 1.0->滚动100%
  • Screencast

PS。 如果要滚动的对象很多,可以尝试以下方法:

  • 创建表示滚动窗格内容的图像位图
  • 在开始滚动时显示图像位图
  • 在X,Y处移动图像(基于ScrollEvent.getDeltaX()ScrollEvent.getDeltaY()
  • 隐藏图像,在滚动完成时显示具有新坐标的滚动窗格节点