【问题标题】:Animate splitpane divider动画拆分窗格分隔线
【发布时间】:2017-11-05 14:30:17
【问题描述】:

我有一个水平拆分窗格,我想在按钮单击时更改分隔位置,以便创建某种“幻灯片”动画。

divider 将从 0 开始(完全向左),在我点击时它会打开到 0.2,当我再次点击时,它会回到 0;

现在我做到了,我只是使用

spane.setdividerPositions(0.2); 

并且分隔线位置发生变化,但我无法缓慢地做到这一点,我真的很喜欢改变分隔线位置时的滑动感觉。

谁能帮助我?我在 google 上找到的所有示例都显示了一些 DoubleTransition,但在 java 8 中不再存在,至少我没有导入。

【问题讨论】:

  • 我认为从来没有DoubleTransition

标签: javafx slide splitpane


【解决方案1】:

您可以致电getDividers().get(0) 获取第一个分隔符。它有一个positionProperty(),您可以使用时间轴制作动画:

import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.SplitPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.util.Duration;

public class AnimatedSplitPane extends Application {

    @Override
    public void start(Stage primaryStage) {
        SplitPane splitPane = new SplitPane(new Pane(), new Pane());
        splitPane.setDividerPositions(0);

        BooleanProperty collapsed = new SimpleBooleanProperty();
        collapsed.bind(splitPane.getDividers().get(0).positionProperty().isEqualTo(0, 0.01));

        Button button = new Button();
        button.textProperty().bind(Bindings.when(collapsed).then("Expand").otherwise("Collapse"));

        button.setOnAction(e -> {
            double target = collapsed.get() ? 0.2 : 0.0 ;
            KeyValue keyValue = new KeyValue(splitPane.getDividers().get(0).positionProperty(), target);
            Timeline timeline = new Timeline(new KeyFrame(Duration.millis(500), keyValue));
            timeline.play();
        });

        HBox controls = new HBox(button);
        controls.setAlignment(Pos.CENTER);
        controls.setPadding(new Insets(5));
        BorderPane root = new BorderPane(splitPane);
        root.setBottom(controls);
        Scene scene = new Scene(root, 600, 600);
        primaryStage.setScene(scene);
        primaryStage.show();

    }

    public static void main(String[] args) {
        launch(args);
    }
}

【讨论】:

  • 詹姆斯一如既往的好主意!看看拆分窗格内容的动画和调整大小是否会导致内容闪烁会很有趣。
猜你喜欢
  • 2014-11-16
  • 1970-01-01
  • 1970-01-01
  • 2013-06-07
  • 1970-01-01
  • 2012-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多