-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClickEffect.java
More file actions
278 lines (259 loc) · 11.4 KB
/
Copy pathClickEffect.java
File metadata and controls
278 lines (259 loc) · 11.4 KB
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
import android.content.Context;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.os.Handler;
import android.support.v4.view.animation.LinearOutSlowInInterpolator;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.BounceInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Interpolator;
import android.widget.ImageButton;
import android.widget.ImageView;
import java.util.ArrayList;
import java.util.Random;
import timber.log.Timber;
/**
* Created by Wesely on 2018/1/3.
* Modificade by John on 11/5/2019,
*/
public class ClickEffect {
public static class AxisPoint {
int x;
int y;
static AxisPoint getCenter(View v){
AxisPoint point = new AxisPoint();
point.x = (int) v.getX()+v.getWidth()/2;
point.y = (int) v.getY()+v.getHeight()/2;
return point;
}
}
private static int getRandomColor() {
Random rnd = new Random();
int transparent = 200;
return Color.argb(transparent, 56 + rnd.nextInt(170), 56 + rnd.nextInt(170), 56 + rnd.nextInt(170));
}
private static void setRandomColor(Drawable d) {
Random rnd = new Random();
int transparent = 200;
int color = Color.argb(transparent, 56 + rnd.nextInt(170), 56 + rnd.nextInt(170), 56 + rnd.nextInt(170));
d.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.LIGHTEN));
}
public static void animNova(Context context, ImageButton v) {
int effectSize = v.getWidth() * 2 / 3;
int color = getRandomColor(); // get a random color
AxisPoint p = AxisPoint.getCenter(v);
ImageView iv = new ImageView(context);
((ViewGroup) v.getParent()).addView(iv); // add View to parent
GradientDrawable gd = new GradientDrawable();
gd.setColor(color);
gd.setShape(GradientDrawable.OVAL);
gd.setStroke(8, color * 7 / 10);
iv.setBackground(gd);
iv.getLayoutParams().height = effectSize;
iv.getLayoutParams().width = effectSize;
iv.setX(p.x - effectSize / 2);
iv.setY(p.y - effectSize / 2);
v.bringToFront(); // bring original view to front
Interpolator ip = new DecelerateInterpolator();
iv.animate().scaleX(6).scaleY(6).setDuration(400).alpha(0).setInterpolator(ip);
}
public static void animRipple(Context context, View v) {
animRing(context, v, 300);
animRing(context, v, 700);
}
public static void animRing(Context context, View v, int duration) {
int effectSize = v.getWidth() * 2 / 3;
int color = getRandomColor(); // get a random color
AxisPoint p = AxisPoint.getCenter(v);
ImageView iv = new ImageView(context);
((ViewGroup) v.getParent()).addView(iv); // add View to parent
GradientDrawable gd = new GradientDrawable();
gd.setShape(GradientDrawable.OVAL);
gd.setStroke(6, color);
iv.setBackground(gd);
iv.getLayoutParams().height = effectSize;
iv.getLayoutParams().width = effectSize;
iv.setX(p.x - effectSize / 2);
iv.setY(p.y - effectSize / 2);
v.bringToFront(); // bring original view to front
Interpolator ip = new DecelerateInterpolator();
iv.animate().scaleX(4).scaleY(4).setDuration(duration).alpha(0).setInterpolator(ip);
}
public static void animParticalNova(Context context, View v, int particles) {
int radious = v.getWidth() * 6 / 5;
int ballSize = v.getWidth() / 8;
int color = getRandomColor(); // get a random color
AxisPoint p = AxisPoint.getCenter(v);
ArrayList<AxisPoint> points = new ArrayList<>();
for (int i = 0; i < particles; i++) {
AxisPoint point = new AxisPoint();
point.x = (int) (Math.cos(Math.PI * 2 * i / particles) * radious);
point.y = (int) (Math.sin(Math.PI * 2 * i / particles) * radious);
points.add(point);
ImageView iv = new ImageView(context);
((ViewGroup) v.getParent()).addView(iv); // add View to parent
GradientDrawable gd = new GradientDrawable();
gd.setColor(color);
gd.setShape(GradientDrawable.OVAL);
iv.setBackground(gd);
iv.getLayoutParams().height = ballSize;
iv.getLayoutParams().width = ballSize;
iv.setX(p.x - ballSize / 2);
iv.setY(p.y - ballSize / 2);
v.bringToFront(); // bring original view to front
Interpolator ip = new LinearOutSlowInInterpolator();
iv.animate().translationXBy(point.x).translationYBy(point.y).scaleX(2).scaleY(2).setDuration(400).alpha(0).setInterpolator(ip);
}
}
public static void animExplosion(Context context, View v) {
animParticalNova(context, v, 18);
int particles = 9;
int radious = v.getWidth() * 8 / 5;
int ballSize = v.getWidth() / 6;
int color = getRandomColor(); // get a random color
AxisPoint p = AxisPoint.getCenter(v);
ArrayList<AxisPoint> points = new ArrayList<AxisPoint>();
for (int i = 0; i < particles; i++) {
AxisPoint point = new AxisPoint();
point.x = (int) (Math.cos(Math.PI * 2 * i / particles) * radious);
point.y = (int) (Math.sin(Math.PI * 2 * i / particles) * radious);
points.add(point);
ImageView iv = new ImageView(context);
((ViewGroup) v.getParent()).addView(iv); // add View to parent
GradientDrawable gd = new GradientDrawable();
gd.setColor(color);
gd.setShape(GradientDrawable.OVAL);
iv.setBackground(gd);
iv.getLayoutParams().height = ballSize;
iv.getLayoutParams().width = ballSize;
iv.setX(p.x - ballSize / 2);
iv.setY(p.y - ballSize / 2);
v.bringToFront(); // bring original view to front
Interpolator ip = new DecelerateInterpolator();
iv.animate().translationXBy(point.x).translationYBy(point.y).scaleX(2).scaleY(2).setDuration(400).alpha(0).setInterpolator(ip);
}
}
/**
* Create another ImageView and then change its scale.
* Can ONLY apply to ImageView or ImageButton.
*
* @param context
* @param v
*/
public static void animHeartBeat(Context context, ImageButton v) {
AxisPoint p = AxisPoint.getCenter(v);
ImageView iv = new ImageView(context);
((ViewGroup) v.getParent()).addView(iv); // add View to parent
iv.setImageDrawable(v.getDrawable());
iv.getLayoutParams().height = v.getHeight();
iv.getLayoutParams().width = v.getWidth();
iv.setX(p.x - v.getWidth() / 2);
iv.setY(p.y - v.getHeight() / 2);
setRandomColor(iv.getDrawable());
Interpolator ip = new BounceInterpolator();
iv.animate().scaleX(3).scaleY(3).setInterpolator(ip).setDuration(500).alpha(0);
}
private static Handler handle = new Handler();
public static void animHelixNova(Context context, final ImageButton v, final int particles) {
final int radious = (int) (v.getWidth() * 3);
final double angle = Math.PI * 2 / 3; // total spinning angle (rad)
final int duration = 600;
int ballSize = v.getWidth() / 6;
final ArrayList<ImageView> points = new ArrayList<>();
for (int i = 0; i < particles; i++) {
ImageView iv = new ImageView(context);
((ViewGroup) v.getParent()).addView(iv); // add View to parent
GradientDrawable gd = new GradientDrawable();
gd.setColor(getRandomColor());
gd.setShape(GradientDrawable.OVAL);
iv.setBackground(gd);
iv.getLayoutParams().height = ballSize;
iv.getLayoutParams().width = ballSize;
points.add(iv);
}
v.bringToFront(); // bring original view to front
final long start = System.currentTimeMillis();
new Thread(new Runnable() {
@Override
public void run() {
int t = (int) (System.currentTimeMillis() - start);
double progress = (t * 1.0) / duration;
for (int i = 0; i < points.size(); i++) {
ImageView iv = points.get(i);
double x = AxisPoint.getCenter(v).x - iv.getWidth() / 2
+ Math.cos(angle * progress + Math.PI * 2 * i / points.size())
* radious * progress / 2;
double y = AxisPoint.getCenter(v).y - iv.getHeight() / 2
+ Math.sin(angle * progress + Math.PI * 2 * i / points.size())
* radious * progress / 2;
iv.setX((float) x);
iv.setY((float) y);
iv.setScaleX((float) 2);
iv.setScaleY((float) 2);
iv.setAlpha((float) (1 - progress));
}
if (progress < 1)
handle.postDelayed(this, 10);
else
for (int i = 0; i < points.size(); i++) {
points.get(i).setVisibility(View.GONE);
}
}
}).start();
}
public static void animJumping(final View v) {
final int duration = 800;
final long start = System.currentTimeMillis();
final float origin = v.getY();
new Thread(new Runnable() {
@Override
public void run() {
int t = (int) (System.currentTimeMillis() - start);
double progress = (t * 1.0) / duration;
float y = (float) Math.abs(
v.getHeight()
* Math.sin(3 * Math.PI * progress)
* (1 - progress));
v.setY(origin - y);
Timber.d(y + "/" + progress);
if (progress < 1)
handle.postDelayed(this, 20);
else
v.setY(origin);
}
}).start();
}
public static void animShaking(final View v) {
final int duration = 1000;
final long start = System.currentTimeMillis();
final float origin = v.getY();
final float rotateAngle = (float) (Math.PI) * 8;
final float waveTimes = 5;
new Thread(new Runnable() {
@Override
public void run() {
int t = (int) (System.currentTimeMillis() - start);
double progress = (t * 1.0) / duration;
float y = (float) Math.abs(
v.getHeight()
* Math.sin(Math.PI * progress)
* (1 - progress));
v.setY(origin - y);
// v.setRotation((float) ((rotateAngle * waveTimes * progress) % rotateAngle - 0.5 * rotateAngle));
v.setRotation((float) (45 * Math.sin(progress * Math.PI * 6)));
Timber.d(y + "/" + progress);
if (progress < 1)
handle.postDelayed(this, 10);
else {
v.setY(origin);
v.setRotation(0);
}
}
}).start();
}
}