forked from Comcast/FreeFlow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DefaultLayoutAnimator.java
341 lines (262 loc) · 9.69 KB
/
DefaultLayoutAnimator.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*******************************************************************************
* Copyright 2013 Comcast Cable Communications Management, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package com.comcast.freeflow.animations;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import com.comcast.freeflow.core.FreeFlowContainer;
import com.comcast.freeflow.core.FreeFlowItem;
import com.comcast.freeflow.core.LayoutChangeset;
import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.graphics.Rect;
import android.util.Log;
import android.util.Pair;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.animation.DecelerateInterpolator;
public class DefaultLayoutAnimator implements FreeFlowLayoutAnimator {
protected LayoutChangeset changeSet;
public static final String TAG = "DefaultLayoutAnimator";
/**
* The duration of the "Appear" animation per new cell being added. Note
* that the total time of the "Appear" animation will be based on the
* cumulative total of this animation playing on each cell
*/
public int newCellsAdditionAnimationDurationPerCell = 200;
public int newCellsAdditionAnimationStartDelay = 0;
/**
* The duration of the "Disappearing" animation per old cell being removed.
* Note that the total time of the "Disappearing" animation will be based on
* the cumulative total of this animation playing on each cell being removed
*/
public int oldCellsRemovalAnimationDuration = 200;
public int oldCellsRemovalAnimationStartDelay = 0;
public int cellPositionTransitionAnimationDuration = 250;
/**
* If set to true, this forces the animation sets to animate in the
* following sequence: delete then add then move
*
* If set to false, all sets will animate in parallel
*/
public boolean animateAllSetsSequentially = false;
/**
* If set to true, this forces each view in a set to animate sequentially
*
* If set to false, all views for a set will animate in parallel
*/
public boolean animateIndividualCellsSequentially = false;
protected FreeFlowContainer callback;
protected AnimatorSet disappearingSet = null;
protected AnimatorSet appearingSet = null;
protected AnimatorSet movingSet = null;
public DefaultLayoutAnimator() {
}
@Override
public void cancel() {
if (disappearingSet != null)
disappearingSet.cancel();
if (appearingSet != null)
appearingSet.cancel();
if (movingSet != null)
movingSet.cancel();
mIsRunning = false;
}
protected boolean mIsRunning = false;
@Override
public void animateChanges(LayoutChangeset changeSet, final FreeFlowContainer callback) {
this.changeSet = changeSet;
this.callback = callback;
Log.d(TAG, "Changes: " + changeSet.toString());
cancel();
mIsRunning = true;
disappearingSet = null;
appearingSet = null;
movingSet = null;
Comparator<FreeFlowItem> cmp = new Comparator<FreeFlowItem>() {
@Override
public int compare(FreeFlowItem lhs, FreeFlowItem rhs) {
return (lhs.itemSection * 1000 + lhs.itemIndex) - (rhs.itemSection * 1000 + rhs.itemIndex);
}
};
List<FreeFlowItem> removed = changeSet.getRemoved();
if (removed.size() > 0) {
Collections.sort(removed, cmp);
disappearingSet = getItemsRemovedAnimation(changeSet.getRemoved());
}
List<FreeFlowItem> added = changeSet.getAdded();
if (added.size() > 0) {
Collections.sort(added, cmp);
appearingSet = getItemsAddedAnimation(added);
}
if (changeSet.getMoved().size() > 0) {
movingSet = getItemsMovedAnimation(changeSet.getMoved());
}
AnimatorSet all = getAnimationSequence();
if (all == null) {
mIsRunning = false;
callback.onLayoutChangeAnimationsCompleted(this);
} else {
all.addListener(new AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
mIsRunning = false;
callback.onLayoutChangeAnimationsCompleted(DefaultLayoutAnimator.this);
}
@Override
public void onAnimationCancel(Animator animation) {
}
});
all.start();
}
}
/**
* The animation to run on the items being removed
*
* @param removed
* An ArrayList of <code>FreeFlowItems</code> removed
* @return The AnimatorSet of the removed objects
*/
protected AnimatorSet getItemsRemovedAnimation(List<FreeFlowItem> removed) {
AnimatorSet disappearingSet = new AnimatorSet();
ArrayList<Animator> fades = new ArrayList<Animator>();
for (FreeFlowItem proxy : removed) {
fades.add(ObjectAnimator.ofFloat(proxy.view, "alpha", 0));
}
disappearingSet.setDuration(oldCellsRemovalAnimationDuration);
disappearingSet.setStartDelay(oldCellsRemovalAnimationStartDelay);
if (animateIndividualCellsSequentially)
disappearingSet.playSequentially(fades);
else
disappearingSet.playTogether(fades);
return disappearingSet;
}
/**
*
*/
protected AnimatorSet getItemsAddedAnimation(List<FreeFlowItem> added) {
AnimatorSet appearingSet = new AnimatorSet();
ArrayList<Animator> fadeIns = new ArrayList<Animator>();
for (FreeFlowItem proxy : added) {
proxy.view.setAlpha(0);
fadeIns.add(ObjectAnimator.ofFloat(proxy.view, "alpha", 1));
}
if (animateIndividualCellsSequentially)
appearingSet.playSequentially(fadeIns);
else
appearingSet.playTogether(fadeIns);
appearingSet.setStartDelay(newCellsAdditionAnimationStartDelay);
appearingSet.setDuration(newCellsAdditionAnimationDurationPerCell);
return appearingSet;
}
protected AnimatorSet getAnimationSequence() {
if (disappearingSet == null && appearingSet == null && movingSet == null)
return null;
AnimatorSet allAnim = new AnimatorSet();
ArrayList<Animator> all = new ArrayList<Animator>();
if (disappearingSet != null)
all.add(disappearingSet);
if (appearingSet != null)
all.add(appearingSet);
if (movingSet != null)
all.add(movingSet);
if (animateAllSetsSequentially)
allAnim.playSequentially(all);
else
allAnim.playTogether(all);
return allAnim;
}
protected AnimatorSet getItemsMovedAnimation(List<Pair<FreeFlowItem, Rect>> moved) {
AnimatorSet anim = new AnimatorSet();
ArrayList<Animator> moves = new ArrayList<Animator>();
for (Pair<FreeFlowItem, Rect> item : moved) {
FreeFlowItem proxy = FreeFlowItem.clone(item.first);
View v = proxy.view;
proxy.frame.left -= callback.getViewportLeft();
proxy.frame.top -= callback.getViewportTop();
proxy.frame.right -= callback.getViewportLeft();
proxy.frame.bottom -= callback.getViewportTop();
// Log.d(TAG, "vpx = " + callback.viewPortX + ", vpy = " +
// callback.viewPortY);
moves.add(transitionToFrame(item.second, proxy, v));
}
anim.playTogether(moves);
return anim;
}
// @Override
public ValueAnimator transitionToFrame(final Rect of, final FreeFlowItem nf, final View v) {
ValueAnimator anim = ValueAnimator.ofFloat(0f, 1f);
anim.setDuration(cellPositionTransitionAnimationDuration);
//
// Log.d(TAG, "of width = " + of.width() + ", nf width = " + nf.frame.width());
// Log.d(TAG, "of height = " + of.height() + ", nf height = " + nf.frame.height());
anim.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
try {
int itemWidth = of.width()
+ (int) ((nf.frame.width() - of.width()) * animation.getAnimatedFraction());
int itemHeight = of.height()
+ (int) ((nf.frame.height() - of.height()) * animation.getAnimatedFraction());
int widthSpec = MeasureSpec.makeMeasureSpec(itemWidth, MeasureSpec.EXACTLY);
int heightSpec = MeasureSpec.makeMeasureSpec(itemHeight, MeasureSpec.EXACTLY);
v.measure(widthSpec, heightSpec);
Rect frame = new Rect();
Rect nff = nf.frame;
frame.left = (int) (of.left + (nff.left - of.left) * animation.getAnimatedFraction());
frame.top = (int) (of.top + (nff.top - of.top) * animation.getAnimatedFraction());
frame.right = frame.left
+ (int) (of.width() + (nff.width() - of.width()) * animation.getAnimatedFraction());
frame.bottom = frame.top
+ (int) (of.height() + (nff.height() - of.height()) * animation.getAnimatedFraction());
v.layout(frame.left, frame.top, frame.right, frame.bottom);
// v.layout(nf.frame.left, nf.frame.top, nf.frame.right,
// nf.frame.bottom);
// v.setAlpha((1 - alpha) * animation.getAnimatedFraction()
// + alpha);
} catch (NullPointerException e) {
Log.e(TAG, "Nullpointer exception");
e.printStackTrace();
animation.cancel();
}
}
});
anim.setInterpolator(new DecelerateInterpolator(2.0f));
return anim;
}
public void setDuration(int duration) {
this.cellPositionTransitionAnimationDuration = duration;
}
@Override
public LayoutChangeset getChangeSet() {
return changeSet;
}
@Override
public boolean isRunning() {
return mIsRunning;
}
}