Android Toast显示动画

原理简介:

       直接在利用Toast做动画比较难实现,本文主要是利用PopupWindow实现的一个Toast仿造效果。

AnimationToast代码:

  1. public class AnimationToast  
  2. {  
  3.     static final String TAG = "AnimationToast";  
  4.     /** 
  5.      * Show the view or text notification for a short period of time.  This time 
  6.      * could be user-definable.  This is the default. 
  7.      * @see #setDuration 
  8.      */  
  9.     public static final int LENGTH_SHORT = 0;  
  10.   
  11.     /** 
  12.      * Show the view or text notification for a long period of time.  This time 
  13.      * could be user-definable. 
  14.      * @see #setDuration 
  15.      */  
  16.     public static final int LENGTH_LONG = 1;  
  17.       
  18.     final Context mContext;  
  19.     int mDuration;  
  20.     PopupWindow mPopToast;  
  21.     View mParent;  
  22.     int width = 300;//toast初始宽度  
  23.     int height = 80;//toast初始高度  
  24.       
  25.     public AnimationToast(Context context)  
  26.     {  
  27.         mContext = context;  
  28.     }  
  29.       
  30.     public void show()  
  31.     {  
  32.         mPopToast.showAtLocation(mParent, Gravity.CENTER, 00);  
  33.           
  34.         //LONG→2000ms    SHORT→1000ms  
  35.         long duration = mDuration== LENGTH_LONG ? 2000 : 1000;  
  36.           
  37.         mParent.postDelayed(new Runnable()  
  38.         {  
  39.             @Override  
  40.             public void run()  
  41.             {  
  42.                 cancel();  
  43.             }  
  44.         }, duration);  
  45.     }  
  46.       
  47.     public void cancel()  
  48.     {  
  49.         mPopToast.dismiss();  
  50.     }  
  51.       
  52.     /** 
  53.      * Set the view to show. 
  54.      * @see #getView 
  55.      */  
  56.     public void setView(View view)   
  57.     {  
  58.         mPopToast = new PopupWindow(view, width, height);  
  59.         mPopToast.setAnimationStyle(R.style.AnimationToast);  
  60.         mPopToast.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.toast_frame));  
  61.         mPopToast.setFocusable(false);  
  62.         mPopToast.setOutsideTouchable(true);  
  63.     }  
  64.   
  65.     /** 
  66.      * Return the view. 
  67.      * @see #setView 
  68.      */  
  69.     public View getView()   
  70.     {  
  71.         return mPopToast.getContentView();  
  72.     }  
  73.       
  74.     public void setParent(View parent)  
  75.     {  
  76.         mParent = parent;  
  77.     }  
  78.       
  79.     public View getParent()  
  80.     {  
  81.         return mParent;  
  82.     }  
  83.   
  84.     /** 
  85.      * Set how long to show the view for. 
  86.      * @see #LENGTH_SHORT 
  87.      * @see #LENGTH_LONG 
  88.      */  
  89.     public void setDuration(int duration)   
  90.     {  
  91.         mDuration = duration;  
  92.     }  
  93.   
  94.     /** 
  95.      * Return the duration. 
  96.      * @see #setDuration 
  97.      */  
  98.     public int getDuration()   
  99.     {  
  100.         return mDuration;  
  101.     }  
  102.       
  103.     public void setWidth(int w)  
  104.     {  
  105.         width = w;  
  106.     }  
  107.       
  108.     public void setHeight(int h)  
  109.     {  
  110.         height =h;  
  111.     }  
  112.       
  113.     /** 
  114.      * Make a standard toast that just contains a text view. 
  115.      * 
  116.      * @param context  The context to use.  Usually your {@link android.app.Application} 
  117.      *                 or {@link android.app.Activity} object. 
  118.      * @param text     The text to show.  Can be formatted text. 
  119.      * @param duration How long to display the message.  Either {@link #LENGTH_SHORT} or 
  120.      *                 {@link #LENGTH_LONG} 
  121.      * @param parent AnimationToast use a PopupWindow, so need a parent. 
  122.      *                  suggestion → using activity.getWindow().getDecorView() 
  123.      * 
  124.      */  
  125.     public static AnimationToast makeText(Context context, CharSequence text, int duration, View parent)   
  126.     {  
  127.         AnimationToast result = new AnimationToast(context);  
  128.   
  129.         TextView tv = new TextView(context);  
  130.         tv.setTextColor(Color.WHITE);  
  131.         tv.setGravity(Gravity.CENTER);  
  132.         tv.setText(text);  
  133.           
  134.         result.setView(tv);  
  135.         result.setParent(parent);  
  136.         result.setDuration(duration);  
  137.   
  138.         return result;  
  139.     }  
  140.   
  141.     /** 
  142.      * Make a standard toast that just contains a text view with the text from a resource. 
  143.      * 
  144.      * @param context  The context to use.  Usually your {@link android.app.Application} 
  145.      *                 or {@link android.app.Activity} object. 
  146.      * @param resId    The resource id of the string resource to use.  Can be formatted text. 
  147.      * @param duration How long to display the message.  Either {@link #LENGTH_SHORT} or 
  148.      *                 {@link #LENGTH_LONG} 
  149.      * @param parent AnimationToast use a PopupWindow, so need a parent. 
  150.      *                 suggestion → using activity.getWindow().getDecorView() 
  151.      * 
  152.      * @throws Resources.NotFoundException if the resource can't be found. 
  153.      */  
  154.     public static AnimationToast makeText(Context context, int resId, int duration, View parent)  
  155.                                 throws Resources.NotFoundException {  
  156.         return makeText(context, context.getResources().getText(resId), duration, parent);  
  157.     }  
  158. }  

