提交 b75eec3e 编写于 作者: B Blankj

see 09/02 log

上级 814491ee
......@@ -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
......
......@@ -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
......
......@@ -15,9 +15,9 @@ import com.blankj.utilcode.util.ToastUtils;
* desc :
* </pre>
*/
public class MyToast {
public class CustomToast {
public static void showMyToast(@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);
......
......@@ -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.setMessageColor(Color.GREEN);
ToastUtils.setMsgColor(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));
}
}
......@@ -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
......@@ -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"
......
......@@ -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>
......
......@@ -29,8 +29,8 @@ ext {
minSdkVersion = 14
targetSdkVersion = 25
versionCode = 100800500
versionName = '1.8.5'
versionCode = 100800600
versionName = '1.8.6'
// App dependencies
supportVersion = '25.3.1'
......
* 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
......
......@@ -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
......@@ -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...");
}
/**
......
......@@ -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(),
className,
fileName,
targetElement.getLineNumber())
.toString();
return new String[]{tag, head + LINE_SEP, " [" + head + "]: "};
......
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.backgroundColor = backgroundColor;
ToastUtils.bgColor = backgroundColor;
}
/**
......@@ -114,10 +82,10 @@ public final class ToastUtils {
/**
* 设置消息颜色
*
* @param messageColor 颜色
* @param msgColor 颜色
*/
public static void setMessageColor(@ColorInt final int messageColor) {
ToastUtils.messageColor = messageColor;
public static void setMsgColor(@ColorInt final int msgColor) {
ToastUtils.msgColor = msgColor;
}
/**
......@@ -316,11 +284,11 @@ public final class ToastUtils {
* 安全地显示短时自定义吐司
*/
public static View showCustomShortSafe(@LayoutRes final int layoutId) {
final View view = setView(layoutId);
final View view = getView(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 = setView(layoutId);
final View view = getView(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 = setView(layoutId);
show("", Toast.LENGTH_SHORT);
final View view = getView(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
......@@ -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.
先完成此消息的编辑!
想要评论请 注册