-
-
Notifications
You must be signed in to change notification settings - Fork 305
/
ParallaxFrameLayout.java
108 lines (85 loc) · 3.34 KB
/
ParallaxFrameLayout.java
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package com.heinrichreimersoftware.materialintro.view.parallax;
import android.content.Context;
import android.content.res.TypedArray;
import androidx.annotation.FloatRange;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import com.heinrichreimersoftware.materialintro.R;
public class ParallaxFrameLayout extends FrameLayout implements Parallaxable {
@FloatRange(from = -1.0, to = 1.0)
private float offset = 0;
public ParallaxFrameLayout(Context context) {
super(context);
}
public ParallaxFrameLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ParallaxFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
return p instanceof LayoutParams;
}
@Override
protected LayoutParams generateDefaultLayoutParams() {
return new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
}
@Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
return new LayoutParams(getContext(), attrs);
}
@Override
protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
return new LayoutParams(p);
}
@Override
public void setOffset(@FloatRange(from = -1.0, to = 1.0) float offset) {
this.offset = offset;
for (int i = getChildCount() - 1; i >= 0; i--) {
View child = getChildAt(i);
ParallaxFrameLayout.LayoutParams p = (LayoutParams) child.getLayoutParams();
if (p.parallaxFactor == 0) continue;
child.setTranslationX(getWidth() * -offset * p.parallaxFactor);
}
}
public static class LayoutParams extends FrameLayout.LayoutParams {
float parallaxFactor = 0f;
public LayoutParams(Context c, AttributeSet attrs) {
super(c, attrs);
TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.ParallaxLayout_Layout);
parallaxFactor = a.getFloat(R.styleable.ParallaxLayout_Layout_layout_parallaxFactor, parallaxFactor);
a.recycle();
}
public LayoutParams(int width, int height) {
super(width, height);
}
public LayoutParams(int width, int height, float parallaxFactor) {
super(width, height);
this.parallaxFactor = parallaxFactor;
}
public LayoutParams(int width, int height, int gravity) {
super(width, height, gravity);
}
public LayoutParams(int width, int height, int gravity, float parallaxFactor) {
super(width, height, gravity);
this.parallaxFactor = parallaxFactor;
}
public LayoutParams(ViewGroup.LayoutParams source) {
super(source);
}
public LayoutParams(ViewGroup.LayoutParams source, float parallaxFactor) {
super(source);
this.parallaxFactor = parallaxFactor;
}
public LayoutParams(MarginLayoutParams source) {
super(source);
}
public LayoutParams(MarginLayoutParams source, float parallaxFactor) {
super(source);
this.parallaxFactor = parallaxFactor;
}
}
}