2013-08-22 66 views
0

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

回答

0

您只能修改和查询JavaFX应用程序线程上的活动场景图。所有代码都可以与场景图形一起使用,因此它们都必须在JavaFX应用程序线程上运行。如果JavaFX应用程序线程中的所有内容都已经运行,那么在您的代码中没有任何原因会导致并发性相关的结构。

如果使用阻塞调用像latch.await(),你将阻止JavaFX应用程序的线程,这将阻止任何渲染,布局或动画步骤来运行。 CountdownLatch不应在此上下文中使用,应从代码中删除。

调用Platform.runLater是不必要的,因为它的目的是在JavaFX应用程序线程上运行代码,并且您已经在JavaFX应用程序线程中。 Platform.runLater不应在此上下文中使用,应从代码中删除。