Slide in animation on app start
The new Google Play application has nice start up animation. Let’s find out how we can achieve this.
Idea
Let’s start from basics. What is the best moment to start a animation?
We need to make sure that:
- we don’t start animation too early
- we don’t start animation too late
- view hierarchy is measured (size of all views are already calculated)
The onWindowFocusChanged method meets all the requirements – it is called when the current Window of the activity gains or loses focus. This is the best indicator of whether this Activity is visible to the user.
onWindowFocusChanged might be called several times so we need to make sure that animation occur only once.
Now the mysterious startSlideInAnimation function. On the video we can discover that views are being animated indepently.
So we need to get reference to every view in our activity.
android.R.id.content is the universal identificator that points to window root ViewGroup. Then we get root ViewGroup of our Activity. And iterate over child views invoking animateSingleView.
Finally we get to essential animation. We move all views to bottom of screen and set transparency. That we set animation — setting translationY to 0 will move view to previous position. The animation is started with a little delay to make sure user will see it from very beginning.
Duration of animation depends on position in view hierarchy. This creates really nice looking effects.