问题描述
我有一个水平的拆分窗格,我想单击按钮,更改分隔线的位置,以便创建某种幻灯片"动画.
I have a horizontal split pane, and i would like to on button click, change divider position, so that i create sort of "slide" animation.
除法器将从0开始(向左完整),在我单击时它将一直打开到0.2,当我再次单击时,它将回到0;
divider would start at 0 (complete left) and on my click it would open till 0.2, when i click again, it would go back to 0;
现在我做到了,我只用
spane.setdividerPositions(0.2);
并且分隔线的位置发生变化,但是我无法缓慢地执行此操作,我真的很希望在更改分隔线位置时的滑动感觉.
and divider position changes, but i cant manage to do this slowly i would really like that slide feeling when changing divider position.
有人可以帮助我吗?我在Google上找到的所有示例都显示了一些DoubleTransition,但是在Java 8中已经不存在了,至少我没有为此导入.
Could anyone help me ? all examples i found on google, show some DoubleTransition, but that does not exist anymore in java 8, at least i dont have import for that.
推荐答案
您可以调用getDividers().get(0)
获取第一个除法器.它具有一个positionProperty()
,您可以使用时间轴对其进行动画处理:
You can call getDividers().get(0)
to get the first divider. It has a positionProperty()
that you can animate using a timeline:
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);
}
}
这篇关于动画分割窗格分隔线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!