Skip to content

Commit 184242c

Browse files
committed
重构 ToastUtils 框架
1 parent 0541c55 commit 184242c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1164
-1117
lines changed

.gitignore

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
*.iml
21
.gradle
3-
/local.properties
4-
/.idea/workspace.xml
5-
/.idea/libraries
6-
.DS_Store
7-
/build
8-
/captures
9-
/.idea
10-
11-
/app/*.pro
2+
.idea
3+
.cxx
4+
.externalNativeBuild
5+
build
6+
captures
127

13-
/library/*.pro
8+
._*
9+
*.iml
10+
.DS_Store
11+
local.properties

README.md

Lines changed: 63 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,35 @@
1-
# 吐司工具类
1+
# 吐司框架
22

3-
> 码云地址:[Gitee](https://gitee.com/getActivity/ToastUtils)
3+
* 码云地址:[Gitee](https://gitee.com/getActivity/ToastUtils)
44

5-
> 博客地址:[只需体验三分钟,你就会跟我一样,爱上这款Toast](https://www.jianshu.com/p/9b174ee2c571)
5+
* 博客地址:[只需体验三分钟,你就会跟我一样,爱上这款 Toast](https://www.jianshu.com/p/9b174ee2c571)
66

7-
> 已投入公司项目多时,没有任何毛病,可胜任任何需求,[点击此处下载Demo](ToastUtils.apk)
8-
9-
> 想了解实现原理的可以点击此链接查看:[ToastUtils](library/src/main/java/com/hjq/toast/ToastUtils.java) 源码
7+
* 已投入公司项目多时,没有任何毛病,可胜任任何需求,[点击此处下载Demo](ToastUtils.apk)
108

119
![](ToastUtils.gif)
1210

13-
#### 本框架意在解决一些常见需求,如果是有一些极端的需求推荐使用 [XToast](https://github.com/getActivity/XToast)
11+
#### 本框架意在解决一些常见需求,如果是有一些特殊的需求推荐配搭 [XToast](https://github.com/getActivity/XToast) 悬浮窗使用
1412

1513
#### 集成步骤
1614

1715
```groovy
16+
android {
17+
// 支持 JDK 1.8
18+
compileOptions {
19+
targetCompatibility JavaVersion.VERSION_1_8
20+
sourceCompatibility JavaVersion.VERSION_1_8
21+
}
22+
}
23+
1824
dependencies {
19-
implementation 'com.hjq:toast:8.8'
25+
// 吐司框架:https://github.com/getActivity/ToastUtils
26+
implementation 'com.hjq:toast:9.0'
2027
}
2128
```
2229

23-
#### 初始化 Toast
30+
#### 初始化
2431

2532
```java
26-
// 在 Application 中初始化
2733
ToastUtils.init(this);
2834
```
2935

@@ -33,82 +39,75 @@ ToastUtils.init(this);
3339
ToastUtils.show("我是吐司");
3440
```
3541

36-
#### 其他 API
42+
#### 取消 Toast
3743

3844
```java
39-
// 设置Toast布局
40-
ToastUtils.setView();
45+
// 取消 Toast 显示
46+
ToastUtils.cancel();
47+
```
4148

42-
// 设置吐司重心
43-
ToastUtils.setGravity();
49+
#### 其他 API
4450

45-
// 获取Toast对象
46-
ToastUtils.getToast();
47-
```
51+
```java
52+
// 设置 Toast 布局及样式
53+
ToastUtils.setStyle(IToastStyle<?> style);
4854

49-
#### 自定义Toast样式
55+
// 设置 Toast 重心和偏移
56+
ToastUtils.setGravity(int gravity, int xOffset, int yOffset);
5057

51-
> 如果对Toast的默认样式不满意,可以在Application初始化样式,具体可参考[ToastBlackStyle](library/src/main/java/com/hjq/toast/style/ToastBlackStyle.java)类的实现
58+
// 设置 Toast 拦截器
59+
ToastUtils.setInterceptor(IToastInterceptor interceptor);
5260

53-
```java
54-
ToastUtils.initStyle(new IToastStyle());
61+
// 设置 Toast 策略
62+
ToastUtils.setStrategy(IToastStrategy strategy);
63+
64+
// 设置 Toast 布局
65+
ToastUtils.setView(int id);
5566
```
5667

5768
#### 框架亮点
5869

59-
* 无需权限:不管有没有授予通知栏权限都不影响吐司的弹出
70+
* 一马当先:首款适配 Android 11 的吐司框架,使用者无需关心适配过程
6071

61-
* 兼容性强:处理原生 Toast 在 Android 7.1 产生崩溃的历史遗留问题
72+
* 无需权限:[不管有没有授予通知栏权限都不影响吐司的弹出](https://www.jianshu.com/p/1d64a5ccbc7c)
73+
74+
* 兼容性强:[处理原生 Toast 在 Android 7.1 产生崩溃的历史遗留问题](https://www.jianshu.com/p/437f473017d6)
6275

6376
* 功能强大:不分主次线程都可以弹出Toast,自动区分资源 id 和 int 类型
6477

6578
* 使用简单:只需传入文本,会自动根据文本长度决定吐司显示的时长
6679

67-
* 性能最佳:单例吐司,整个 Toast 只有一个 TextView,并且通过代码创建
68-
69-
* 体验最优:限制 Toast 短时间内弹出的次数,避免频繁弹出造成不良的用户体验
70-
71-
* 支持多种样式:默认为黑色样式,夜间模式可使用白色样式,还有仿 QQ 吐司样式
72-
73-
* 支持自定义样式:吐司(背景、圆角、重心、偏移),文字(大小、颜色、边距)
74-
75-
* 支持自定义扩展:支持获取 ToastUtils 中的 Toast 对象,支持重新自定义 Toast 布局
80+
* 性能最佳:使用懒加载模式,只在显示时创建 Toast,不占用 Application 启动时间
7681

77-
* 支持全局配置样式:可以在 Application 中初始化 Toast 样式,达到一劳永逸的效果
82+
* 体验最佳:显示下一个 Toast 会取消上一个 Toast 的显示,真正做到即显即示
7883

79-
* 已适配 Android R:Android 11 之后不能弹出自定义样式的 Toast,框架针对这种情况进行了适配
84+
* 全局统一:可以在 Application 中初始化 Toast 样式,达到一劳永逸的效果
8085

8186
#### 关于通知栏权限
8287

83-
> 本框架已经完美解决这个问题(禁用通知栏权限后需要重启应用、重启应用、重启应用才能后生效,请以[Demo](https://raw.githubusercontent.com/getActivity/ToastUtils/master/ToastUtils.apk)测试为主)
88+
* 本框架已经完美解决这个问题,即使没有通知栏权限的情况下也能在前台显示 Toast
8489

85-
> 在开启 APP 的同时关闭通知栏权限的情况极少(测试人员才会这么做),所以仅在 Application 初始化的时候才对没有通知栏权限的情况进行判断和兼容
86-
87-
> 具体解决方案可见:[Toast通知栏权限填坑指南](https://www.jianshu.com/p/1d64a5ccbc7c)
90+
* 具体解决方案参见:[Toast通知栏权限填坑指南](https://www.jianshu.com/p/1d64a5ccbc7c)
8891

8992
![](issue_taobao.gif)
9093

9194
![](issue_utils.gif)
9295

93-
#### ToastUtils 架构图
94-
95-
![](ToastUtils.jpg)
96+
#### 如何替换项目中已有的原生 Toast
9697

97-
#### 如何替换项目中已有的 Toast ?
98-
99-
> 右击项目,Replace in path,勾选 Regex 选项
98+
* 在项目中右击弹出菜单,Replace in path,勾选 Regex 选项,点击替换
10099

101100
```java
102101
Toast\.makeText\([^,]+,\s*(.+{1}),\s*[^,]+\)\.show\(\)
103102
```
104103

105-
> 替换使用
104+
---
106105

107106
```java
108107
ToastUtils.show($1)
109108
```
110109

111-
> 包名替换
110+
* 对导包进行替换
112111

113112
```java
114113
import android.widget.Toast
@@ -120,20 +119,24 @@ import android.widget.Toast
120119
import com.hjq.toast.ToastUtils
121120
```
122121

123-
> 再全局搜索,手动更换一些没有替换成功的
122+
* 再全局搜索,手动更换一些没有替换成功的
124123

125124
```java
126125
Toast.makeText
127126
```
128127

128+
---
129+
130+
```java
131+
new Toast
132+
```
133+
129134
#### 作者的其他开源项目
130135

131-
* 安卓架构[AndroidProject](https://github.com/getActivity/AndroidProject)
136+
* 安卓技术中台:[AndroidProject](https://github.com/getActivity/AndroidProject)
132137

133138
* 网络框架:[EasyHttp](https://github.com/getActivity/EasyHttp)
134139

135-
* 日志框架:[Logcat](https://github.com/getActivity/Logcat)
136-
137140
* 权限框架:[XXPermissions](https://github.com/getActivity/XXPermissions)
138141

139142
* 标题栏框架:[TitleBar](https://github.com/getActivity/TitleBar)
@@ -142,7 +145,15 @@ Toast.makeText
142145

143146
* 悬浮窗框架:[XToast](https://github.com/getActivity/XToast)
144147

145-
#### Android技术讨论Q群:78797078
148+
* Gson 解析容错:[GsonFactory](https://github.com/getActivity/GsonFactory)
149+
150+
* 日志查看框架:[Logcat](https://github.com/getActivity/Logcat)
151+
152+
#### 微信公众号:Android轮子哥
153+
154+
![](https://raw.githubusercontent.com/getActivity/Donate/master/picture/official_ccount.png)
155+
156+
#### Android 技术分享 QQ 群:78797078
146157

147158
#### 如果您觉得我的开源库帮你节省了大量的开发时间,请扫描下方的二维码随意打赏,要是能打赏个 10.24 :monkey_face:就太:thumbsup:了。您的支持将鼓励我继续创作:octocat:
148159

ToastUtils.apk

-1.13 MB
Binary file not shown.

ToastUtils.jpg

-28.1 KB
Binary file not shown.

app/.gitignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

app/build.gradle

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ android {
55

66
defaultConfig {
77
applicationId "com.hjq.toast.demo"
8-
minSdkVersion 14
8+
minSdkVersion 16
99
targetSdkVersion 30
10-
versionCode 88
11-
versionName "8.8"
10+
versionCode 90
11+
versionName "9.0"
1212
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
1313
}
1414
buildTypes {
@@ -17,17 +17,24 @@ android {
1717
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
1818
}
1919
}
20+
21+
// 使用 JDK 1.8
22+
compileOptions {
23+
targetCompatibility JavaVersion.VERSION_1_8
24+
sourceCompatibility JavaVersion.VERSION_1_8
25+
}
2026
}
2127

2228
dependencies {
2329
implementation fileTree(include: ['*.jar'], dir: 'libs')
2430
implementation project(':library')
25-
implementation 'androidx.appcompat:appcompat:1.3.0-alpha02'
26-
implementation 'com.google.android.material:material:1.3.0-alpha02'
31+
implementation 'androidx.appcompat:appcompat:1.2.0'
2732
// 标题栏:https://github.com/getActivity/TitleBar
28-
implementation 'com.hjq:titlebar:8.0'
33+
implementation 'com.hjq:titlebar:8.5'
2934
// 悬浮窗:https://github.com/getActivity/XToast
30-
implementation 'com.hjq:xtoast:5.5'
35+
implementation 'com.hjq:xtoast:6.9'
3136
// 内存泄漏捕捉:https://github.com/square/leakcanary
32-
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.4'
37+
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.7'
38+
// 权限请求框架:https://github.com/getActivity/XXPermissions
39+
implementation 'com.hjq:xxpermissions:10.6'
3340
}

app/proguard-rules.pro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-keep class com.hjq.toast.** {*;}

0 commit comments

Comments
 (0)