ViewPagerAnimator


Source link: https://github.com/StylingAndroid/ViewPagerAnimator

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.

Resources

IntelliJ Idea, Android Studio plugin for JSON to POJO conversion.

Add a header and/or footer to your RecyclerView - the easy way.

A library project offering an interactive info window for Google maps on Android.

Java and Android class communication library.

A simple Android wrapper for interfacing with the new Camera2 API for video capture.

Gradle Plugin for Simple AspectJ Weaving.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes