Skip to content

Commit

Permalink
新增自定义吐司使用案例以及算法,优化吐司多次点击只显示最后一次,摒弃使用Looper而使用Handler来处理来自子线程的吐司以及管理吐司…
Browse files Browse the repository at this point in the history
…显示队列
  • Loading branch information
880634 committed Oct 23, 2018
1 parent f871400 commit c556532
Show file tree
Hide file tree
Showing 17 changed files with 221 additions and 81 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#### 集成步骤

dependencies {
implementation 'com.hjq:toast:2.2'
implementation 'com.hjq:toast:2.5'
}

#### 初始化Toast
Expand All @@ -33,7 +33,7 @@

#### 自定义Toast样式

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

Expand Down
Binary file modified ToastUtils.apk
Binary file not shown.
Binary file modified ToastUtils.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 11 additions & 3 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 22
versionName "2.2"
versionCode 25
versionName "2.5"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand All @@ -23,5 +23,13 @@ dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation project(':library')
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.hjq:titlebar:2.6'
implementation 'com.hjq:titlebar:3.2'

// implementation 'com.blankj:utilcode:1.21.0'
// implementation 'me.drakeet.support:toastcompat:1.1.0'
// implementation 'com.github.GrenderG:Toasty:1.3.0'
// implementation 'com.github.johnpersano:supertoasts:2.0'
// implementation 'com.sdsmdg.tastytoast:tastytoast:0.1.1'
// implementation 'com.muddzdev:styleabletoast:2.2.0'
// implementation 'com.github.Blincheng:EToast2:v2.1.1'
}
13 changes: 9 additions & 4 deletions app/src/main/java/com/hjq/toast/demo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.hjq.toast.ToastBlackStyle;
import com.hjq.toast.ToastQQStyle;
import com.hjq.toast.ToastUtils;
import com.hjq.toast.ToastWhiteStyle;
import com.hjq.toast.style.ToastBlackStyle;
import com.hjq.toast.style.ToastQQStyle;
import com.hjq.toast.style.ToastWhiteStyle;

public class MainActivity extends AppCompatActivity {

Expand All @@ -17,7 +17,7 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
}

public void show1(View v) {
public void show1(final View v) {
ToastUtils.show("我是吐司");
}

Expand Down Expand Up @@ -46,6 +46,11 @@ public void show5(View v) {
}

public void show6(View v) {
ToastUtils.setView(this, R.layout.toast_custom_view);
ToastUtils.show("我是自定义Toast");
}

public void show7(View v) {
ToastUtils.show(this);
}
}
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/shape_gradient.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="180"
android:endColor="#387cfd"
android:startColor="#26c4fd"
android:type="linear" />

<corners android:radius="10dp" />
</shape>
8 changes: 8 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:onClick="show6"
android:text="点我自定义Toast布局" />

<Button
android:layout_marginTop="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:onClick="show7"
android:text="打印当前Activity对象" />
</LinearLayout>

Expand Down
22 changes: 22 additions & 0 deletions app/src/main/res/layout/toast_custom_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/shape_gradient"
android:gravity="center"
android:orientation="horizontal"
android:padding="10dp">

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher" />

<TextView
android:id="@id/toast_main_text_view_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:textColor="#FFFFFFFF"
android:textSize="20sp" />
</LinearLayout>
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<resources>
<string name="app_name">ToastUtilsDemo</string>
<string name="app_name">ToastUtils</string>
</resources>
8 changes: 5 additions & 3 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@ apply plugin: 'com.novoda.bintray-release'
android {
compileSdkVersion 26

resourcePrefix "toast_"

defaultConfig {
targetSdkVersion 26
versionCode 22
versionName "2.2"
versionCode 25
versionName "2.5"
}
}

