forked from getActivity/Toaster
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathToastUtils.java
More file actions
317 lines (270 loc) · 9.47 KB
/
Copy pathToastUtils.java
File metadata and controls
317 lines (270 loc) · 9.47 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
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
package com.hjq.toast;
import android.app.Activity;
import android.app.Application;
import android.app.Service;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import com.hjq.toast.style.ToastBlackStyle;
import com.hjq.toast.style.ToastQQStyle;
import com.hjq.toast.style.ToastWhiteStyle;
/**
* author : Android 轮子哥
* github : https://github.com/getActivity/ToastUtils
* time : 2018/09/01
* desc : Toast 工具类
*/
public final class ToastUtils {
/** Toast 拦截器 */
private static IToastInterceptor sInterceptor;
/** Toast 处理策略 */
private static IToastStrategy sStrategy;
/** Toast 单例对象 */
private static Toast sToast;
/**
* 不允许被外部实例化
*/
private ToastUtils() {}
/**
* 初始化 Toast,需要在 Application.create 中初始化
*
* @param application 应用的上下文
*/
public static void init(Application application) {
init(application, new ToastBlackStyle(application));
}
/**
* 初始化 Toast 及样式
*/
public static void init(Application application, IToastStyle style) {
checkNullPointer(application);
// 初始化 Toast 拦截器
if (sInterceptor == null) {
setToastInterceptor(new ToastInterceptor());
}
// 初始化 Toast 显示处理器
if (sStrategy == null) {
setToastStrategy(new ToastStrategy());
}
// 创建 Toast 对象
setToast(sStrategy.create(application));
// 设置 Toast 视图
setView(createTextView(application, style));
// 设置 Toast 重心
setGravity(style.getGravity(), style.getXOffset(), style.getYOffset());
}
/**
* 显示一个对象的吐司
*
* @param object 对象
*/
public static void show(Object object) {
show(object != null ? object.toString() : "null");
}
/**
* 显示一个吐司
*
* @param id 如果传入的是正确的 string id 就显示对应字符串
* 如果不是则显示一个整数的string
*/
public static void show(int id) {
checkToastState();
try {
// 如果这是一个资源 id
show(getContext().getResources().getText(id));
} catch (Resources.NotFoundException ignored) {
// 如果这是一个 int 整数
show(String.valueOf(id));
}
}
/**
* 显示一个吐司
*
* @param text 需要显示的文本
*/
public static synchronized void show(CharSequence text) {
checkToastState();
if (sInterceptor.intercept(sToast, text)) {
return;
}
sStrategy.show(text);
}
/**
* 取消吐司的显示
*/
public static synchronized void cancel() {
checkToastState();
sStrategy.cancel();
}
/**
* 设置吐司的位置
*
* @param gravity 重心
* @param xOffset x轴偏移
* @param yOffset y轴偏移
*/
public static void setGravity(int gravity, int xOffset, int yOffset) {
checkToastState();
// 适配 Android 4.2 新特性,布局反方向(开发者选项 - 强制使用从右到左的布局方向)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
gravity = Gravity.getAbsoluteGravity(gravity, getContext().getResources().getConfiguration().getLayoutDirection());
}
sToast.setGravity(gravity, xOffset, yOffset);
}
/**
* 给当前Toast设置新的布局,具体实现可看{@link NormalToast#setView(View)}
*/
public static void setView(int id) {
checkToastState();
setView(View.inflate(getContext(), id, null));
}
public static void setView(View view) {
checkToastState();
// 这个 View 不能为空
checkNullPointer(view);
// 当前必须用 Application 的上下文创建的 View,否则可能会导致内存泄露
Context context = view.getContext();
if (context instanceof Activity || context instanceof Service) {
throw new IllegalArgumentException("The view must be initialized using the context of the application");
}
// 如果吐司已经创建,就重新初始化吐司
if (sToast != null) {
// 取消原有吐司的显示
sToast.cancel();
sToast.setView(view);
}
}
/**
* 获取当前 Toast 的视图
*/
@SuppressWarnings("unchecked")
public static <V extends View> V getView() {
checkToastState();
return (V) sToast.getView();
}
/**
* 初始化全局的Toast样式
*
* @param style 样式实现类,框架已经实现三种不同的样式
* 黑色样式:{@link ToastBlackStyle}
* 白色样式:{@link ToastWhiteStyle}
* 仿QQ样式:{@link ToastQQStyle}
*/
public static void initStyle(IToastStyle style) {
checkNullPointer(style);
// 如果吐司已经创建,就重新初始化吐司
if (sToast != null) {
// 取消原有吐司的显示
sToast.cancel();
sToast.setView(createTextView(getContext(), style));
sToast.setGravity(style.getGravity(), style.getXOffset(), style.getYOffset());
}
}
/**
* 设置当前Toast对象
*/
public static void setToast(Toast toast) {
checkNullPointer(toast);
if (sToast != null && toast.getView() == null) {
// 移花接木
toast.setView(sToast.getView());
toast.setGravity(sToast.getGravity(), sToast.getXOffset(), sToast.getYOffset());
toast.setMargin(sToast.getHorizontalMargin(), sToast.getVerticalMargin());
}
sToast = toast;
if (sStrategy != null) {
sStrategy.bind(sToast);
}
}
/**
* 设置 Toast 显示策略
*/
public static void setToastStrategy(IToastStrategy strategy) {
checkNullPointer(strategy);
sStrategy = strategy;
if (sToast != null) {
sStrategy.bind(sToast);
}
}
/**
* 设置 Toast 拦截器(可以根据显示的内容决定是否拦截这个Toast)
* 场景:打印 Toast 内容日志、根据 Toast 内容是否包含敏感字来动态切换其他方式显示(这里可以使用我的另外一套框架 XToast)
*/
public static void setToastInterceptor(IToastInterceptor interceptor) {
checkNullPointer(interceptor);
sInterceptor = interceptor;
}
/**
* 获取当前Toast对象
*/
public static Toast getToast() {
return sToast;
}
/**
* 检查吐司状态,如果未初始化请先调用{@link ToastUtils#init(Application)}
*/
private static void checkToastState() {
// 吐司工具类还没有被初始化,必须要先调用init方法进行初始化
if (sToast == null) {
throw new IllegalStateException("ToastUtils has not been initialized");
}
}
/**
* 检查对象是否为空
*/
private static void checkNullPointer(Object object) {
if (object == null) {
throw new NullPointerException("are you ok?");
}
}
/**
* 根据样式生成默认的 TextView 对象
*/
private static TextView createTextView(Context context, IToastStyle style) {
TextView textView = new TextView(context);
textView.setId(android.R.id.message);
textView.setTextColor(style.getTextColor());
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, style.getTextSize());
// 适配布局反方向特性
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
textView.setPaddingRelative(style.getPaddingStart(), style.getPaddingTop(), style.getPaddingEnd(), style.getPaddingBottom());
} else {
textView.setPadding(style.getPaddingStart(), style.getPaddingTop(), style.getPaddingEnd(), style.getPaddingBottom());
}
textView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
GradientDrawable drawable = new GradientDrawable();
// 设置背景色
drawable.setColor(style.getBackgroundColor());
// 设置圆角大小
drawable.setCornerRadius(style.getCornerRadius());
// setBackground API 版本兼容
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
textView.setBackground(drawable);
} else {
textView.setBackgroundDrawable(drawable);
}
// 设置 Z 轴阴影
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
textView.setZ(style.getZ());
}
// 设置最大显示行数
if (style.getMaxLines() > 0) {
textView.setMaxLines(style.getMaxLines());
}
return textView;
}
/**
* 获取上下文对象
*/
private static Context getContext() {
checkToastState();
return sToast.getView().getContext();
}
}