测试代码:

  1. public class TestActivity extends Activity  
  2. {  
  3.     @Override  
  4.     protected void onCreate(Bundle savedInstanceState)  
  5.     {  
  6.         super.onCreate(savedInstanceState);  
  7.           
  8.         setContentView(R.layout.activity_test);  
  9.           
  10.         Button button = (Button)findViewById(R.id.bt_show_toast);  
  11.         button.setOnClickListener(listener);  
  12.     }  
  13.       
  14.     private View.OnClickListener listener = new View.OnClickListener()  
  15.     {  
  16.         @Override  
  17.         public void onClick(View v)  
  18.         {  
  19.             AnimationToast.makeText(  
  20.                     TestActivity.this,   
  21.                     "动画Toast",  
  22.                     AnimationToast.LENGTH_LONG,   
  23.                     TestActivity.this.getWindow().getDecorView()  
  24.                     ).show();  
  25.         }  
  26.     };  
  27. }  
AnimationToast由于利用PopupWindow的动画效果,所以引用了两个资源文件:

styles中:

  1. <style name="AnimationToast" mce_bogus="1" parent="android:Animation">  
  2.     <item name="android:windowEnterAnimation">@anim/animation_toast_enter</item>  
  3.     <item name="android:windowExitAnimation">@anim/animation_toast_exit</item>  
  4. </style>  

anim文件中:

      animation_toast_enter.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.   
  4.     <alpha  
  5.         android:duration="500"  
  6.         android:fromAlpha="0.0"  
  7.         android:interpolator="@android:anim/decelerate_interpolator"  
  8.         android:toAlpha="1.0" />  
  9.   
  10.     <translate  
  11.         android:duration="500"  
  12.         android:fromXDelta="0"  
  13.         android:fromYDelta="50%p"  
  14.         android:toXDelta="0"  
  15.         android:toYDelta="0" />  
  16.   
  17. </set>  
      animation_toast_exit.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.   
  4.     <translate  
  5.         android:duration="500"  
  6.         android:fromXDelta="0"  
  7.         android:fromYDelta="0"  
  8.         android:toXDelta="0"  
  9.         android:toYDelta="50%p" />  
  10.   
  11.     <alpha  
  12.         android:duration="500"  
  13.         android:fromAlpha="1.0"  
  14.         android:interpolator="@android:anim/decelerate_interpolator"  
  15.         android:toAlpha="0.0" />  
  16.   
  17. </set>  


效果如下:

另一种动画:

自定义的Anim:

1.显示Toast的动画:

[html]  view plain copy 在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 plain copy 在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>  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一些Android Java Toast自定义动画的代码,以下是示例代码: 1. 在res目录下新建anim文件夹(如果没有的话),并在该文件夹下新建一个xml文件,例如:toast_slide_in.xml,其内容如下: ``` <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="500" android:fromXDelta="100%" android:toXDelta="0%" /> </set> ``` 2. 然后在res/drawable目录下创建背景文件,例如:toast_custom_bg.xml,其内容如下: ``` <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <corners android:radius="50dp" /> <solid android:color="#FF4081" /> <padding android:bottom="10dp" android:left="12dp" android:right="12dp" android:top="10dp" /> </shape> ``` 3. 然后在Java代码中创建一个自定义的Toast,代码如下: ``` public class CustomToast { public static void show(Context context, String message) { LayoutInflater inflater = LayoutInflater.from(context); View layout = inflater.inflate(R.layout.toast_layout, null, false); TextView text = layout.findViewById(R.id.toast_text); ImageView icon = layout.findViewById(R.id.toast_icon); //设置文本和图标 text.setText(message); icon.setImageResource(R.drawable.ic_launcher); Toast toast = new Toast(context); toast.setGravity(Gravity.BOTTOM, 0, 100); toast.setView(layout); toast.setDuration(Toast.LENGTH_LONG); //设置动画 Animation slideIn = AnimationUtils.loadAnimation(context, R.anim.toast_slide_in); toast.getView().startAnimation(slideIn); toast.show(); } } ``` 4. 最后,调用CustomToast.show()方法即可展示自定义的Toast,如下所示: ``` CustomToast.show(this, "这是一条自定义的Toast"); ``` 以上是示例代码,您可以根据需要修改和调整。希望能对您有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值