1515import android .support .annotation .StringRes ;
1616import android .support .v4 .view .ViewCompat ;
1717import android .support .v4 .widget .TextViewCompat ;
18- import android .view .Gravity ;
1918import android .view .LayoutInflater ;
2019import android .view .View ;
2120import android .widget .TextView ;
@@ -35,12 +34,12 @@ public final class ToastUtils {
3534 private static final Handler HANDLER = new Handler (Looper .getMainLooper ());
3635
3736 private static Toast sToast ;
38- private static int gravity = Gravity . CENTER_HORIZONTAL | Gravity . BOTTOM ;
39- private static int xOffset = 0 ;
40- private static int yOffset = ( int ) ( 64 * Utils . getApp (). getResources (). getDisplayMetrics (). density + 0.5 ) ;
41- private static int bgColor = COLOR_DEFAULT ;
42- private static int bgResource = -1 ;
43- private static int msgColor = COLOR_DEFAULT ;
37+ private static int sGravity = - 1 ;
38+ private static int sXOffset = - 1 ;
39+ private static int sYOffset = - 1 ;
40+ private static int sBgColor = COLOR_DEFAULT ;
41+ private static int sBgResource = -1 ;
42+ private static int sMsgColor = COLOR_DEFAULT ;
4443
4544 private ToastUtils () {
4645 throw new UnsupportedOperationException ("u can't instantiate me..." );
@@ -54,9 +53,9 @@ private ToastUtils() {
5453 * @param yOffset y 偏移
5554 */
5655 public static void setGravity (final int gravity , final int xOffset , final int yOffset ) {
57- ToastUtils . gravity = gravity ;
58- ToastUtils . xOffset = xOffset ;
59- ToastUtils . yOffset = yOffset ;
56+ sGravity = gravity ;
57+ sXOffset = xOffset ;
58+ sYOffset = yOffset ;
6059 }
6160
6261 /**
@@ -65,7 +64,7 @@ public static void setGravity(final int gravity, final int xOffset, final int yO
6564 * @param backgroundColor 背景色
6665 */
6766 public static void setBgColor (@ ColorInt final int backgroundColor ) {
68- ToastUtils . bgColor = backgroundColor ;
67+ sBgColor = backgroundColor ;
6968 }
7069
7170 /**
@@ -74,7 +73,7 @@ public static void setBgColor(@ColorInt final int backgroundColor) {
7473 * @param bgResource 背景资源
7574 */
7675 public static void setBgResource (@ DrawableRes final int bgResource ) {
77- ToastUtils . bgResource = bgResource ;
76+ sBgResource = bgResource ;
7877 }
7978
8079 /**
@@ -83,7 +82,7 @@ public static void setBgResource(@DrawableRes final int bgResource) {
8382 * @param msgColor 颜色
8483 */
8584 public static void setMsgColor (@ ColorInt final int msgColor ) {
86- ToastUtils . msgColor = msgColor ;
85+ sMsgColor = msgColor ;
8786 }
8887
8988 /**
@@ -208,11 +207,15 @@ private static void show(final CharSequence text, final int duration) {
208207 public void run () {
209208 cancel ();
210209 sToast = Toast .makeText (Utils .getApp (), text , duration );
211- // solve the font of toast
210+ //t solve the font of toast
212211 TextView tvMessage = sToast .getView ().findViewById (android .R .id .message );
213212 TextViewCompat .setTextAppearance (tvMessage , android .R .style .TextAppearance );
214- tvMessage .setTextColor (msgColor );
215- sToast .setGravity (gravity , xOffset , yOffset );
213+ if (sMsgColor != COLOR_DEFAULT ) {
214+ tvMessage .setTextColor (sMsgColor );
215+ }
216+ if (sGravity != -1 || sXOffset != -1 || sYOffset != -1 ) {
217+ sToast .setGravity (sGravity , sXOffset , sYOffset );
218+ }
216219 setBg (tvMessage );
217220 sToast .show ();
218221 }
@@ -227,7 +230,9 @@ public void run() {
227230 sToast = new Toast (Utils .getApp ());
228231 sToast .setView (view );
229232 sToast .setDuration (duration );
230- sToast .setGravity (gravity , xOffset , yOffset );
233+ if (sGravity != -1 || sXOffset != -1 || sYOffset != -1 ) {
234+ sToast .setGravity (sGravity , sXOffset , sYOffset );
235+ }
231236 setBg ();
232237 sToast .show ();
233238 }
@@ -236,37 +241,37 @@ public void run() {
236241
237242 private static void setBg () {
238243 View toastView = sToast .getView ();
239- if (bgResource != -1 ) {
240- toastView .setBackgroundResource (bgResource );
241- } else if (bgColor != COLOR_DEFAULT ) {
244+ if (sBgResource != -1 ) {
245+ toastView .setBackgroundResource (sBgResource );
246+ } else if (sBgColor != COLOR_DEFAULT ) {
242247 Drawable background = toastView .getBackground ();
243248 if (background != null ) {
244249 background .setColorFilter (
245- new PorterDuffColorFilter (bgColor , PorterDuff .Mode .SRC_IN )
250+ new PorterDuffColorFilter (sBgColor , PorterDuff .Mode .SRC_IN )
246251 );
247252 } else {
248- ViewCompat .setBackground (toastView , new ColorDrawable (bgColor ));
253+ ViewCompat .setBackground (toastView , new ColorDrawable (sBgColor ));
249254 }
250255 }
251256 }
252257
253258 private static void setBg (final TextView tvMsg ) {
254259 View toastView = sToast .getView ();
255- if (bgResource != -1 ) {
256- toastView .setBackgroundResource (bgResource );
260+ if (sBgResource != -1 ) {
261+ toastView .setBackgroundResource (sBgResource );
257262 tvMsg .setBackgroundColor (Color .TRANSPARENT );
258- } else if (bgColor != COLOR_DEFAULT ) {
263+ } else if (sBgColor != COLOR_DEFAULT ) {
259264 Drawable tvBg = toastView .getBackground ();
260265 Drawable msgBg = tvMsg .getBackground ();
261266 if (tvBg != null && msgBg != null ) {
262- tvBg .setColorFilter (new PorterDuffColorFilter (bgColor , PorterDuff .Mode .SRC_IN ));
267+ tvBg .setColorFilter (new PorterDuffColorFilter (sBgColor , PorterDuff .Mode .SRC_IN ));
263268 tvMsg .setBackgroundColor (Color .TRANSPARENT );
264269 } else if (tvBg != null ) {
265- tvBg .setColorFilter (new PorterDuffColorFilter (bgColor , PorterDuff .Mode .SRC_IN ));
270+ tvBg .setColorFilter (new PorterDuffColorFilter (sBgColor , PorterDuff .Mode .SRC_IN ));
266271 } else if (msgBg != null ) {
267- msgBg .setColorFilter (new PorterDuffColorFilter (bgColor , PorterDuff .Mode .SRC_IN ));
272+ msgBg .setColorFilter (new PorterDuffColorFilter (sBgColor , PorterDuff .Mode .SRC_IN ));
268273 } else {
269- toastView .setBackgroundColor (bgColor );
274+ toastView .setBackgroundColor (sBgColor );
270275 }
271276 }
272277 }
0 commit comments