ViewPagerAnimator

Additional

Language
Java
Version
N/A
Created
Apr 29, 2017
Updated
Oct 28, 2017 (Retired)
Owner
Mark Allison (StylingAndroid)
Contributors
Maciej Górski (mg6maciej)
Mark Allison (StylingAndroid)
2
Activity
Badge
Generate
Download
Source code

Advertisement

ViewPagerAnimator

ViewPagerAnimator is a new lightweight, yet powerful ViewPager animation library for Android. it 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. Although the library itself may be of use to some, the main purpose of publishing this library is to demonstrate some wonderful API subtleties which really come to the fore when using Java 8 extensions which are coming our way soon. Sample projects for both Java 7 and Java 8 are provided.

More comprehensive documentation is available on the Styling Android blog

The library is published to jcenter and can be included in to a project just add the following to the dependencies section:

compile 'com.stylingandroid.viewpageranimator:viewpageranimator:1.0.1'

Java 7

To use ViewPagerAnimator in Java 7 code it is necessary to implement two interfaces which act as facades to arbitrary objects of your chosing: The Provider interface will provide an arbitrary value for each pager position within the ViewPager (this would typicaly be a value controlled by the PagerAdapter); the Property interface will control the value we wish to animate (this would typically be a value which controlled the appearance of a View).

The only thing that we need to do is ensure that the value types for both the Provider and Property match. As both are Generic interfaces, then we need to match the Generic types. For example Provider<Integer> would need to be matched with Property<Integer>.

Provider

We can create a Provider facade to our custom PagerAdapter which has a method named getColour(int position) which will return a distinct colour value for each page position within the ViewPager:

final ViewPager viewPager = ...;
final ColourPagerAdapter pagerAdapter = ...;
viewPager.setAdapter(pagerAdapter);
Provider<Integer> provider = new Provider<Integer>() {
    @Override
    public Integer get(int position) {
        return pagerAdapter.getColour(position);
    }
};

So now we have a Provider instance. Any consumer of this requires no knowledge of ColourPagerAdapter put can retrieve a value from it using Provider#get(int position).

Property

We can create a Property facade to the ViewPager itself to change the background colour:

Property<Integer> property = new Property<Integer>() {
    @Override
    public void set(Integer value) {
        viewPager.setBackgroundColor(value);
    }
};

Once again the Property facade allows a component to set the background colour of the ViewPager instance without any knowledge of what a ViewPager is. This becomes particularly powerful if we are changing the appearance of any arbitrary UI component such as the system bar colour.

ViewPagerAnimator

Now that we have a Provider and a Property we can create the ViewPagerAnimator:

ViewPagerAnimator animator = ViewPagerAnimator.ofArgb(viewPager, provider, property);

There are three factory methods which enable the construction of ViewPagerAnimator instances whic will animate ARGB colour values (as in this example), Integer values, or Float values. It is also possible to animate any object type you like by calling the constructor directly, but you will need to provide a TypeEvaluator which will be responsible for calculating intermediate values.

The compiler will give an error if we mis-match the generic types of the Provider and a Property implementation (i.e. if we were to mix a Property<Float> with a Provider<Integer>).

Java 8

Although we are not required to do that much work in Java 7, things become much terser and more fluent if we use Java 8 (this is currently in the 2.4 alpha 6 and later Android gradle plugin). The exact same functionality as Java7 can be implemented in this way:

final ViewPager viewPager = ...;
final ColourPagerAdapter pagerAdapter = ...;
viewPager.setAdapter(pagerAdapter);
ViewPagerAnimator animator = ViewPagerAnimator.ofArgb(viewPager, pagerAdapter::getColour, viewPager::setBackgroundColor);

Here we can use Java 8 method references to save us from having to actually create the Provider and Property instances as long as we specify methods which match the method signature of the single method in each facade interface.

The facade pattern is really useful for decoupling components, but when we then add it Java 8 method references the APIs become really simple and extremely fluent. That is the really interesting thing here, IMO.