publish {
userOrg = 'getactivity'//填写bintray用户名,注意大小写
groupId = 'com.hjq'//定义的maven group id最终引用形式
artifactId = 'toast'//maven的artifact id
version = '2.2'//maven 上发布版本号
version = '2.5'//maven 上发布版本号
description = 'This is a very functional Toast'//描述,自己定义
website = "https://github.com/getActivity/ToastUtils"//项目在github中的地址
}
Expand Down
25 changes: 13 additions & 12 deletions library/src/main/java/com/hjq/toast/IToastStyle.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,20 @@
*/
public interface IToastStyle {

int getGravity();//吐司的重心
int getXOffset();//X轴偏移
int getYOffset();//Y轴偏移
int getGravity(); // 吐司的重心
int getXOffset(); // X轴偏移
int getYOffset(); // Y轴偏移
int getZ(); // 吐司Z轴坐标

int getCornerRadius();//圆角大小
int getBackgroundColor();//背景颜色
int getCornerRadius(); // 圆角大小
int getBackgroundColor(); // 背景颜色

int getTextColor();//文本颜色
float getTextSize();//文本大小
int getMaxLines();//最大行数
int getTextColor(); // 文本颜色
float getTextSize(); // 文本大小
int getMaxLines(); // 最大行数

int getPaddingLeft();//左边内边距
int getPaddingTop();//顶部内边距
int getPaddingRight();//右边内边距
int getPaddingBottom();//底部内边距
int getPaddingLeft(); // 左边内边距
int getPaddingTop(); // 顶部内边距
int getPaddingRight(); // 右边内边距
int getPaddingBottom(); // 底部内边距
}
89 changes: 55 additions & 34 deletions library/src/main/java/com/hjq/toast/ToastUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
import android.content.res.Resources;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;
import android.os.Looper;
import android.util.TypedValue;
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 : HJQ
* github : https://github.com/getActivity/ToastUtils
Expand All @@ -29,35 +32,39 @@ public final class ToastUtils {
* @param context 应用的上下文
*/
public static void init(Context context) {
//检查默认样式是否为空,如果是就创建一个默认样式
// 检查默认样式是否为空,如果是就创建一个默认样式
if (sDefaultStyle == null) {
sDefaultStyle = new ToastBlackStyle();
}

//如果这个上下文不是全局的上下文,就自动换成全局的上下文
// 如果这个上下文不是全局的上下文,就自动换成全局的上下文
if (context != context.getApplicationContext()) {
context = context.getApplicationContext();
}

GradientDrawable drawable = new GradientDrawable();
drawable.setColor(sDefaultStyle.getBackgroundColor());
drawable.setCornerRadius(DimensUtils.dp2px(context, sDefaultStyle.getCornerRadius()));
drawable.setColor(sDefaultStyle.getBackgroundColor()); // 设置背景色
drawable.setCornerRadius(DimensUtils.dp2px(context, sDefaultStyle.getCornerRadius())); // 设置圆角

TextView textView = new TextView(context);
textView.setTextColor(sDefaultStyle.getTextColor());
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, DimensUtils.sp2px(context, sDefaultStyle.getTextSize()));
textView.setPadding(DimensUtils.dp2px(context, sDefaultStyle.getPaddingLeft()), DimensUtils.dp2px(context, sDefaultStyle.getPaddingTop()),
DimensUtils.dp2px(context, sDefaultStyle.getPaddingRight()), DimensUtils.dp2px(context, sDefaultStyle.getPaddingBottom()));
textView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
//setBackground API版本兼容
// setBackground API版本兼容
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
textView.setBackground(drawable);
}else {
textView.setBackgroundDrawable(drawable);
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
textView.setZ(sDefaultStyle.getZ()); // 设置 Z 轴阴影
}

if (sDefaultStyle.getMaxLines() > 0) {
textView.setMaxLines(sDefaultStyle.getMaxLines());
textView.setMaxLines(sDefaultStyle.getMaxLines()); // 设置最大显示行数
}

sToast = new XToast(context);
Expand All @@ -82,16 +89,13 @@ public static void show(Object object) {
*/
public static void show(int id) {

//吐司工具类还没有被初始化,必须要先调用init方法进行初始化
if (sToast == null) {
throw new IllegalStateException("ToastUtils has not been initialized");
}
checkToastState();

try {
//如果这是一个资源id
// 如果这是一个资源id
show(sToast.getView().getContext().getResources().getText(id));
} catch (Resources.NotFoundException ignored) {
//如果这是一个int类型
// 如果这是一个int类型
show(String.valueOf(id));
}
}
Expand All @@ -103,33 +107,27 @@ public static void show(int id) {
*/
public static void show(CharSequence text) {

//吐司工具类还没有被初始化,必须要先调用init方法进行初始化
if (sToast == null) {
throw new IllegalStateException("ToastUtils has not been initialized");
}
checkToastState();

if (text == null || text.equals("")) return;

//如果显示的文字超过了10个就显示长吐司,否则显示短吐司
// 如果显示的文字超过了10个就显示长吐司,否则显示短吐司
if (text.length() > 20) {
sToast.setDuration(Toast.LENGTH_LONG);
} else {
sToast.setDuration(Toast.LENGTH_SHORT);
}

try {
//判断是否在主线程中执行
if (Looper.myLooper() == Looper.getMainLooper()) {
sToast.setText(text);
sToast.show();
}else {
//在子线程中显示处理
Looper.prepare();
sToast.setText(text);
sToast.show();
Looper.loop();
}
} catch (Exception ignored) {}
sToast.setText(text);
sToast.show();
}

/**
* 取消吐司的显示
*/
public void cancel() {
checkToastState();
sToast.cancel();
}

/**
Expand All @@ -140,19 +138,32 @@ public static Toast getToast() {
}

/**
* 给当前Toast设置新的布局
* 给当前Toast设置新的布局,具体实现可看{@link XToast#setView(View)}
*/
public static void setView(Context context, int layoutId) {
if (context != context.getApplicationContext()) {
context = context.getApplicationContext();
}
setView(View.inflate(context, layoutId, null));
}

public static void setView(View view) {

checkToastState();

if (view == null) {
throw new IllegalArgumentException("Views cannot be empty");
}
sToast.setView(view);
}

/**
* 初始化Toast样式
* 统一全局的Toast样式,建议在{@link android.app.Application#onCreate()}中初始化
*
* @param style 样式实现类
* @param style 样式实现类,框架已经实现三种不同的样式
* 黑色样式:{@link ToastBlackStyle}
* 白色样式:{@link ToastWhiteStyle}
* 仿QQ样式:{@link ToastQQStyle}
*/
public static void initStyle(IToastStyle style) {
ToastUtils.sDefaultStyle = style;
Expand All @@ -164,4 +175,14 @@ public static void initStyle(IToastStyle style) {
init(sToast.getView().getContext().getApplicationContext());
}
}

/**
* 检查吐司状态,如果未初始化请先调用{@link ToastUtils#init(Context)}
*/
private static void checkToastState() {
//吐司工具类还没有被初始化,必须要先调用init方法进行初始化
if (sToast == null) {
throw new IllegalStateException("ToastUtils has not been initialized");
}
}
}
Loading

0 comments on commit c556532

Please sign in to comment.