-
Notifications
You must be signed in to change notification settings - Fork 25
/
ParallaxImageView.java
207 lines (168 loc) · 6 KB
/
ParallaxImageView.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package com.yayandroid.parallaxrecyclerview;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.util.AttributeSet;
import android.widget.ImageView;
/**
* Created by yahyabayramoglu on 15/04/15.
*/
public class ParallaxImageView extends ImageView {
private final float DEFAULT_PARALLAX_RATIO = 1.2f;
private float parallaxRatio = DEFAULT_PARALLAX_RATIO;
private boolean DEFAULT_CENTER_CROP = true;
private boolean shouldCenterCrop = DEFAULT_CENTER_CROP;
private boolean needToTranslate = true;
private ParallaxImageListener listener;
private int rowYPos = -1;
private int recyclerViewHeight = -1;
private int recyclerViewYPos = -1;
public interface ParallaxImageListener {
int[] requireValuesForTranslate();
}
public ParallaxImageView(Context context) {
super(context);
init(context, null);
}
public ParallaxImageView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public ParallaxImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
setScaleType(ImageView.ScaleType.MATRIX);
if (attrs != null) {
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.ParallaxImageView, 0, 0);
this.parallaxRatio = ta.getFloat(R.styleable.ParallaxImageView_parallax_ratio, DEFAULT_PARALLAX_RATIO);
this.shouldCenterCrop = ta.getBoolean(R.styleable.ParallaxImageView_center_crop, DEFAULT_CENTER_CROP);
ta.recycle();
}
}
/**
* This trick was needed because there is no way to detect when image is displayed,
* we need to translate image for very first time as well. This will be needed only
* if you are using async image loading...
* <p/>
* # If only there was another way to get notified when image has displayed.
*/
// region EnsureTranslate
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
ensureTranslate();
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
ensureTranslate();
}
@Override
public void setImageDrawable(Drawable drawable) {
super.setImageDrawable(drawable);
ensureTranslate();
}
@Override
public void setImageBitmap(Bitmap bm) {
super.setImageBitmap(bm);
ensureTranslate();
}
@Override
public void setImageResource(int resId) {
super.setImageResource(resId);
ensureTranslate();
}
@Override
public void setImageURI(Uri uri) {
super.setImageURI(uri);
ensureTranslate();
}
// endregion
/**
* Notify this view when it is back on recyclerView, so we can reset.
*/
public void reuse() {
this.needToTranslate = true;
}
public void centerCrop(boolean enable) {
this.shouldCenterCrop = enable;
}
public void setParallaxRatio(float parallaxRatio) {
this.parallaxRatio = parallaxRatio;
}
public void setListener(ParallaxImageListener listener) {
this.listener = listener;
}
public ParallaxImageListener getListener() {
return listener;
}
public synchronized boolean doTranslate() {
if (getDrawable() == null) {
return false;
}
if (getListener() != null && getValues()) {
calculateAndMove();
return true;
} else {
return false;
}
}
private boolean ensureTranslate() {
if (needToTranslate) {
needToTranslate = !doTranslate();
}
return !needToTranslate;
}
private boolean getValues() {
int[] values = getListener().requireValuesForTranslate();
if (values == null)
return false;
this.rowYPos = values[0];
this.recyclerViewHeight = values[1];
this.recyclerViewYPos = values[2];
return true;
}
private void calculateAndMove() {
float distanceFromCenter = (recyclerViewYPos + recyclerViewHeight) / 2 - rowYPos;
int drawableHeight = getDrawable().getIntrinsicHeight();
int imageViewHeight = getMeasuredHeight();
float scale = 1;
if (shouldCenterCrop) {
scale = recomputeImageMatrix();
drawableHeight *= scale;
}
float difference = drawableHeight - imageViewHeight;
float move = (distanceFromCenter / recyclerViewHeight) * difference * parallaxRatio;
moveTo((move / 2) - (difference / 2), scale);
}
private float recomputeImageMatrix() {
float scale;
final int viewWidth = getWidth() - getPaddingLeft() - getPaddingRight();
final int viewHeight = getHeight() - getPaddingTop() - getPaddingBottom();
final int drawableWidth = getDrawable().getIntrinsicWidth();
final int drawableHeight = getDrawable().getIntrinsicHeight();
if (drawableWidth * viewHeight > drawableHeight * viewWidth) {
scale = (float) viewHeight / (float) drawableHeight;
} else {
scale = (float) viewWidth / (float) drawableWidth;
}
return scale;
}
private void moveTo(float move, float scale) {
Matrix imageMatrix = getImageMatrix();
if (scale != 1) {
imageMatrix.setScale(scale, scale);
}
float[] matrixValues = new float[9];
imageMatrix.getValues(matrixValues);
float current = matrixValues[Matrix.MTRANS_Y];
imageMatrix.postTranslate(0, move - current);
setImageMatrix(imageMatrix);
invalidate();
}
}