-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScrollAnimator.java
302 lines (273 loc) · 10.8 KB
/
ScrollAnimator.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package com.xcoder.animator;
import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.TimeInterpolator;
import android.graphics.Rect;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ScrollView;
import androidx.annotation.NonNull;
import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class ScrollAnimator {
private static ScrollAnimator INSTANCE;
private boolean onlyOnce;
private String[] properties;
private int duration = 500;
private TimeInterpolator interpolator;
private Animator.AnimatorListener listener;
private int oldPosition = 0;
/**
* Creates or gets the instance of the class.
*
* @return Singleton instance of the class.
*/
public static ScrollAnimator create() {
if (INSTANCE == null)
INSTANCE = new ScrollAnimator();
return INSTANCE;
}
/**
* Sets the property of animation to use.
*
* @param animation animation property. use constants defined in this class.
* @return this
*/
public ScrollAnimator withAnimation(String... animation) {
properties = animation;
return this;
}
/**
* Sets the custom property for the animation. The property must be a valid property of the view.
* <p>
* For example, if the target view has setter method {@code setBackgroundColor(int)}, then the property will be "backgroundColor".
* </p>
* Note : This must not be called along with {@link #withAnimation(String...)}
* as the last one will override the animation property.
* @param property property of the view to animate
* @param start start value of the property
* @param end end value of the property
* @return this
*/
public ScrollAnimator withCustom(String property,float start,float end){
properties = new String[]{property};
return this;
}
/**
* Sets the duration of the animation.
*
* @param duration duration of the animation. Use constants for best result.
* @return this
*/
public ScrollAnimator tillDuration(int duration) {
this.duration = duration;
return this;
}
/**
* Sets the interpolator of the animation.
*
* @param interpolator interpolator of the animation.
* @return this
*/
public ScrollAnimator withInterpolator(TimeInterpolator interpolator) {
this.interpolator = interpolator;
return this;
}
/**
* Sets the listener of the animation.
*
* @param listener listener of the animation.
* @return this
*/
public ScrollAnimator listenOn(Animator.AnimatorListener listener) {
this.listener = listener;
return this;
}
/**
* Sets the animation to be played only when scrolled bottom to top.
*
* @param reverse true for yes, false otherwise. Default to false.
* @return this
*/
public ScrollAnimator playOnlyOnDownScroll(boolean reverse) {
this.onlyOnce = reverse;
return this;
}
/**
* Starts the animation on the specified nested scroll view. If you are using scroll change listener on this scroll view,
* use {@link #animateOnScroll(FrameLayout, int, int)} instead. Setting listener on this scroll view will not play the animation.
* @param scrollView The scroll view to animate.
*/
public void animate(@NonNull NestedScrollView scrollView) {
View[] views = getChildren((ViewGroup) scrollView.getChildAt(0));
scrollView.setOnScrollChangeListener((NestedScrollView.OnScrollChangeListener) (v, scrollX, scrollY, oldScrollX, oldScrollY) -> {
if (onlyOnce) {
if (scrollY > oldScrollY) {
setUpAnimation(views, scrollView);
}
} else {
setUpAnimation(views, scrollView);
}
});
}
/**
* Starts the animation on the specified scroll view.
*
* @param scrollView The scroll view to animate.
*/
public void animate(@NonNull ScrollView scrollView) {
View[] views = getChildren((ViewGroup) scrollView.getChildAt(0));
scrollView.setOnScrollChangeListener((v, scrollX, scrollY, oldScrollX, oldScrollY) -> {
if (onlyOnce) {
if (scrollY > oldScrollY) {
setUpAnimation(views, scrollView);
}
} else {
setUpAnimation(views, scrollView);
}
});
}
/**
* Starts the animation on the specified list view. Must be called after setting the adapter.
* otherwise, the animation will not work.
*
* @param listView The list view to animate.
*/
public void animate(@NonNull ListView listView) {
ListAdapter adapter = listView.getAdapter();
BaseAdapter animatedAdapter = new BaseAdapter() {
@Override
public int getCount() {
return adapter.getCount();
}
@Override
public Object getItem(int position) {
return adapter.getItem(position);
}
@Override
public long getItemId(int position) {
return adapter.getItemId(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = adapter.getView(position, convertView, parent);
animate(view, position);
return view;
}
};
listView.setAdapter(animatedAdapter);
}
/**
* Starts the animation on the specified recycler view. Must be called after setting adapter to it
* or else you will get a null pointer exception.
* @param recyclerView The recycler view to animate.
*/
public void animate(@NonNull RecyclerView recyclerView) {
RecyclerView.Adapter adapter = recyclerView.getAdapter();
RecyclerView.Adapter animatedAdapter = new RecyclerView.Adapter() {
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
RecyclerView.ViewHolder viewHolder = adapter.onCreateViewHolder(parent, viewType);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
adapter.onBindViewHolder(holder, position);
animate(holder.itemView, position);
}
@Override
public int getItemCount() {
return adapter.getItemCount();
}
};
recyclerView.setAdapter(animatedAdapter);
}
/**
* Call this in adapters onBindViewHolder/getView to animate the items.
*
* @param view The item that is to be displayed in the recycler/list view.
*/
protected void animate(@NonNull View view, int position) {
Animator animator = getAnimation(view);
if (onlyOnce) {
System.out.println("oldPosition: " + oldPosition + " position: " + position);
if (position > oldPosition) {
animator.start();
oldPosition = position;
} else {
oldPosition = position - 1;
}
} else {
animator.start();
}
}
/**
* Animates the scrollview when scroll is changed. To be called in scrollview's onScrollChangeListener.
* @param scrollview The scrollview to animate.
* @param scrollY The current scrollY of the scrollview.
* @param oldScrollY The previous scrollY of the scrollview.
*/
public void animateOnScroll(@NonNull FrameLayout scrollview, int scrollY, int oldScrollY) {
View[] views = getChildren((ViewGroup) scrollview.getChildAt(0));
if (onlyOnce) {
if (scrollY > oldScrollY) {
setUpAnimation(views, scrollview);
}
} else {
setUpAnimation(views, scrollview);
}
}
private void setUpAnimation(View[] views, FrameLayout scrollView) {
for (View view : views) {
if (isHidden(view, scrollView)) {
getAnimation(view).start();
}
}
}
private AnimatorSet getAnimation(View view) {
ObjectAnimator animator;
List<Animator> animators = new ArrayList<>();
for (String property : properties){
switch (property) {
case "slide4left" -> animator = ObjectAnimator.ofFloat(view, "translationX", -view.getLeft() - 150, 0);
case "slide4right" -> animator = ObjectAnimator.ofFloat(view, "translationX", view.getRight(), 0);
case "slide4top" -> animator = ObjectAnimator.ofFloat(view, "translationY", -view.getResources().getDisplayMetrics().heightPixels, 0);
case "slide4bottom" -> animator = ObjectAnimator.ofFloat(view, "translationY", view.getBottom() + 150, 0);
case "drop4top" -> animator = ObjectAnimator.ofFloat(view, "rotation", 90, 0);
case "drop4bottom" -> animator = ObjectAnimator.ofFloat(view, "rotation", -90, 0);
case "rotate" -> animator = ObjectAnimator.ofFloat(view, "rotation", 360, 0);
default -> animator = ObjectAnimator.ofFloat(view, "alpha", 0, 1);
}
animators.add(animator);
}
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(animators);
animatorSet.setDuration(duration);
animatorSet.setInterpolator(interpolator);
if (listener != null)
animatorSet.addListener(listener);
return animatorSet;
}
private View[] getChildren(ViewGroup layout) {
View[] views = new View[layout.getChildCount()];
for (int i = 0; i < layout.getChildCount(); i++) {
views[i] = layout.getChildAt(i);
}
return views;
}
public boolean isHidden(View view, FrameLayout scrollView) {
Rect scrollBounds = new Rect();
scrollView.getDrawingRect(scrollBounds);
float top = view.getY();
float bottom = top + view.getHeight();
return scrollBounds.top > top && scrollBounds.bottom < bottom;
}
}