title: Understanding getViewTreeObserver
body: <p>First of all, Let me aplogize, but I didnt know where else to ask.</p>
  
  <p>I want to understand the following code that can be <a href="https://raw.githubusercontent.com/rahulmmohan/SpringAnimation/master/app/src/main/java/info/overrideandroid/springanimation/MainActivity.java" rel="nofollow noreferrer">found here.</a></p>
  
  <p>I understand all of it apart from the <code>getViewTreeObserver</code> bit:</p>
  
  <pre><code>movingView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
      @Override
      public void onGlobalLayout() {
          yAnimation = createSpringAnimation(movingView,SpringAnimation.Y, movingView.getY(), STIFFNESS, DAMPING_RATIO);
          xAnimation = createSpringAnimation(movingView,SpringAnimation.X, movingView.getX(), STIFFNESS, DAMPING_RATIO);
          movingView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
      }
  });
  </code></pre>
  
  <p>Why do we need this bit? The last line, removes the <code>OnGlobalLayoutListener</code> from the <code>movingView</code>, so I dont understand why we set it and then remove it.  If I take the <code>yAnimation</code> and <code>xAmination</code> lines out of this listener, and just run them on their own, the code still works fine however it doesn't return to its original X and Y.  But I dont understand why the above code allows the <code>createSpringAnimation</code>function to receive the <em>correct</em> X and Y.</p>
  
  <p>Please, can someone help me understand?</p>