ButtonAnimUtil.java
8.7 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
package com.cnlive.strike.xiaojiaspeaker;
import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.os.Build;
import android.os.Handler;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.view.animation.DecelerateInterpolator;
/**
* 圆形按钮点击特效
*
* @author ShinnyYang
*/
public class ButtonAnimUtil {
public static final int DEFAULT_ZOOM_DURATION = 50;//默认缩放动画时间间隔
public static final int SHOW_VIEW_ZOOM_DURATION = 150;//显示控件缩放动画时间间隔
public static final int HIDE_VIEW_ZOOM_DURATION = 100;//隐藏控件缩放动画时间间隔
//设置控件放大动画
public static void setViewZoomIn(View view) {
try {
AnimatorSet animatorSetsuofang = new AnimatorSet();//组合动画
ObjectAnimator scaleX = ObjectAnimator.ofFloat(view, "scaleX", 1, 1.2f);
ObjectAnimator scaleY = ObjectAnimator.ofFloat(view, "scaleY", 1, 1.2f);
animatorSetsuofang.setDuration(DEFAULT_ZOOM_DURATION);
animatorSetsuofang.setInterpolator(new DecelerateInterpolator());
animatorSetsuofang.play(scaleX).with(scaleY);//两个动画同时开始
animatorSetsuofang.start();
} catch (Exception e) {
}
}
//设置控件缩小动画
public static void setViewZoomOut(View view) {
try {
AnimatorSet animatorSetsuofang = new AnimatorSet();//组合动画
ObjectAnimator scaleX = ObjectAnimator.ofFloat(view, "scaleX", 1.2f, 1);
ObjectAnimator scaleY = ObjectAnimator.ofFloat(view, "scaleY", 1.2f, 1);
animatorSetsuofang.setDuration(DEFAULT_ZOOM_DURATION);
animatorSetsuofang.setInterpolator(new DecelerateInterpolator());
animatorSetsuofang.play(scaleX).with(scaleY);//两个动画同时开始
animatorSetsuofang.start();
} catch (Exception e) {
}
}
//设置控件缩小动画
public static void setViewDefault(View view) {
try {
AnimatorSet animatorSetsuofang = new AnimatorSet();//组合动画
ObjectAnimator scaleX = ObjectAnimator.ofFloat(view, "scaleX", 1f, 1);
ObjectAnimator scaleY = ObjectAnimator.ofFloat(view, "scaleY", 1f, 1);
animatorSetsuofang.setDuration(DEFAULT_ZOOM_DURATION);
animatorSetsuofang.setInterpolator(new DecelerateInterpolator());
animatorSetsuofang.play(scaleX).with(scaleY);//两个动画同时开始
animatorSetsuofang.start();
} catch (Exception e) {
}
}
//设置控件显示放大动画
public static void setViewShowZoomIn(View view) {
try {
AnimatorSet animatorSetsuofang = new AnimatorSet();//组合动画
ObjectAnimator scaleX = ObjectAnimator.ofFloat(view, "scaleX", 0, 1.2f);
ObjectAnimator scaleY = ObjectAnimator.ofFloat(view, "scaleY", 0, 1.2f);
animatorSetsuofang.setDuration(SHOW_VIEW_ZOOM_DURATION);
animatorSetsuofang.setInterpolator(new DecelerateInterpolator());
animatorSetsuofang.play(scaleX).with(scaleY);//两个动画同时开始
animatorSetsuofang.start();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (view == null) {
return;
}
AnimatorSet animatorSetsuofang = new AnimatorSet();//组合动画
ObjectAnimator scaleX = ObjectAnimator.ofFloat(view, "scaleX", 1.2f, 1);
ObjectAnimator scaleY = ObjectAnimator.ofFloat(view, "scaleY", 1.2f, 1);
animatorSetsuofang.setDuration(SHOW_VIEW_ZOOM_DURATION);
animatorSetsuofang.setInterpolator(new DecelerateInterpolator());
animatorSetsuofang.play(scaleX).with(scaleY);//两个动画同时开始
animatorSetsuofang.start();
}
}, SHOW_VIEW_ZOOM_DURATION);
} catch (Exception e) {
}
}
//设置控件隐藏缩小动画
public static void setViewHideZoomOut(View view) {
try {
AnimatorSet animatorSetsuofang = new AnimatorSet();//组合动画
ObjectAnimator scaleX = ObjectAnimator.ofFloat(view, "scaleX", 1, 0);
ObjectAnimator scaleY = ObjectAnimator.ofFloat(view, "scaleY", 1, 0);
animatorSetsuofang.setDuration(HIDE_VIEW_ZOOM_DURATION);
animatorSetsuofang.setInterpolator(new DecelerateInterpolator());
animatorSetsuofang.play(scaleX).with(scaleY);//两个动画同时开始
animatorSetsuofang.start();
} catch (Exception e) {
}
}
//设置时间显示放大动画
public static void setTimeShowZoomIn(View view) {
try {
ObjectAnimator scaleX = ObjectAnimator.ofFloat(view, "scaleX", 0, 1.4f);
ObjectAnimator scaleY = ObjectAnimator.ofFloat(view, "scaleY", 0, 1.4f);
ObjectAnimator scaleX1 = ObjectAnimator.ofFloat(view, "scaleX", 1.4f, 1);
ObjectAnimator scaleY1 = ObjectAnimator.ofFloat(view, "scaleY", 1.4f, 1);
ObjectAnimator scaleX2 = ObjectAnimator.ofFloat(view, "scaleX", 1, 0);
ObjectAnimator scaleY2 = ObjectAnimator.ofFloat(view, "scaleY", 1, 0);
AnimatorSet animatorSetsuofang = new AnimatorSet();//组合动画
animatorSetsuofang.setDuration(100);
animatorSetsuofang.setInterpolator(new DecelerateInterpolator());
animatorSetsuofang.play(scaleX).with(scaleY);
animatorSetsuofang.start();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (view == null) {
return;
}
AnimatorSet animatorSetsuofang2 = new AnimatorSet();//组合动画
animatorSetsuofang2.setDuration(100);
animatorSetsuofang2.setInterpolator(new DecelerateInterpolator());
animatorSetsuofang2.play(scaleX1).with(scaleY1);
animatorSetsuofang2.start();
}
}, 100);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (view == null) {
return;
}
AnimatorSet animatorSetsuofang3 = new AnimatorSet();//组合动画
animatorSetsuofang3.setDuration(10);
animatorSetsuofang3.setInterpolator(new DecelerateInterpolator());
animatorSetsuofang3.play(scaleX2).with(scaleY2);
animatorSetsuofang3.start();
}
}, 300);
} catch (Exception e) {
}
}
/**
* 设置控件展开特效
*
* @param view
* @param closeFlag
*/
public static void startAnimation(View view, boolean closeFlag) {
try {
//因为CircularReveal动画是api21之后才有的,所以加个判断语句,免得崩溃
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
int cicular_R = view.getHeight() / 2 > view.getWidth() / 2 ? view.getHeight() / 2 : view.getWidth() / 2;
Animator animator = null;
if (!closeFlag) {
animator = ViewAnimationUtils.createCircularReveal(view, (int) view.getWidth() / 2, (int) view.getHeight() / 2, 0, cicular_R);
} else {
animator = ViewAnimationUtils.createCircularReveal(view, (int) view.getWidth() / 2, (int) view.getHeight() / 2, cicular_R, 0);
}
animator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animator) {
}
@Override
public void onAnimationEnd(Animator animator) {
if (closeFlag) {
view.setVisibility(View.GONE);
}
}
@Override
public void onAnimationCancel(Animator animator) {
}
@Override
public void onAnimationRepeat(Animator animator) {
}
});
animator.setDuration(350);
animator.start();
} else {
if (closeFlag) {
view.setVisibility(View.GONE);
}
}
} catch (Exception e) {
}
}
}