forked from ogdlukic/android-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RemoveItemAnimation.java
131 lines (104 loc) · 3.49 KB
/
RemoveItemAnimation.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
package com.github.axet.androidlibrary.animations;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Transformation;
import android.widget.ListView;
public class RemoveItemAnimation extends Animation {
ViewGroup list;
View convertView;
Runnable run;
int h;
int w;
ViewGroup.LayoutParams lp;
ViewGroup.LayoutParams lpOrig;
float mPivotX, mPivotY;
int duration = 500;
int alphaOffset = 0;
int alphaDuration = (int) (duration * 0.6);
int scaleOffset = alphaDuration;
int scaleDuration = (int) (duration * 0.4);
Handler handler;
public static void apply(final ViewGroup list, final View v, final Runnable run) {
v.startAnimation(new RemoveItemAnimation(list, v, run));
}
public static void restore(final View v) {
Animation a = v.getAnimation();
if (a != null && a instanceof RemoveItemAnimation) {
v.clearAnimation();
RemoveItemAnimation m = (RemoveItemAnimation) a;
m.restore();
}
v.setVisibility(View.VISIBLE);
}
public RemoveItemAnimation(ViewGroup list, View v, Runnable run) {
this.convertView = v;
this.list = list;
this.run = run;
handler = new Handler();
setInterpolator(new AccelerateInterpolator());
setDuration(duration);
this.h = v.getHeight();
this.w = v.getWidth();
this.lp = v.getLayoutParams();
lpOrig = new ViewGroup.LayoutParams(lp);
}
@Override
public void initialize(int width, int height, int parentWidth, int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
convertView.setVisibility(View.VISIBLE);
mPivotX = resolveSize(RELATIVE_TO_SELF, 0.5f, width, parentWidth);
mPivotY = resolveSize(RELATIVE_TO_SELF, 0.5f, height, parentHeight);
}
float intTime(int off, int dur) {
long cur = AnimationUtils.currentAnimationTimeMillis();
long start = getStartTime();
if (start == -1)
start = cur;
long past = cur - start;
long wait = off - past;
long left = dur + wait;
if (wait > 0)
return -1;
if (left < 0)
return 1f;
else
return getInterpolator().getInterpolation((dur - left) / (float) dur);
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
super.applyTransformation(interpolatedTime, t);
{
float ia = intTime(alphaOffset, alphaDuration);
if (ia >= 0) {
t.setAlpha(1 - ia);
t.getMatrix().setTranslate(w * ia, 0);
}
float is = intTime(scaleOffset, scaleDuration);
if (is >= 0) {
is = 1 - is;
t.getMatrix().setScale(is, is, mPivotX, 0);
lp.height = (int) (h * is);
convertView.requestLayout();
}
}
if (interpolatedTime >= 1) {
restore();
end();
}
}
void restore() {
lp.height = lpOrig.height;
convertView.requestLayout();
}
void end() {
convertView.setVisibility(View.GONE);
if (run != null) {
handler.post(run);
run = null;
}
}
}