2017-06-04 36 views
1

我有一個水平分割窗格,我想按鈕單擊,更改分隔符位置,以便創建一種「幻燈片」動畫。Animate splitpane divider

分頻器將從0開始(完成左),並在我點擊它將打開到0.2,當我再次點擊,它會回到0;

現在我achived這個,我只是使用

spane.setdividerPositions(0.2); 

和分隔條位置的改變,但我不能設法做到這一點慢慢地我會很喜歡那個滑的感覺改變時,分隔條位置。

任何人都可以幫助我嗎?我在google上找到的所有示例都顯示了一些DoubleTransition,但在java 8中不再存在,至少我沒有爲此導入。

+0

我不認爲曾經有過'DoubleTransition'。 –

回答

2

您可以撥打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); 
    } 
} 
+0

你是一個黃金。非常感謝! – ImRaphael

+0

詹姆斯一如既往的好主意! 查看動畫和拆分窗格內容的大小是否會導致內容閃爍很有意思。 – Birdasaur