`

Android自定义Toast

 
阅读更多

转于:http://blog.csdn.net/zhangweiwtmdbf/article/details/30031015

-------------------------------------------------------------------------------------------

一、引言

在开发的过程中你会发现Android自身的Toast提示有许多限制,比如我想自定义Toast的动画、自定义一个美观的View显示在Toast中、更多的是让Toast显示指定的时长等等。

首先一下效果如何:

 

二、原理

自定义的原理也很简单,就是给WindowManager添加View和删除View,不过需要设置WindowManager.LayoutParams和View的样式,使其看起来和Android系统的Toast看起来很相像。

具体代码如下:

 

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. /** 
  2.  * Custom Toast 
  3.  *  
  4.  * @author Lucky 
  5.  *  
  6.  */  
  7. public class ToastHelper {  
  8.     public static final int LENGTH_LONG = 3500;  
  9.     public static final int LENGTH_SHORT = 2000;  
  10.     private WindowManager mWindowManager;  
  11.     private WindowManager.LayoutParams mWindowParams;  
  12.     private View toastView;  
  13.     private Context mContext;  
  14.     private Handler mHandler;  
  15.     private String mToastContent = "";  
  16.     private int duration = 0;  
  17.     private int animStyleId = android.R.style.Animation_Toast;  
  18.   
  19.     private final Runnable timerRunnable = new Runnable() {  
  20.   
  21.         @Override  
  22.         public void run() {  
  23.             removeView();  
  24.         }  
  25.     };  
  26.   
  27.     private ToastHelper(Context context) {  
  28.         // Notice: we should get application context  
  29.         // otherwise we will get error  
  30.         // "Activity has leaked window that was originally added"  
  31.         Context ctx = context.getApplicationContext();  
  32.         if (ctx == null) {  
  33.             ctx = context;  
  34.         }  
  35.         this.mContext = ctx;  
  36.         mWindowManager = (WindowManager) mContext  
  37.                 .getSystemService(Context.WINDOW_SERVICE);  
  38.         init();  
  39.     }  
  40.   
  41.     private void init() {  
  42.         mWindowParams = new WindowManager.LayoutParams();  
  43.         mWindowParams.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON  
  44.                 | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE  
  45.                 | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;  
  46.         mWindowParams.alpha = 1.0f;  
  47.         mWindowParams.width = WindowManager.LayoutParams.WRAP_CONTENT;  
  48.         mWindowParams.height = WindowManager.LayoutParams.WRAP_CONTENT;  
  49.         mWindowParams.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;  
  50.         mWindowParams.format = PixelFormat.TRANSLUCENT;  
  51.         mWindowParams.type = WindowManager.LayoutParams.TYPE_TOAST;  
  52.         mWindowParams.setTitle("ToastHelper");  
  53.         mWindowParams.packageName = mContext.getPackageName();  
  54.         mWindowParams.windowAnimations = animStyleId;// TODO  
  55.         mWindowParams.y = mContext.getResources().getDisplayMetrics().widthPixels / 5;  
  56.     }  
  57.   
  58.     @SuppressWarnings("deprecation")  
  59.     @SuppressLint("NewApi")  
  60.     private View getDefaultToastView() {  
  61.   
  62.         TextView view = new TextView(mContext);  
  63.         view.setText(mToastContent);  
  64.         view.setGravity(Gravity.CENTER_VERTICAL | Gravity.START);  
  65.         view.setFocusable(false);  
  66.         view.setClickable(false);  
  67.         view.setFocusableInTouchMode(false);  
  68.         view.setTextColor(android.graphics.Color.WHITE);  
  69.         Drawable drawable = mContext.getResources().getDrawable(  
  70.                 android.R.drawable.toast_frame);  
  71.   
  72.         if (Build.VERSION.SDK_INT < 16) {  
  73.             view.setBackgroundDrawable(drawable);  
  74.         } else {  
  75.             view.setBackground(drawable);  
  76.         }  
  77.         return view;  
  78.     }  
  79.   
  80.     public void show() {  
  81.         removeView();  
  82.         if (toastView == null) {  
  83.             toastView = getDefaultToastView();  
  84.         }  
  85.         mWindowParams.gravity = android.support.v4.view.GravityCompat  
  86.                 .getAbsoluteGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM,  
  87.                         android.support.v4.view.ViewCompat  
  88.                                 .getLayoutDirection(toastView));  
  89.         removeView();  
  90.         mWindowManager.addView(toastView, mWindowParams);  
  91.         if (mHandler == null) {  
  92.             mHandler = new Handler();  
  93.         }  
  94.         mHandler.postDelayed(timerRunnable, duration);  
  95.     }  
  96.   
  97.     public void removeView() {  
  98.         if (toastView != null && toastView.getParent() != null) {  
  99.             mWindowManager.removeView(toastView);  
  100.             mHandler.removeCallbacks(timerRunnable);  
  101.         }  
  102.     }  
  103.   
  104.     /** 
  105.      * @param context 
  106.      * @param content 
  107.      * @param duration 
  108.      * @return 
  109.      */  
  110.     public static ToastHelper makeText(Context context, String content,  
  111.             int duration) {  
  112.         ToastHelper helper = new ToastHelper(context);  
  113.         helper.setDuration(duration);  
  114.         helper.setContent(content);  
  115.         return helper;  
  116.     }  
  117.   
  118.     /** 
  119.      * @param context 
  120.      * @param strId 
  121.      * @param duration 
  122.      * @return 
  123.      */  
  124.     public static ToastHelper makeText(Context context, int strId, int duration) {  
  125.         ToastHelper helper = new ToastHelper(context);  
  126.         helper.setDuration(duration);  
  127.         helper.setContent(context.getString(strId));  
  128.         return helper;  
  129.     }  
  130.   
  131.     public ToastHelper setContent(String content) {  
  132.         this.mToastContent = content;  
  133.         return this;  
  134.     }  
  135.   
  136.     public ToastHelper setDuration(int duration) {  
  137.         this.duration = duration;  
  138.         return this;  
  139.     }  
  140.   
  141.     public ToastHelper setAnimation(int animStyleId) {  
  142.         this.animStyleId = animStyleId;  
  143.         mWindowParams.windowAnimations = this.animStyleId;  
  144.         return this;  
  145.     }  
  146.   
  147.     /** 
  148.      * custom view 
  149.      *  
  150.      * @param view 
  151.      */  
  152.     public ToastHelper setView(View view) {  
  153.         this.toastView = view;  
  154.         return this;  
  155.     }  
  156. }  


另外分享一个自定义的Anim:

 

1.显示Toast的动画:

 

[html] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:duration="300"  
  4.     android:fillAfter="true" >  
  5.   
  6.     <alpha  
  7.         android:fromAlpha="0.0"  
  8.         android:toAlpha="1.0" />  
  9.   
  10.     <translate  
  11.         android:fromYDelta="20%"  
  12.         android:toYDelta="0%" />  
  13.   
  14.     <scale  
  15.         android:fromXScale="0.5"  
  16.         android:fromYScale="0.5"  
  17.         android:pivotX="50%"  
  18.         android:pivotY="50%"  
  19.         android:toXScale="1.0"  
  20.         android:toYScale="1.0" />  
  21.   
  22. </set>  


2.退出Toast的动画:

 

 

[html] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. <set xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:duration="300"  
  3.     android:fillAfter="true" >  
  4.   
  5.     <alpha  
  6.         android:fromAlpha="1.0"  
  7.         android:toAlpha="0.0" />  
  8.   
  9.     <translate  
  10.         android:fromYDelta="0%"  
  11.         android:toYDelta="20%" />  
  12.   
  13. </set>  



 

给WIndowManager中的View添加动画需要定义一个style,如下:

 

[html] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. <style name="PopToast">  
  2.     <item name="@android:windowEnterAnimation">@anim/anim_toast_enter</item>  
  3.     <item name="@android:windowExitAnimation">@anim/anim_toast_exit</item>  
  4. </style>  


最后可以按照如下的方式去使用:

 

 

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. ToastHelper  
  2.                     .makeText(this"hello world 你好,哈拉雷速度发说得对",  
  3.                             ToastHelper.LENGTH_SHORT)  
  4.                     .setAnimation(R.style.PopToast).show();  



 

三、参考资料:

SuperToast: https://github.com/JohnPersano/SuperToasts

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics