码迷,mamicode.com
首页 > 其他好文 > 详细

自定义Toast,从顶部掉下,然后再弹一下

时间:2015-08-18 19:27:34      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:自定义toast   toast动画   

先把自定义类赠上,如果您觉得有用,请赞!

import java.util.Timer;
import java.util.TimerTask;

import com.example.viewpageindicator.R;

import android.content.Context;
import android.graphics.PixelFormat;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.Toast;

public class MyToast {

    private LayoutParams mParams;
    private boolean mShowTime;
    private boolean mIsShow;
    private WindowManager mWdm;
    private View mToastView;
    private Timer mTimer;
    private MyToast(Context context, String text, boolean showTime ){
          mShowTime = showTime;//记录Toast的显示长短类型
          mIsShow = false;//记录当前Toast的内容是否已经在显示
          mWdm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
          //通过Toast实例获取当前android系统的默认Toast的View布局
          mToastView = Toast.makeText(context, text, Toast.LENGTH_SHORT).getView();
          mTimer = new Timer();
          //设置布局参数
          setParams();
        }

    public static MyToast MakeText(Context context, String text, boolean showTime) {
        MyToast result = new MyToast(context, text, showTime);
        return result;
    }
    private void setParams() {
        // TODO Auto-generated method stub
         mParams = new WindowManager.LayoutParams();
          mParams.height = WindowManager.LayoutParams.WRAP_CONTENT;  
          mParams.width = WindowManager.LayoutParams.WRAP_CONTENT;  
          mParams.format = PixelFormat.TRANSLUCENT;  
          mParams.windowAnimations = R.style.anim_view;//设置进入退出动画效果
          mParams.type = WindowManager.LayoutParams.TYPE_TOAST;  
          mParams.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON  
              | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE  
              | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
          mParams.gravity = Gravity.CENTER_HORIZONTAL|Gravity.BOTTOM;
          mParams.y = 200;
    }
    public void show(){
          if(!mIsShow){//如果Toast没有显示,则开始加载显示
            mIsShow = true;
            mWdm.addView(mToastView, mParams);//将其加载到windowManager上
            mTimer.schedule(new TimerTask() {
              @Override
              public void run() {
                mWdm.removeView(mToastView);
                mIsShow = false;
              }
            }, (long)(mShowTime ? 3500 : 2000));
          }
        }
}

动画xml anim_view

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="anim_view">
        <item name="@android:windowEnterAnimation">@anim/anim_in</item>
        <item name="@android:windowExitAnimation">@anim/anim_out</item>
    </style>
</resources>

anim_in 动画xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
  <translate
    android:fromXDelta="0"
    android:fromYDelta="0"
    android:toXDelta="0"
    android:toYDelta="100"
    android:duration="100"
    />
  <translate
    android:fromXDelta="0"
    android:fromYDelta="0"
    android:toXDelta="0"
    android:toYDelta="-200"
    android:duration="150"
    android:fillAfter="true"
    android:interpolator="@android:anim/decelerate_interpolator"
    />
    <translate
    android:fromXDelta="0"
    android:fromYDelta="0"
    android:toXDelta="0"
    android:toYDelta="200"
    android:duration="100"
    />
      <translate
    android:fromXDelta="0"
    android:fromYDelta="0"
    android:toXDelta="0"
    android:toYDelta="-100"
    android:duration="150"
    android:fillAfter="true"
    android:interpolator="@android:anim/decelerate_interpolator"
    />
  <alpha 
    android:fromAlpha="0"
    android:toAlpha="1"
    android:duration="150"
    />

</set>

anim_out 动画xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha 
        android:fromAlpha="1"
        android:toAlpha="0"
        android:duration="800"/>
</set>

淡出界面!

调用方法 第三个参数为true和false true则3500毫秒false则2000毫秒

版权声明:本文为博主原创文章,未经博主允许不得转载。

自定义Toast,从顶部掉下,然后再弹一下

标签:自定义toast   toast动画   

原文地址:http://blog.csdn.net/a4384142/article/details/47755275

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!