• Stars
    star
    335
  • Rank 125,426 (Top 3 %)
  • Language
    Java
  • License
    Apache License 2.0
  • Created over 7 years ago
  • Updated almost 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A lightweight, yet powerful ViewPager animation library for Android

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.

More Repositories

1

Prism

A dynamic colouring library for Android
Java
663
star
2

Rialto

Kotlin
259
star
3

Prefekt

Kotlin
205
star
4

Snowfall

Java
205
star
5

WeatherStation

Kotlin
85
star
6

ManualLayoutTransitions

Java
73
star
7

Muselee

Kotlin
57
star
8

AnimatedIcons

55
star
9

MaterialProgressBar

Java
54
star
10

DesignLibrary

Java
50
star
11

ViewPager

Java
40
star
12

DataBinding

Java
39
star
13

CollapsingToolbar

Kotlin
39
star
14

AppBar

Java
33
star
15

StylingActionBar

Java
32
star
16

WindowInsets

Kotlin
31
star
17

AnimatedStateListDrawable

https://blog.stylingandroid.com/animatedstatelistdrawable/
Java
30
star
18

DataStore

Kotlin
28
star
19

Connectivity

Kotlin
26
star
20

ToolTime

Kotlin
25
star
21

Permissions

Java
22
star
22

ChristmasVoice

Java
21
star
23

StateListAnimator

Java
20
star
24

CurvedMotion

Java
20
star
25

RecyclerViewAnimations

Kotlin
20
star
26

BottomNavigationView

Java
18
star
27

IsometricAnimatedVector

Java
18
star
28

FileProvider

Java
17
star
29

Indeterminate

How to create a backwardly compatible material-like indeterminate circular drawable
Java
16
star
30

ArchitectureComponents

Java
16
star
31

Biometrics

Kotlin
16
star
32

ListView

Java
15
star
33

DownloadManager

Java
15
star
34

MdcMotion

Kotlin
15
star
35

ColourWheel

Kotlin
15
star
36

ScrollingTable

Java
14
star
37

VerticalText

How to create a TextView containing vertical text
Java
14
star
38

OreoNotifications

Kotlin
14
star
39

DynamicToolbar

Kotlin
14
star
40

PresenterLite

Java
14
star
41

LocationServices

Java
14
star
42

Parallax

Kotlin
14
star
43

ViewPager2

Kotlin
14
star
44

TextInputLayout

Java
13
star
45

Vectors4All

Java
13
star
46

AlertDialog

How to style an AlertDialog
Java
13
star
47

RenderEffect

Kotlin
13
star
48

MaterialSlider

Kotlin
12
star
49

MidiPad

Kotlin
12
star
50

Nougat

Java
12
star
51

BackgroundTasks

Keeping your app responsive to the user
Java
11
star
52

GestureNavigation

Kotlin
11
star
53

UserIdentity

Java
11
star
54

ChristmasFace

Kotlin
10
star
55

SlidingPaneLayout

Kotlin
10
star
56

Spans

Java
10
star
57

AnimatedVectorDrawables

Java
10
star
58

ImageDecoder

Kotlin
10
star
59

Animation

Introduction to animation
Java
9
star
60

Transparency

Java
9
star
61

MemCache

9
star
62

Text-Shadow

Using shadows on TextView controls
Java
9
star
63

FlexboxLayout

A demo app for Android Developers who want to play with FlexboxLayout
Java
9
star
64

BackupRestore

Java
9
star
65

BasicActionBar

Java
9
star
66

VersionCatalogs

Kotlin
9
star
67

Fonts

9
star
68

CertificatePinning

Kotlin
8
star
69

AdaptiveIcons

Java
8
star
70

CustomControl

Java
8
star
71

Visualiser

Visualiser articles on Styling Android blog
Java
8
star
72

MoreVectorDrawables

Java
7
star
73

Mandelbrot

Kotlin
7
star
74

SimpleThings

Am example app showing how to develop on Android Things
7
star
75

NumberDetector

Kotlin
7
star
76

JUnit5

Support code for Styling Android blog series
Java
7
star
77

CompoundDrawable

Learn how to simplify your layouts using compound drawables
Java
7
star
78

VectorDrawablePng

Java
7
star
79

ReadResolveCheck

A lint check for Kotlin Serializable objects which lack a readResolve() method
Kotlin
7
star
80

StateListDrawables

Java
6
star
81

GridLayout

Java
6
star
82

AnnotationSpans

Kotlin
6
star
83

Time

Kotlin
6
star
84

ActivityResultContract

Kotlin
6
star
85

IntelligentLayouts

Java
6
star
86

SomethingOClock

Java
6
star
87

TextClassification

Kotlin
6
star
88

VectorDrawable

Java
6
star
89

ActionMode

Java
5
star
90

MotionLayoutVisibility

Kotlin
5
star
91

SerializableObject

Kotlin
5
star
92

MaterialShape

Kotlin
4
star
93

MotionVectors

Kotlin
4
star
94

DownloadableFonts

Java
4
star
95

ComposeListDetail

Kotlin
4
star
96

Parcelize

Kotlin
4
star
97

ConstraintLayoutChains

Java
4
star
98

Colours

Java
4
star
99

RadialGradient

Kotlin
4
star
100

DynamicLayouts

Java
4
star