我在盯着我的代碼,我很困擾一個問題: 我想淡出過渡,我想阻止當前線程,while淡入淡出過渡正在運行。 所以,我的嘗試是創造的CountDownLatch,它阻止該線程,直到transition.setOnFinished()被調用,在這裏我做一個latch.countdown()。簡而言之:我想確保轉換總是可見的。JavaFX FadeTransition.onSetOnFinished with CountdownLatch無法正常工作
看起來很直截了當,但... setOnFinished()不會被調用,因爲上面提到的當前線程被倒計數鎖存器阻塞。
我該如何解決這個問題? Thx提前。
private void initView() {
Rectangle rect = new Rectangle();
rect.widthProperty().bind(widthProperty());
rect.heightProperty().bind(heightProperty());
rect.setFill(Color.BLACK);
rect.setOpacity(0.8f);
getChildren().add(rect);
MyUiAnimation animator = new MyUiAnimation();
fadeInTransition = animator.getShortFadeInFor(this);
fadeOutTransition = animator.getShortFadeOutFor(this);
fadeOutTransition.setOnFinished(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent arg0) {
Platform.runLater(new Runnable() {
@Override
public void run() {
latch.countDown();
setVisible(false);
}
});
}
});
}
public void hide() {
fadeInTransition.stop();
if (isVisible()) {
latch = new CountDownLatch(1);
fadeOutTransition.playFromStart();
try {
latch.await();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}