Skip to content

Commit c584fb1

Browse files
committed
see 08/28 log
1 parent 55649a9 commit c584fb1

File tree

5 files changed

+64
-32
lines changed

5 files changed

+64
-32
lines changed

app/src/main/res/core/layout/activity_activity.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
<TextView
1111
android:id="@+id/tv_about_activity"
12-
style="@style/TextStyle"
12+
style="@style/TextStyle.Font"
1313
android:layout_width="match_parent"
1414
android:layout_height="wrap_content" />
1515

app/src/main/res/values/styles.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616
<style name="TextStyle">
1717
<item name="android:textSize">@dimen/font_24</item>
1818
<item name="android:textColor">@color/light_black</item>
19-
<item name="android:gravity">start</item>
19+
<item name="android:gravity">center</item>
20+
</style>
21+
22+
<style name="TextStyle.Font">
23+
<item name="android:textSize">@dimen/font_40</item>
2024
</style>
2125

2226
<style name="WideBtnStyle">

update_log.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
* 17/08/28 修复ToastUtils内存泄露,新增toast可根据系统字体显示不同字体
12
* 17/08/20 新增监听Activity生命周期,退出App,发布版本1.8.3
23
* 17/08/11 LogUtils的Builder改为Config,发布版本1.8.2
34
* 17/08/10 优化FileUtils的deleteFilesInDir和listFilesInDir

utilcode/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ android {
3737
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
3838
}
3939
release {
40-
minifyEnabled true
40+
minifyEnabled false
4141
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
4242
}
4343
}

utilcode/src/main/java/com/blankj/utilcode/util/ToastUtils.java

Lines changed: 56 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@
88
import android.support.annotation.LayoutRes;
99
import android.support.annotation.NonNull;
1010
import android.support.annotation.StringRes;
11+
import android.support.v4.widget.TextViewCompat;
1112
import android.text.SpannableString;
1213
import android.text.Spanned;
1314
import android.text.style.ForegroundColorSpan;
15+
import android.util.Log;
1416
import android.view.Gravity;
1517
import android.view.LayoutInflater;
1618
import android.view.View;
19+
import android.widget.TextView;
1720
import android.widget.Toast;
1821

1922
import java.lang.ref.WeakReference;
@@ -28,10 +31,11 @@
2831
*/
2932
public final class ToastUtils {
3033

34+
private static final String TAG = "ToastUtils";
3135
private static final int DEFAULT_COLOR = 0x12000000;
3236
private static final Handler sHandler = new Handler(Looper.getMainLooper());
33-
private static Toast sToast;
34-
private static WeakReference<View> sViewWeakReference;
37+
private static WeakReference<Toast> sToastWeakReference;
38+
private static WeakReference<View> sViewWeakReference;
3539
private static int gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
3640
private static int xOffset = 0;
3741
private static int yOffset = (int) (64 * Utils.getApp().getResources().getDisplayMetrics().density + 0.5);
@@ -81,13 +85,14 @@ public static void setView(final View view) {
8185
* @return view
8286
*/
8387
public static View getView() {
84-
if (sViewWeakReference != null) {
85-
final View view = sViewWeakReference.get();
86-
if (view != null) {
87-
return view;
88-
}
88+
final View view = getViewFromWR();
89+
if (view != null) {
90+
return view;
91+
}
92+
final Toast toast = getToastFromWR();
93+
if (toast != null) {
94+
return toast.getView();
8995
}
90-
if (sToast != null) return sToast.getView();
9196
return null;
9297
}
9398

@@ -434,43 +439,65 @@ private static void show(final String format, final int duration, final Object..
434439
*/
435440
private static void show(final CharSequence text, final int duration) {
436441
cancel();
437-
boolean isCustom = false;
438-
if (sViewWeakReference != null) {
439-
final View view = sViewWeakReference.get();
440-
if (view != null) {
441-
sToast = new Toast(Utils.getApp());
442-
sToast.setView(view);
443-
sToast.setDuration(duration);
444-
isCustom = true;
445-
}
446-
}
447-
if (!isCustom) {
442+
Toast toast;
443+
final View view = getViewFromWR();
444+
if (view != null) {
445+
toast = new Toast(Utils.getApp());
446+
toast.setView(view);
447+
toast.setDuration(duration);
448+
} else {
448449
if (messageColor != DEFAULT_COLOR) {
449450
SpannableString spannableString = new SpannableString(text);
450451
ForegroundColorSpan colorSpan = new ForegroundColorSpan(messageColor);
451452
spannableString.setSpan(colorSpan, 0, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
452-
sToast = Toast.makeText(Utils.getApp(), spannableString, duration);
453+
toast = Toast.makeText(Utils.getApp(), spannableString, duration);
453454
} else {
454-
sToast = Toast.makeText(Utils.getApp(), text, duration);
455+
toast = Toast.makeText(Utils.getApp(), text, duration);
455456
}
457+
// solve the font of toast
458+
TextViewCompat.setTextAppearance((TextView) toast.getView().findViewById(android.R.id.message), android.R.style.TextAppearance);
456459
}
457-
View view = sToast.getView();
460+
View toastView = toast.getView();
458461
if (bgResource != -1) {
459-
view.setBackgroundResource(bgResource);
462+
toastView.setBackgroundResource(bgResource);
460463
} else if (backgroundColor != DEFAULT_COLOR) {
461-
view.setBackgroundColor(backgroundColor);
464+
toastView.setBackgroundColor(backgroundColor);
462465
}
463-
sToast.setGravity(gravity, xOffset, yOffset);
464-
sToast.show();
466+
toast.setGravity(gravity, xOffset, yOffset);
467+
sToastWeakReference = new WeakReference<>(toast);
468+
toast.show();
465469
}
466470

467471
/**
468472
* 取消吐司显示
469473
*/
470474
public static void cancel() {
471-
if (sToast != null) {
472-
sToast.cancel();
473-
sToast = null;
475+
Toast toast = getToastFromWR();
476+
if (toast != null) {
477+
toast.cancel();
474478
}
479+
sToastWeakReference = null;
480+
}
481+
482+
private static Toast getToastFromWR() {
483+
if (sToastWeakReference != null) {
484+
final Toast toast = sToastWeakReference.get();
485+
if (toast != null) {
486+
return toast;
487+
}
488+
}
489+
Log.e(TAG, "getToastFromWR: ", new NullPointerException("Toast is null"));
490+
return null;
491+
}
492+
493+
private static View getViewFromWR() {
494+
if (sViewWeakReference != null) {
495+
final View view = sViewWeakReference.get();
496+
if (view != null) {
497+
return view;
498+
}
499+
}
500+
Log.e(TAG, "getViewFromWR: ", new NullPointerException("The custom view of toast is null"));
501+
return null;
475502
}
476503
}

0 commit comments

Comments
 (0)