Animation / ViewPager / ViewPagerAnimator

ViewPagerAnimator – The Internals

Earlier this month I released a new lightweight, yet extremely powerful ViewPager animation library named, unsurprisingly ViewPagerAnimator. ViewPagerAnimator is designed to animate arbitrary values as the user navigates between pages within a ViewPager, and will precisely follow the motion of h[is|er] finger. There is nothing groundbreaking in terms of the techniques I’m using to do this – I previously did something very similar in Prism. However, there are some very nice subtleties in terms of the API design.

In this final article about ViewPagerAnimator we’ll take a look inside and explain how it actually works. The main thing that enables us to easily hook in to the operation of the ViewPager itself is by implementing the ViewPager.OnPageChangeListener interface which will provide us with callback as the user swipes or switches between view. Before we dive in to that let’s first look at those strongly-typed factory methods:

Although there looks to be a lot of code here, it’s actually much simpler that it first appears. There are 6 factory methods, but only 3 of them are designed for external consumption, the others are package private and allow us to inject mocks much easier in our tests. So each of these will create a suitable TypeEvaluator and instantiate a ViewPagerAnimator.

There are the two constructors listed there as well. These can both be called externally for creating a custom type animators, as we saw previously.

Finally there are a couple of lifecycle methods which will attach and detach from a ViewPager, and finally a method to allow the override of an Interpolator.

The main workhorse of ViewPagerAnimator is the implementation of ViewPager.OnPageChangeListener:

The onPageScrolled() callback will be called for every tiny movement during the scroll and is where we actually calculate and apply the animated value to the Property. However, there are a number of distinct things going on. The first conditional is an initialiser to ensure that we set up the initial page state as the ViewPager is initialised. We need to handle this here because onPageScrolled() will be called when we first attach to the ViewPager and as we are responsible for setting the Property, we must set an initial value.

The next conditional checks whether the an in-progress animation should be ended because we’ve reached a stable page boundary.

Next we determine whether we should begin an animation.

Finally if, after all that, an animation is currently in-progress then we calculate a value based upon the interpolated fractional distance between pages, and apply that to the Property.

The remaining methods are a little more straightforward. Firstly isAnimating() will return whether an animation is currently in progress; endAnimation() will set the animation to a quiescent state; and beginAnimation() will, as it’s name suggest start an animation.There are two branches here which cover whether the pager transition is from left-to-right or right-to-left. Both cases initialise the start andEnd values by performing look-ups from the Provider. It is these values that are used in onPageScrolled() to work out the fractional value which gets applied to the Property.

The final two methods of ViewPager.OnPageChangeListener are onPageScrollStateChanged() which we ignore, and onPageSelected() which is called if the user selects a page by tapping on a tab in the TabBar. In this case we just set the property based on the value of the selected position – we don’t need to calculate a fractional position because we know that we are now settled on a page stable page position.

There’s nothing really that clever going on here but, as mentioned previously, it is the single method Provider & Property interfaces which facilitate the use of Java 8 method references which turn the API in to an extremely concise, fluent, and powerful one when we use Java 8.

ViewPagerAnimator is available now on jcenter compile 'com.stylingandroid.viewpageranimator:viewpageranimator:1.0.1' and the source is available here.

© 2017, Mark Allison. All rights reserved.

CC BY-NC-SA 4.0 ViewPagerAnimator – The Internals by Styling Android is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Permissions beyond the scope of this license may be available at

Leave a Reply

Your email address will not be published. Required fields are marked *