First of all, Let me aplogize, but I didnt know where else to ask.
I want to understand the following code that can be found here.
I understand all of it apart from the getViewTreeObserver
bit:
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);
}
});
Why do we need this bit? The last line, removes the OnGlobalLayoutListener
from the movingView
, so I dont understand why we set it and then remove it. If I take the yAnimation
and xAmination
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 createSpringAnimation
function to receive the correct X and Y.
Please, can someone help me understand?