Skip to content

Commit

Permalink
优化Toast线程队列数量,优化Toast延迟处理,优化框架部分源码
Browse files Browse the repository at this point in the history
  • Loading branch information
880634 committed Feb 19, 2019
1 parent 120afe2 commit 87b06cd
Show file tree
Hide file tree
Showing 17 changed files with 123 additions and 96 deletions.
41 changes: 31 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,44 @@
![](ToastUtils.gif)

#### 本框架意在解决一些常见需求,如果是有一些极端的需求推荐使用 [XToast](https://github.com/getActivity/XToast)

#### 集成步骤

dependencies {
implementation 'com.hjq:toast:5.2'
implementation 'com.hjq:toast:5.5'
}

#### 初始化Toast

// 在Application中初始化
// 在 Application 中初始化
ToastUtils.init(this);

#### 显示Toast
#### 显示 Toast

ToastUtils.show("我是吐司");

#### 获取Toast对象
#### 其他 API

ToastUtils.getToast();
// 设置Toast布局
ToastUtils.setView();

#### 设置Toast布局
// 设置吐司重心
ToastUtils.setGravity();

ToastUtils.setView();
// 获取Toast对象
ToastUtils.getToast();

#### 自定义Toast样式

> 如果对Toast的默认样式不满意,可以在Application初始化样式,具体可参考[ToastBlackStyle](https://github.com/getActivity/ToastUtils/blob/master/library/src/main/java/com/hjq/toast/style/ToastBlackStyle.java)类的实现
ToastUtils.initStyle(new IToastStyle());

#### 混淆规则

-keep class com.hjq.toast.** {*;}

#### 框架亮点

* 无需权限:不管有没有授予通知栏权限都不影响吐司的弹出
Expand Down Expand Up @@ -69,18 +78,30 @@

![](issue_utils.gif)

#### ToastUtils 架构图

![](ToastUtils.jpg)

#### 作者的其他开源项目

* 架构工程:[AndroidProject](https://github.com/getActivity/AndroidProject)

* 权限框架:[XXPermissions](https://github.com/getActivity/XXPermissions)

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

* 标题栏框架:[TitleBar](https://github.com/getActivity/TitleBar)

#### Android技术讨论Q群:78797078

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

![](pay_ali.png) ![](pay_wechat.png)

![](pay_bonus.png)

## License

```text
Copyright 2018 Huang Jinqun
Copyright 2018 Huang JinQun
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
Binary file modified ToastUtils.apk
Binary file not shown.
Binary file added ToastUtils.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
applicationId "com.hjq.toast.demo"
minSdkVersion 14
targetSdkVersion 26
versionCode 52
versionName "5.2"
versionCode 55
versionName "5.5"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
11 changes: 7 additions & 4 deletions app/src/main/java/com/hjq/toast/demo/ToastActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_toast);
}

public void show1(final View v) {
ToastUtils.show("我是一个普通的吐司");
public void show1(View v) {
for (int i = 0; i < 3; i++) {
ToastUtils.show("我是第" + (i + 1) + "个吐司");
}
}

public void show2(View v) {
new Thread(new Runnable() {

@Override
public void run() {
ToastUtils.show("我是子线程中弹出的吐司");
Expand All @@ -41,12 +44,12 @@ public void run() {

public void show3(View v) {
ToastUtils.initStyle(new ToastWhiteStyle());
ToastUtils.show("动态切换吐司样式成功");
ToastUtils.show("动态切换白色吐司样式成功");
}

public void show4(View v) {
ToastUtils.initStyle(new ToastBlackStyle());
ToastUtils.show("动态切换吐司样式成功");
ToastUtils.show("动态切换黑色吐司样式成功");
}

public void show5(View v) {
Expand Down
16 changes: 9 additions & 7 deletions app/src/main/res/layout/activity_toast.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,56 +28,58 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:onClick="show1"
android:text="显示一个普通的吐司" />
android:text="连续显示三个不同吐司" />

<Button
android:layout_marginTop="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:onClick="show2"
android:text="在子线程中显示吐司" />

<Button
android:layout_marginTop="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:onClick="show3"
android:text="切换吐司的样式为白色" />

<Button
android:layout_marginTop="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:onClick="show4"
android:text="切换吐司的样式为黑色" />

<Button
android:layout_marginTop="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:onClick="show5"
android:text="切换吐司的样式为QQ" />

<Button
android:layout_marginTop="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:onClick="show6"
android:text="切换吐司的样式为支付宝" />

<Button
android:layout_marginTop="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:onClick="show7"
android:text="点我自定义Toast布局" />

</LinearLayout>

</FrameLayout>
Expand Down
Binary file removed app/src/main/res/mipmap-mdpi/ic_launcher.png
Binary file not shown.
Binary file removed app/src/main/res/mipmap-mdpi/ic_launcher_round.png
Binary file not shown.
7 changes: 4 additions & 3 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@ android {
resourcePrefix "toast_"

defaultConfig {
minSdkVersion 3
targetSdkVersion 26
versionCode 52
versionName "5.2"
versionCode 55
versionName "5.5"
}
}

publish {
userOrg = 'getactivity'//填写bintray用户名,注意大小写
groupId = 'com.hjq'//定义的maven group id最终引用形式
artifactId = 'toast'//maven的artifact id
version = '5.2'//maven 上发布版本号
version = '5.5'//maven 上发布版本号
description = 'This is a very functional Toast'//描述,自己定义
website = "https://github.com/getActivity/ToastUtils"//项目在github中的地址
}
Expand Down
26 changes: 17 additions & 9 deletions library/src/main/java/com/hjq/toast/BaseToast.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,32 @@ class BaseToast extends Toast {
@Override
public void setView(View view) {
super.setView(view);
mMessageView = getMessageView(view);
}

@Override
public void setText(CharSequence s) {
mMessageView.setText(s);
}

/**
* 智能获取用于显示消息的 TextView
*/
private static TextView getMessageView(View view) {
TextView messageView;
if (view instanceof TextView) {
mMessageView = (TextView) view; return;
messageView = (TextView) view; return messageView;
} else if (view.findViewById(android.R.id.message) instanceof TextView) {
mMessageView = ((TextView) view.findViewById(android.R.id.message)); return;
messageView = ((TextView) view.findViewById(android.R.id.message)); return messageView;
} else if (view instanceof ViewGroup) {
if ((mMessageView = findTextView((ViewGroup) view)) != null) return;
if ((messageView = findTextView((ViewGroup) view)) != null) return messageView;
}
// 如果设置的布局没有包含一个 TextView 则抛出异常,必须要包含一个 TextView 作为 Message View
throw new IllegalArgumentException("The layout must contain a TextView");
}

@Override
public void setText(CharSequence s) {
mMessageView.setText(s);
}

/**
* 递归获取ViewGroup中的TextView对象
* 递归获取 ViewGroup 中的 TextView 对象
*/
private static TextView findTextView(ViewGroup group) {
for (int i = 0; i < group.getChildCount(); i++) {
Expand Down
5 changes: 3 additions & 2 deletions library/src/main/java/com/hjq/toast/SafeToast.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ final class SafeToast extends BaseToast {
field_handler.setAccessible(true);

Handler handler = (Handler) field_handler.get(mTN);
field_handler.set(mTN, new SafeHandler(handler));
field_handler.set(mTN, new SafeHandler(handler)); // 偷梁换柱

} catch (Exception ignored) {}
}
Expand All @@ -55,7 +55,8 @@ public void handleMessage(Message msg) {
*/
mHandler.handleMessage(msg);
} catch (WindowManager.BadTokenException ignored) {
// android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@94ae84f is not valid; is your activity running?
// android.view.WindowManager$BadTokenException:
// Unable to add window -- token android.os.BinderProxy@94ae84f is not valid; is your activity running?
}
}

Expand Down
2 changes: 1 addition & 1 deletion library/src/main/java/com/hjq/toast/SupportToast.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* author : Android 轮子哥
* github : https://github.com/getActivity/ToastUtils
* time : 2018/11/02
* desc : 不需要通知栏权限的 Toast
* desc : Toast 无通知栏权限兼容
*/
final class SupportToast extends BaseToast {

Expand Down
9 changes: 5 additions & 4 deletions library/src/main/java/com/hjq/toast/ToastHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ final class ToastHandler extends Handler {
private static final int TYPE_CONTINUE = 2; // 继续显示
private static final int TYPE_CANCEL = 3; // 取消显示

// 最大吐司的容量
private static final int MAX_TOAST_CAPACITY = 5;
// 队列最大容量
private static final int MAX_TOAST_CAPACITY = 3;

// 吐司队列
private volatile Queue<CharSequence> mQueue = new ArrayBlockingQueue<>(MAX_TOAST_CAPACITY);
private volatile Queue<CharSequence> mQueue;

// 当前是否正在执行显示操作
private volatile boolean isShow;
Expand All @@ -38,6 +38,7 @@ final class ToastHandler extends Handler {
ToastHandler(Toast toast) {
super(Looper.getMainLooper());
mToast = toast;
mQueue = new ArrayBlockingQueue<>(MAX_TOAST_CAPACITY);
}

void add(CharSequence s) {
Expand Down Expand Up @@ -75,7 +76,7 @@ public void handleMessage(Message msg) {
mToast.setText(text);
mToast.show();
// 等这个 Toast 显示完后再继续显示
sendEmptyMessageDelayed(TYPE_CONTINUE, getToastDuration(text) + 1000);
sendEmptyMessageDelayed(TYPE_CONTINUE, getToastDuration(text));
}else {
isShow = false;
}
Expand Down
7 changes: 5 additions & 2 deletions library/src/main/java/com/hjq/toast/ToastHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
final class ToastHelper extends Handler {

private static final String TOAST = "Toast";

// 当前的吐司对象
private final Toast mToast;

Expand All @@ -25,6 +27,7 @@ final class ToastHelper extends Handler {
// 当前应用的包名
private final String mPackageName;

// 当前是否已经显示
private boolean isShow;

ToastHelper(Toast toast, Application application) {
Expand Down Expand Up @@ -75,7 +78,7 @@ void show() {
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
params.format = PixelFormat.TRANSLUCENT;
params.windowAnimations = android.R.style.Animation_Toast;
params.setTitle(Toast.class.getSimpleName());
params.setTitle(TOAST);
params.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
Expand All @@ -93,7 +96,7 @@ void show() {
isShow = true;
// 添加一个移除吐司的任务
sendEmptyMessageDelayed(0, mToast.getDuration() == Toast.LENGTH_LONG ? ToastHandler.LONG_DURATION_TIMEOUT : ToastHandler.SHORT_DURATION_TIMEOUT);
}catch (NullPointerException | IllegalStateException | WindowManager.BadTokenException ignored) {}
} catch (NullPointerException | IllegalStateException | WindowManager.BadTokenException ignored) {}
}
}

Expand Down
Loading

0 comments on commit 87b06cd

Please sign in to comment.