Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
mmm-rain
AndroidUtilCode
提交
b75eec3e
A
AndroidUtilCode
项目概览
mmm-rain
/
AndroidUtilCode
与 Fork 源项目一致
从无法访问的项目Fork
通知
4
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
A
AndroidUtilCode
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
b75eec3e
编写于
9月 02, 2017
作者:
B
Blankj
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
see 09/02 log
上级
814491ee
变更
14
隐藏空白更改
内联
并排
Showing
14 changed file
with
111 addition
and
196 deletion
+111
-196
README-CN.md
README-CN.md
+2
-2
README.md
README.md
+2
-4
app/src/main/java/com/blankj/androidutilcode/core/toast/CustomToast.java
...va/com/blankj/androidutilcode/core/toast/CustomToast.java
+2
-2
app/src/main/java/com/blankj/androidutilcode/core/toast/ToastActivity.java
.../com/blankj/androidutilcode/core/toast/ToastActivity.java
+15
-6
app/src/main/res/core/drawable/shape_round_rect.xml
app/src/main/res/core/drawable/shape_round_rect.xml
+5
-0
app/src/main/res/core/layout/activity_toast.xml
app/src/main/res/core/layout/activity_toast.xml
+9
-2
app/src/main/res/core/values/strings.xml
app/src/main/res/core/values/strings.xml
+3
-1
build.gradle
build.gradle
+2
-2
update_log.md
update_log.md
+1
-0
utilcode/build.gradle
utilcode/build.gradle
+1
-1
utilcode/src/main/java/com/blankj/utilcode/util/IntentUtils.java
...e/src/main/java/com/blankj/utilcode/util/IntentUtils.java
+1
-1
utilcode/src/main/java/com/blankj/utilcode/util/LogUtils.java
...code/src/main/java/com/blankj/utilcode/util/LogUtils.java
+4
-10
utilcode/src/main/java/com/blankj/utilcode/util/ToastUtils.java
...de/src/main/java/com/blankj/utilcode/util/ToastUtils.java
+64
-164
utilcode/src/main/java/com/blankj/utilcode/util/Utils.java
utilcode/src/main/java/com/blankj/utilcode/util/Utils.java
+0
-1
未找到文件。
README-CN.md
浏览文件 @
b75eec3e
...
...
@@ -616,7 +616,7 @@ getEntries : 获取压缩文件中的文件对象
Gradle:
```
groovy
compile
'com.blankj:utilcode:1.8.
5
'
compile
'com.blankj:utilcode:1.8.
6
'
```
...
...
@@ -639,7 +639,7 @@ Utils.init(context);
[
logo
]:
https://raw.githubusercontent.com/Blankj/AndroidUtilCode/master/art/logo.png
[
aucsvg
]:
https://img.shields.io/badge/AndroidUtilCode-v1.8.
5
-brightgreen.svg
[
aucsvg
]:
https://img.shields.io/badge/AndroidUtilCode-v1.8.
6
-brightgreen.svg
[
auc
]:
https://github.com/Blankj/AndroidUtilCode
[
apisvg
]:
https://img.shields.io/badge/API-14+-brightgreen.svg
...
...
README.md
浏览文件 @
b75eec3e
...
...
@@ -575,8 +575,6 @@ getZodiac
*
### About Toast→
[
ToastUtils.java
][
toast.java
]
→
[
Demo
][
toast.demo
]
```
setGravity
setView
getView
setBgColor
setBgResource
setMessageColor
...
...
@@ -618,7 +616,7 @@ getEntries
Gradle:
```
groovy
compile
'com.blankj:utilcode:1.8.
5
'
compile
'com.blankj:utilcode:1.8.
6
'
```
...
...
@@ -641,7 +639,7 @@ Utils.init(context);
[
logo
]:
https://raw.githubusercontent.com/Blankj/AndroidUtilCode/master/art/logo.png
[
aucsvg
]:
https://img.shields.io/badge/AndroidUtilCode-v1.8.
5
-brightgreen.svg
[
aucsvg
]:
https://img.shields.io/badge/AndroidUtilCode-v1.8.
6
-brightgreen.svg
[
auc
]:
https://github.com/Blankj/AndroidUtilCode
[
apisvg
]:
https://img.shields.io/badge/API-14+-brightgreen.svg
...
...
app/src/main/java/com/blankj/androidutilcode/core/toast/CustomToast.java
浏览文件 @
b75eec3e
...
...
@@ -15,9 +15,9 @@ import com.blankj.utilcode.util.ToastUtils;
* desc :
* </pre>
*/
public
class
My
Toast
{
public
class
Custom
Toast
{
public
static
void
show
MyToast
(
@NonNull
final
String
message
){
public
static
void
show
(
@NonNull
final
String
message
){
View
toastView
=
ToastUtils
.
showCustomLongSafe
(
R
.
layout
.
toast_custom
);
TextView
tvMessage
=
(
TextView
)
toastView
.
findViewById
(
R
.
id
.
tv_toast_message
);
tvMessage
.
setText
(
message
);
...
...
app/src/main/java/com/blankj/androidutilcode/core/toast/ToastActivity.java
浏览文件 @
b75eec3e
...
...
@@ -4,6 +4,7 @@ import android.content.Context;
import
android.content.Intent
;
import
android.graphics.Color
;
import
android.os.Bundle
;
import
android.support.v4.content.ContextCompat
;
import
android.view.Gravity
;
import
android.view.View
;
...
...
@@ -22,6 +23,8 @@ import com.blankj.utilcode.util.ToastUtils;
*/
public
class
ToastActivity
extends
BaseBackActivity
{
View
toastView
;
public
static
void
start
(
Context
context
)
{
Intent
starter
=
new
Intent
(
context
,
ToastActivity
.
class
);
context
.
startActivity
(
starter
);
...
...
@@ -46,11 +49,13 @@ public class ToastActivity extends BaseBackActivity {
findViewById
(
R
.
id
.
btn_show_long_toast_safe
).
setOnClickListener
(
this
);
findViewById
(
R
.
id
.
btn_show_long_toast
).
setOnClickListener
(
this
);
findViewById
(
R
.
id
.
btn_show_green_font
).
setOnClickListener
(
this
);
findViewById
(
R
.
id
.
btn_show_custom_bg
).
setOnClickListener
(
this
);
findViewById
(
R
.
id
.
btn_show_bg_color
).
setOnClickListener
(
this
);
findViewById
(
R
.
id
.
btn_show_bg_resource
).
setOnClickListener
(
this
);
findViewById
(
R
.
id
.
btn_show_span
).
setOnClickListener
(
this
);
findViewById
(
R
.
id
.
btn_show_custom_view
).
setOnClickListener
(
this
);
findViewById
(
R
.
id
.
btn_show_middle
).
setOnClickListener
(
this
);
findViewById
(
R
.
id
.
btn_cancel_toast
).
setOnClickListener
(
this
);
toastView
=
findViewById
(
R
.
id
.
btn_cancel_toast
);
}
@Override
...
...
@@ -85,10 +90,14 @@ public class ToastActivity extends BaseBackActivity {
ToastUtils
.
showLong
(
R
.
string
.
toast_long
);
break
;
case
R
.
id
.
btn_show_green_font
:
ToastUtils
.
setM
essage
Color
(
Color
.
GREEN
);
ToastUtils
.
setM
sg
Color
(
Color
.
GREEN
);
ToastUtils
.
showLong
(
R
.
string
.
toast_green_font
);
break
;
case
R
.
id
.
btn_show_custom_bg
:
case
R
.
id
.
btn_show_bg_color
:
ToastUtils
.
setBgColor
(
ContextCompat
.
getColor
(
this
,
R
.
color
.
colorAccent
));
ToastUtils
.
showLong
(
R
.
string
.
toast_bg_color
);
break
;
case
R
.
id
.
btn_show_bg_resource
:
ToastUtils
.
setBgResource
(
R
.
drawable
.
shape_round_rect
);
ToastUtils
.
showLong
(
R
.
string
.
toast_custom_bg
);
break
;
...
...
@@ -102,7 +111,7 @@ public class ToastActivity extends BaseBackActivity {
);
break
;
case
R
.
id
.
btn_show_custom_view
:
MyToast
.
showMyToast
(
getString
(
R
.
string
.
toast_custom_view
));
CustomToast
.
show
(
getString
(
R
.
string
.
toast_custom_view
));
break
;
case
R
.
id
.
btn_show_middle
:
ToastUtils
.
setGravity
(
Gravity
.
CENTER
,
0
,
0
);
...
...
@@ -121,9 +130,9 @@ public class ToastActivity extends BaseBackActivity {
}
private
void
resetToast
()
{
ToastUtils
.
setMessageColor
(
0xFFFFFFFF
);
ToastUtils
.
setMsgColor
(
0xFEFFFFFF
);
ToastUtils
.
setBgColor
(
0xFEFFFFFF
);
ToastUtils
.
setBgResource
(-
1
);
ToastUtils
.
setView
(
null
);
ToastUtils
.
setGravity
(
Gravity
.
BOTTOM
|
Gravity
.
CENTER_HORIZONTAL
,
0
,
getResources
().
getDimensionPixelSize
(
R
.
dimen
.
offset_64
));
}
}
app/src/main/res/core/drawable/shape_round_rect.xml
浏览文件 @
b75eec3e
...
...
@@ -3,5 +3,10 @@
<solid
android:color=
"@color/colorAccent"
/>
<corners
android:radius=
"@dimen/radius_16"
/>
<padding
android:bottom=
"@dimen/spacing_16"
android:left=
"@dimen/spacing_16"
android:right=
"@dimen/spacing_16"
android:top=
"@dimen/spacing_16"
/>
</shape>
\ No newline at end of file
app/src/main/res/core/layout/activity_toast.xml
浏览文件 @
b75eec3e
...
...
@@ -43,11 +43,18 @@
android:text=
"@string/toast_show_green_font"
/>
<Button
android:id=
"@+id/btn_show_
custom_bg
"
android:id=
"@+id/btn_show_
bg_color
"
style=
"@style/WideBtnStyle"
android:layout_width=
"match_parent"
android:layout_height=
"wrap_content"
android:text=
"@string/toast_show_custom_bg"
/>
android:text=
"@string/toast_show_bg_color"
/>
<Button
android:id=
"@+id/btn_show_bg_resource"
style=
"@style/WideBtnStyle"
android:layout_width=
"match_parent"
android:layout_height=
"wrap_content"
android:text=
"@string/toast_show_bg_resource"
/>
<Button
android:id=
"@+id/btn_show_span"
...
...
app/src/main/res/core/values/strings.xml
浏览文件 @
b75eec3e
...
...
@@ -152,7 +152,8 @@
<string
name=
"toast_show_long_safe"
>
Show Long Safe
</string>
<string
name=
"toast_show_long"
>
Show Long
</string>
<string
name=
"toast_show_green_font"
>
Show Green Font
</string>
<string
name=
"toast_show_custom_bg"
>
Show Custom Bg
</string>
<string
name=
"toast_show_bg_color"
>
Show Bg Color
</string>
<string
name=
"toast_show_bg_resource"
>
Show Bg Resource
</string>
<string
name=
"toast_show_span"
>
Show Span
</string>
<string
name=
"toast_show_custom_view"
>
Show Custom View
</string>
<string
name=
"toast_custom_view"
>
Custom View
</string>
...
...
@@ -164,6 +165,7 @@
<string
name=
"toast_long_safe"
>
Long Safe
</string>
<string
name=
"toast_long"
>
Long
</string>
<string
name=
"toast_green_font"
>
Green Font
</string>
<string
name=
"toast_bg_color"
>
Bg Color
</string>
<string
name=
"toast_custom_bg"
>
Custom Bg
</string>
<string
name=
"toast_span"
>
Span
</string>
<string
name=
"toast_middle"
>
Middle
</string>
...
...
build.gradle
浏览文件 @
b75eec3e
...
...
@@ -29,8 +29,8 @@ ext {
minSdkVersion
=
14
targetSdkVersion
=
25
versionCode
=
100800
5
00
versionName
=
'1.8.
5
'
versionCode
=
100800
6
00
versionName
=
'1.8.
6
'
// App dependencies
supportVersion
=
'25.3.1'
...
...
update_log.md
浏览文件 @
b75eec3e
*
17/09/02 完善ToastUtils,去除引入view带来的问题,发布版本1.8.6
*
17/08/30 修复ToastUtils弱引用带来的问题,修复CacheUtils异步问题,发布版本1.8.5
*
17/08/28 修复ToastUtils内存泄露,新增toast可根据系统字体显示不同字体,发布版本1.8.4
*
17/08/20 新增监听Activity生命周期,退出App,发布版本1.8.3
...
...
utilcode/build.gradle
浏览文件 @
b75eec3e
...
...
@@ -62,5 +62,5 @@ dependencies {
testCompile
"org.robolectric:robolectric:$rootProject.ext.robolectricVersion"
testCompile
"com.google.truth:truth:0.31"
}
//
apply from: "https://raw.githubusercontent.com/xiaopansky/android-library-publish-to-jcenter/master/bintrayUpload.gradle"
apply
from:
"https://raw.githubusercontent.com/xiaopansky/android-library-publish-to-jcenter/master/bintrayUpload.gradle"
//gradle bintrayUpload
utilcode/src/main/java/com/blankj/utilcode/util/IntentUtils.java
浏览文件 @
b75eec3e
...
...
@@ -21,7 +21,7 @@ import java.io.File;
public
final
class
IntentUtils
{
private
IntentUtils
()
{
throw
new
UnsupportedOperationException
(
"u can't
fuck
me..."
);
throw
new
UnsupportedOperationException
(
"u can't
instantiate
me..."
);
}
/**
...
...
utilcode/src/main/java/com/blankj/utilcode/util/LogUtils.java
浏览文件 @
b75eec3e
...
...
@@ -212,23 +212,17 @@ public final class LogUtils {
tag
=
sGlobalTag
;
}
else
{
StackTraceElement
targetElement
=
new
Throwable
().
getStackTrace
()[
3
];
String
className
=
targetElement
.
getClassName
();
String
[]
classNameInfo
=
className
.
split
(
"\\."
);
if
(
classNameInfo
.
length
>
0
)
{
className
=
classNameInfo
[
classNameInfo
.
length
-
1
];
}
if
(
className
.
contains
(
"$"
))
{
className
=
className
.
split
(
"\\$"
)[
0
];
}
String
fileName
=
targetElement
.
getFileName
();
String
className
=
fileName
.
substring
(
0
,
fileName
.
indexOf
(
'.'
));
if
(
sTagIsSpace
)
{
tag
=
isSpace
(
tag
)
?
className
:
tag
;
}
if
(
sLogHeadSwitch
)
{
String
head
=
new
Formatter
()
.
format
(
"%s, %s(%s
.java
:%d)"
,
.
format
(
"%s, %s(%s:%d)"
,
Thread
.
currentThread
().
getName
(),
targetElement
.
getMethodName
(),
class
Name
,
file
Name
,
targetElement
.
getLineNumber
())
.
toString
();
return
new
String
[]{
tag
,
head
+
LINE_SEP
,
" ["
+
head
+
"]: "
};
...
...
utilcode/src/main/java/com/blankj/utilcode/util/ToastUtils.java
浏览文件 @
b75eec3e
package
com.blankj.utilcode.util
;
import
android.content.Context
;
import
android.graphics.PorterDuff
;
import
android.graphics.PorterDuffColorFilter
;
import
android.graphics.drawable.Drawable
;
import
android.os.Handler
;
import
android.os.Looper
;
import
android.support.annotation.ColorInt
;
...
...
@@ -9,6 +12,7 @@ import android.support.annotation.LayoutRes;
import
android.support.annotation.NonNull
;
import
android.support.annotation.StringRes
;
import
android.support.v4.widget.TextViewCompat
;
import
android.util.Log
;
import
android.view.Gravity
;
import
android.view.LayoutInflater
;
import
android.view.View
;
...
...
@@ -29,14 +33,16 @@ public final class ToastUtils {
private
static
final
int
DEFAULT_COLOR
=
0xFEFFFFFF
;
private
static
final
Handler
HANDLER
=
new
Handler
(
Looper
.
getMainLooper
());
private
static
Toast
sToast
;
private
static
WeakReference
<
View
>
sViewWeakReference
;
private
static
int
gravity
=
Gravity
.
CENTER_HORIZONTAL
|
Gravity
.
BOTTOM
;
private
static
int
xOffset
=
0
;
private
static
int
yOffset
=
(
int
)
(
64
*
Utils
.
getApp
().
getResources
().
getDisplayMetrics
().
density
+
0.5
);
private
static
int
backgroundColor
=
DEFAULT_COLOR
;
private
static
int
bgResource
=
-
1
;
private
static
int
messageColor
=
DEFAULT_COLOR
;
private
static
int
sLayoutId
=
-
1
;
private
static
int
gravity
=
Gravity
.
CENTER_HORIZONTAL
|
Gravity
.
BOTTOM
;
private
static
int
xOffset
=
0
;
private
static
int
yOffset
=
(
int
)
(
64
*
Utils
.
getApp
().
getResources
().
getDisplayMetrics
().
density
+
0.5
);
private
static
int
bgColor
=
DEFAULT_COLOR
;
private
static
int
bgResource
=
-
1
;
private
static
int
msgColor
=
DEFAULT_COLOR
;
private
ToastUtils
()
{
throw
new
UnsupportedOperationException
(
"u can't instantiate me..."
);
...
...
@@ -55,51 +61,13 @@ public final class ToastUtils {
ToastUtils
.
yOffset
=
yOffset
;
}
/**
* 设置吐司view
*
* @param layoutId 视图
*/
public
static
View
setView
(
@LayoutRes
final
int
layoutId
)
{
LayoutInflater
inflate
=
(
LayoutInflater
)
Utils
.
getApp
().
getSystemService
(
Context
.
LAYOUT_INFLATER_SERVICE
);
final
View
toastView
=
inflate
.
inflate
(
layoutId
,
null
);
sViewWeakReference
=
new
WeakReference
<>(
toastView
);
return
toastView
;
}
/**
* 设置吐司view
*
* @param view 视图
*/
public
static
View
setView
(
final
View
view
)
{
sViewWeakReference
=
view
==
null
?
null
:
new
WeakReference
<>(
view
);
return
view
;
}
/**
* 获取吐司view
*
* @return view
*/
public
static
View
getView
()
{
final
View
view
=
getViewFromWR
();
if
(
view
!=
null
)
{
return
view
;
}
if
(
sToast
!=
null
)
{
return
sToast
.
getView
();
}
return
null
;
}
/**
* 设置背景颜色
*
* @param backgroundColor 背景色
*/
public
static
void
setBgColor
(
@ColorInt
final
int
backgroundColor
)
{
ToastUtils
.
b
ackground
Color
=
backgroundColor
;
ToastUtils
.
b
g
Color
=
backgroundColor
;
}
/**
...
...
@@ -114,10 +82,10 @@ public final class ToastUtils {
/**
* 设置消息颜色
*
* @param m
essage
Color 颜色
* @param m
sg
Color 颜色
*/
public
static
void
setM
essageColor
(
@ColorInt
final
int
message
Color
)
{
ToastUtils
.
m
essageColor
=
message
Color
;
public
static
void
setM
sgColor
(
@ColorInt
final
int
msg
Color
)
{
ToastUtils
.
m
sgColor
=
msg
Color
;
}
/**
...
...
@@ -316,11 +284,11 @@ public final class ToastUtils {
* 安全地显示短时自定义吐司
*/
public
static
View
showCustomShortSafe
(
@LayoutRes
final
int
layoutId
)
{
final
View
view
=
s
etView
(
layoutId
);
final
View
view
=
g
etView
(
layoutId
);
HANDLER
.
post
(
new
Runnable
()
{
@Override
public
void
run
()
{
show
(
""
,
Toast
.
LENGTH_SHORT
);
show
(
view
,
Toast
.
LENGTH_SHORT
);
}
});
return
view
;
...
...
@@ -330,11 +298,11 @@ public final class ToastUtils {
* 安全地显示长时自定义吐司
*/
public
static
View
showCustomLongSafe
(
@LayoutRes
final
int
layoutId
)
{
final
View
view
=
s
etView
(
layoutId
);
final
View
view
=
g
etView
(
layoutId
);
HANDLER
.
post
(
new
Runnable
()
{
@Override
public
void
run
()
{
show
(
""
,
Toast
.
LENGTH_LONG
);
show
(
view
,
Toast
.
LENGTH_LONG
);
}
});
return
view
;
...
...
@@ -344,8 +312,8 @@ public final class ToastUtils {
* 显示短时自定义吐司
*/
public
static
View
showCustomShort
(
@LayoutRes
final
int
layoutId
)
{
final
View
view
=
s
etView
(
layoutId
);
show
(
""
,
Toast
.
LENGTH_SHORT
);
final
View
view
=
g
etView
(
layoutId
);
show
(
view
,
Toast
.
LENGTH_SHORT
);
return
view
;
}
...
...
@@ -353,146 +321,78 @@ public final class ToastUtils {
* 显示长时自定义吐司
*/
public
static
View
showCustomLong
(
@LayoutRes
final
int
layoutId
)
{
final
View
view
=
setView
(
layoutId
);
show
(
""
,
Toast
.
LENGTH_LONG
);
return
view
;
}
/**
* 安全地显示短时自定义吐司
*/
public
static
View
showCustomShortSafe
(
@NonNull
final
View
view
)
{
setView
(
view
);
HANDLER
.
post
(
new
Runnable
()
{
@Override
public
void
run
()
{
show
(
""
,
Toast
.
LENGTH_SHORT
);
}
});
final
View
view
=
getView
(
layoutId
);
show
(
view
,
Toast
.
LENGTH_LONG
);
return
view
;
}
/**
* 安全地显示长时自定义吐司
*/
public
static
View
showCustomLongSafe
(
@NonNull
final
View
view
)
{
setView
(
view
);
HANDLER
.
post
(
new
Runnable
()
{
@Override
public
void
run
()
{
setView
(
view
);
show
(
""
,
Toast
.
LENGTH_LONG
);
}
});
return
view
;
}
/**
* 显示短时自定义吐司
*/
public
static
View
showCustomShort
(
@NonNull
final
View
view
)
{
setView
(
view
);
show
(
""
,
Toast
.
LENGTH_SHORT
);
return
view
;
}
/**
* 显示长时自定义吐司
* 取消吐司显示
*/
public
static
View
showCustomLong
(
@NonNull
final
View
view
)
{
setView
(
view
);
show
(
""
,
Toast
.
LENGTH_LONG
);
return
view
;
public
static
void
cancel
()
{
if
(
sToast
!=
null
)
{
sToast
.
cancel
();
sToast
=
null
;
}
}
/**
* 显示吐司
*
* @param resId 资源Id
* @param duration 显示时长
*/
private
static
void
show
(
@StringRes
final
int
resId
,
final
int
duration
)
{
show
(
Utils
.
getApp
().
getResources
().
getText
(
resId
).
toString
(),
duration
);
}
/**
* 显示吐司
*
* @param resId 资源Id
* @param duration 显示时长
* @param args 参数
*/
private
static
void
show
(
@StringRes
final
int
resId
,
final
int
duration
,
final
Object
...
args
)
{
show
(
String
.
format
(
Utils
.
getApp
().
getResources
().
getString
(
resId
),
args
),
duration
);
}
/**
* 显示吐司
*
* @param format 格式
* @param duration 显示时长
* @param args 参数
*/
private
static
void
show
(
final
String
format
,
final
int
duration
,
final
Object
...
args
)
{
show
(
String
.
format
(
format
,
args
),
duration
);
}
/**
* 显示吐司
*
* @param text 文本
* @param duration 显示时长
*/
private
static
void
show
(
final
CharSequence
text
,
final
int
duration
)
{
cancel
();
final
View
view
=
getViewFromWR
();
if
(
view
!=
null
)
{
sToast
=
new
Toast
(
Utils
.
getApp
());
sToast
.
setView
(
view
);
sToast
.
setDuration
(
duration
);
}
else
{
sToast
=
Toast
.
makeText
(
Utils
.
getApp
(),
text
,
duration
);
// solve the font of toast
TextView
tvMessage
=
(
TextView
)
sToast
.
getView
().
findViewById
(
android
.
R
.
id
.
message
);
TextViewCompat
.
setTextAppearance
(
tvMessage
,
android
.
R
.
style
.
TextAppearance
);
tvMessage
.
setTextColor
(
messageColor
);
}
View
toastView
=
sToast
.
getView
();
if
(
bgResource
!=
-
1
)
{
toastView
.
setBackgroundResource
(
bgResource
);
}
else
if
(
backgroundColor
!=
DEFAULT_COLOR
)
{
toastView
.
setBackgroundColor
(
backgroundColor
);
}
sToast
.
setGravity
(
gravity
,
xOffset
,
yOffset
);
sToast
=
Toast
.
makeText
(
Utils
.
getApp
(),
text
,
duration
);
// solve the font of toast
TextView
tvMessage
=
(
TextView
)
sToast
.
getView
().
findViewById
(
android
.
R
.
id
.
message
);
TextViewCompat
.
setTextAppearance
(
tvMessage
,
android
.
R
.
style
.
TextAppearance
);
tvMessage
.
setTextColor
(
msgColor
);
setBgAndGravity
();
sToast
.
show
();
}
/**
* 取消吐司显示
*/
public
static
void
cancel
()
{
if
(
sToast
!=
null
)
{
sToast
.
cancel
();
sToast
=
null
;
}
private
static
void
show
(
final
View
view
,
final
int
duration
)
{
cancel
();
sToast
=
new
Toast
(
Utils
.
getApp
());
sToast
.
setView
(
view
);
sToast
.
setDuration
(
duration
);
setBgAndGravity
();
sToast
.
show
();
}
/**
* 如果自定义View的Context为Activity级别,需要调用releaseView,否则会内存泄露
*/
static
void
releaseView
()
{
if
(
sToast
!=
null
)
{
sToast
.
setView
(
null
);
private
static
void
setBgAndGravity
()
{
View
toastView
=
sToast
.
getView
();
if
(
bgResource
!=
-
1
)
{
toastView
.
setBackgroundResource
(
bgResource
);
}
else
if
(
bgColor
!=
DEFAULT_COLOR
)
{
Drawable
background
=
toastView
.
getBackground
();
background
.
setColorFilter
(
new
PorterDuffColorFilter
(
bgColor
,
PorterDuff
.
Mode
.
SRC_IN
));
}
sToast
.
setGravity
(
gravity
,
xOffset
,
yOffset
);
}
private
static
View
getViewFromWR
()
{
if
(
sViewWeakReference
!=
null
)
{
final
View
view
=
sViewWeakReference
.
get
();
if
(
view
!=
null
)
{
return
view
;
private
static
View
getView
(
@LayoutRes
final
int
layoutId
)
{
if
(
sLayoutId
==
layoutId
)
{
if
(
sViewWeakReference
!=
null
)
{
final
View
toastView
=
sViewWeakReference
.
get
();
if
(
toastView
!=
null
)
{
return
toastView
;
}
}
}
return
null
;
Log
.
d
(
"bbbb"
,
"getView: "
);
LayoutInflater
inflate
=
(
LayoutInflater
)
Utils
.
getApp
().
getSystemService
(
Context
.
LAYOUT_INFLATER_SERVICE
);
final
View
toastView
=
inflate
.
inflate
(
layoutId
,
null
);
sViewWeakReference
=
new
WeakReference
<>(
toastView
);
sLayoutId
=
layoutId
;
return
toastView
;
}
}
\ No newline at end of file
utilcode/src/main/java/com/blankj/utilcode/util/Utils.java
浏览文件 @
b75eec3e
...
...
@@ -61,7 +61,6 @@ public final class Utils {
@Override
public
void
onActivityDestroyed
(
Activity
activity
)
{
sActivityList
.
remove
(
activity
);
ToastUtils
.
releaseView
();
}
};
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录