mViewsToParallax
16 | = new ArrayList<>();
17 |
18 | ParallaxPageTransformer() {
19 | }
20 |
21 | ParallaxPageTransformer addViewToParallax(
22 | ParallaxTransformInformation viewInfo) {
23 | if (mViewsToParallax != null) {
24 | mViewsToParallax.add(viewInfo);
25 | }
26 | return this;
27 | }
28 |
29 | public void transformPage(View view, float position) {
30 |
31 | int pageWidth = view.getWidth();
32 |
33 | if (position < -1) {
34 | // This page is way off-screen to the left.
35 | view.setAlpha(1);
36 |
37 | } else if (position <= 1 && mViewsToParallax != null) { // [-1,1]
38 | for (ParallaxTransformInformation parallaxTransformInformation : mViewsToParallax) {
39 | applyParallaxEffect(view, position, pageWidth, parallaxTransformInformation,
40 | position > 0);
41 | }
42 | } else {
43 | // This page is way off-screen to the right.
44 | view.setAlpha(1);
45 | }
46 | }
47 |
48 | private void applyParallaxEffect(View view, float position, int pageWidth,
49 | ParallaxTransformInformation information, boolean isEnter) {
50 | if (information.isValid() && view.findViewById(information.resource) != null) {
51 | if (isEnter && !information.isEnterDefault()) {
52 | view.findViewById(information.resource)
53 | .setTranslationX(-position * (pageWidth / information.parallaxEnterEffect));
54 | } else if (!isEnter && !information.isExitDefault()) {
55 | view.findViewById(information.resource)
56 | .setTranslationX(-position * (pageWidth / information.parallaxExitEffect));
57 | }
58 | }
59 | }
60 |
61 |
62 | /**
63 | * Information to make the parallax effect in a concrete view.
64 | *
65 | * parallaxEffect positive values reduces the speed of the view in the translation
66 | * ParallaxEffect negative values increase the speed of the view in the translation
67 | * Try values to see the different effects. I recommend 2, 0.75 and 0.5
68 | */
69 | static class ParallaxTransformInformation {
70 |
71 | static final float PARALLAX_EFFECT_DEFAULT = -101.1986f;
72 |
73 | int resource = -1;
74 | float parallaxEnterEffect = 1f;
75 | float parallaxExitEffect = 1f;
76 |
77 | ParallaxTransformInformation(int resource, float parallaxEnterEffect,
78 | float parallaxExitEffect) {
79 | this.resource = resource;
80 | this.parallaxEnterEffect = parallaxEnterEffect;
81 | this.parallaxExitEffect = parallaxExitEffect;
82 | }
83 |
84 | boolean isValid() {
85 | return parallaxEnterEffect != 0 && parallaxExitEffect != 0 && resource != -1;
86 | }
87 |
88 | boolean isEnterDefault() {
89 | return parallaxEnterEffect == PARALLAX_EFFECT_DEFAULT;
90 | }
91 |
92 | boolean isExitDefault() {
93 | return parallaxExitEffect == PARALLAX_EFFECT_DEFAULT;
94 | }
95 | }
96 | }
97 |
98 |
--------------------------------------------------------------------------------
/library/src/main/java/com/ihsanbal/introview/ScrollerCustomDuration.java:
--------------------------------------------------------------------------------
1 | package com.ihsanbal.introview;
2 |
3 | import android.content.Context;
4 | import android.view.animation.Interpolator;
5 | import android.widget.Scroller;
6 |
7 | /**
8 | * @author ihsan on 10/10/16.
9 | */
10 |
11 | class ScrollerCustomDuration extends Scroller {
12 |
13 | private double mScrollFactor = 1;
14 |
15 | ScrollerCustomDuration(Context context, Interpolator interpolator) {
16 | super(context, interpolator);
17 | }
18 |
19 | /**
20 | * Set the factor by which the duration will change
21 | */
22 | void setScrollDurationFactor(double scrollFactor) {
23 | mScrollFactor = scrollFactor;
24 | }
25 |
26 | @Override
27 | public void startScroll(int startX, int startY, int dx, int dy, int duration) {
28 | super.startScroll(startX, startY, dx, dy, (int) (duration * mScrollFactor));
29 | }
30 |
31 | }
32 |
--------------------------------------------------------------------------------
/library/src/main/res/layout/fragment_page.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
12 |
13 |
22 |
23 |
32 |
33 |
34 |
--------------------------------------------------------------------------------
/library/src/main/res/values/attrs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
--------------------------------------------------------------------------------
/library/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/library/src/test/java/com/ihsanbal/introview/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.ihsanbal.introview;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':library'
2 |
--------------------------------------------------------------------------------