我在盯着我的代码,我很困扰一个问题: 我想淡出过渡,我想阻止当前线程,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();
}
}
}