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

Spinner2 is a re-implementation of the default Android's spinner, with a simple way for control selection behavior.

A Page Indicator Library which is realized in a different way.

A powerful android view looper library with animation.

Material letter icon with circle background. Replicates android L contacts icon view.

Android library to inform EU users of the use of cookies. The alert can be shown via dialog or an overlay view.

Simple dot animation to show progress.

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