一、style样式:

  1、  // 移动和透明渐变结合的动画

  <style name="anim_view">
        <item name="@android:windowEnterAnimation">@anim/anim_in</item>
        <item name="@android:windowExitAnimation">@anim/anim_out</item>
    </style>

  anim_in.xml 文件:

  <?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

<translate
        android:duration="1"
        android:fromXDelta="0"
        android:fromYDelta="0"
        android:toXDelta="0"
        android:toYDelta="85" />
    <translate
        android:duration="350"
        android:fillAfter="true"
        android:fromXDelta="0"
        android:fromYDelta="0"
        android:toXDelta="0"
        android:interpolator="@android:anim/decelerate_interpolator"
        android:toYDelta="-105" />

<alpha
        android:duration="100"
        android:fromAlpha="0"
        android:toAlpha="1" />

<translate
        android:duration="80"
        android:fillAfter="true"
        android:fromXDelta="0"
        android:fromYDelta="0"
        android:startOffset="350"
        android:toXDelta="0"
        android:toYDelta="20" />
</set>

anim_out.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

<alpha
        android:duration="800"
        android:fromAlpha="1"
        android:toAlpha="0" />
</set>

2、   // 缩放效果的动画
    <style name="anim_view_scale">
        <item name="@android:windowEnterAnimation">@anim/lite_toast_enter</item>
        <item name="@android:windowExitAnimation">@anim/lite_toast_exit</item>
    </style>

  lite_toast_enter.xml 文件:

  <?xml version="1.0" encoding="utf-8"?>
<!-- 进入动画 -->
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="@android:integer/config_longAnimTime"
    android:fromXScale="0"
    android:toXScale="1"
    android:fromYScale="0"
    android:toYScale="1"
    android:pivotX="50%"
    android:pivotY="50%"
    android:interpolator="@android:anim/decelerate_interpolator" />

  lite_toast_exit.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<!-- 出去动画 -->
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="@android:integer/config_longAnimTime"
    android:fromXScale="1"
    android:toXScale="0"
    android:fromYScale="1"
    android:toYScale="0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:interpolator="@android:anim/decelerate_interpolator" />

二、自定义的Toast类文件

第一种方法是:

package com.bright.shuiyin.toast;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Handler;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.Toast;

@SuppressLint("ShowToast")
/** 自定义带动画的Toast */
public class MyCToast {
    /** 显示时长 */
    boolean mShowTime;
    long mShowTimeShort = 2000;
    long mShowTimeLong = 3500;
    /** 显示的 Y 轴所在的位置 */
    int mShowAtY = 140;
    /** 是否正在显示 */
    boolean mIsShow;
    WindowManager mWdm;
    View mToastView;
    LayoutParams mParams;
    /** 动画资源 */
    int mStyleId;

/**
     * @param context
     * @param text
     *            提示信息
     * @param showTime
     *            提示显示的时长,false为短(2000)、true为长(3500)
     */
    public MyCToast(Context context, String text, boolean showTime, int styleId) {
        // 记录Toast的显示长短类型
        this.mShowTime = showTime;
        // 记录当前Toast的内容是否已经在显示
        this.mIsShow = false;
        this.mWdm = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        // 通过Toast实例获取当前android系统的默认Toast的View布局
        this.mToastView = Toast.makeText(context, text, Toast.LENGTH_SHORT)
                .getView();
        this.mStyleId = styleId;
        setParams();
    }

/** 参数的添加:添加动画 */
    private void setParams() {
        mParams = new WindowManager.LayoutParams();
        mParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        mParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
        mParams.format = PixelFormat.TRANSLUCENT;
        mParams.windowAnimations = mStyleId;
        // 设置进入退出动画效果
        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;
        mParams.y = mShowAtY;
    }

/** 显示:addView */
    public void show() {
        // 如果Toast没有显示,则开始加载显示
        if (!mIsShow) {
            mIsShow = true;
            // 将其加载到windowManager上
            mWdm.addView(mToastView, mParams);
            cancelShow();
        }
    }

/** 获取 Toast 对象 */
    public static MyCToast makeText(Context context, String text,
            boolean showTime, int styleId) {
        MyCToast result = new MyCToast(context, text, showTime, styleId);
        return result;
    }

/** 退出显示:removeView */
    public void cancelShow() {
        if (mToastView != null) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    mWdm.removeView(mToastView);
                    mIsShow = false;
                }
            }, (long) (mShowTime ? 3500 : 2000)); // 这里设置显示的时长,之后执行 removeView
        }
    }
}

  第二种方法是:

package com.bright.shuiyin.toast;

import java.lang.reflect.Field;

import android.content.Context;
import android.view.WindowManager;
import android.widget.Toast;

/**
 * 使用反射自定义带动画的Toast
 */
public class MyToast extends Toast {

public MyToast(Context context) {
        super(context);
    }

/**
     * 调用有动画的Toast
     * @param context
     * @param text
     * @param duration
     * @param 自定义的动画id
     * @return
     */
    public static Toast makeTextAnim(Context context, CharSequence text,
            int duration, int styleId) {
        Toast toast = makeText(context, text, duration);
        toast.setText(text);
        toast.setDuration(duration);

try {
            Object mTN = null;
            mTN = getField(toast, "mTN");
            if (mTN != null) {
                Object mParams = getField(mTN, "mParams");
                if (mParams != null
                        && mParams instanceof WindowManager.LayoutParams) {
                    WindowManager.LayoutParams params = (WindowManager.LayoutParams) mParams;
                    params.windowAnimations = styleId;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

return toast;
    }

/**
     * 反射字段
     *
     * @param object
     *            要反射的对象
     * @param fieldName
     *            要反射的字段名称
     * @return
     * @throws NoSuchFieldException
     * @throws IllegalAccessException
     */
    private static Object getField(Object object, String fieldName)
            throws NoSuchFieldException, IllegalAccessException {
        Field field = object.getClass().getDeclaredField(fieldName);
        if (field != null) {
            field.setAccessible(true);
            return field.get(object);
        }
        return null;
    }
}

三、使用:

第一种的使用:

MyCToast.makeText(MyToastActivity.this, "MyCToast !", false, R.style.anim_view_scale).show();

第二种的使用:

MyToast.makeTextAnim(MyToastActivity.this, "自定义缩放Toast", 0, R.style.anim_view_scale).show();

自定义带动画的Toast的更多相关文章

  1. [转]Android UI:看看Google官方自定义带旋转动画的ImageView-----RotateImageView怎么写(附 图片淡入淡出效果)

    http://blog.csdn.net/yanzi1225627/article/details/22439119 众所周知,想要让ImageView旋转的话,可以用setRotation()让其围 ...

  2. 微信小程序之自定义模态弹窗(带动画)实例

    1.基本需求. 实现用户自定义弹框 带动画(动画可做参靠,个人要是觉得不好看可以自定义动画) 获取弹出框的内容,自定义事件获取 2.案例目录结构 二.程序实现具体步骤 1.弹框index.wxml代码 ...

  3. Android UI:看看Google官方自定义带旋转动画的ImageView-----RotateImageView怎么写(附 图片淡入淡...)

    众所周知,想要让ImageView旋转的话,可以用setRotation()让其围绕中心点旋转,但这个旋转是不带动画的,也就是旋转屏幕时图片噌的一下就转过去了,看不到旋转的过程,此UI体验不大好,为此 ...

  4. android标题栏上面弹出提示框(二) PopupWindow实现,带动画效果

    需求:上次用TextView写了一个从标题栏下面弹出的提示框.android标题栏下面弹出提示框(一) TextView实现,带动画效果,  总在找事情做的产品经理又提出了奇葩的需求.之前在通知栏显示 ...

  5. android标题栏下面弹出提示框(一) TextView实现,带动画效果

    产品经理用的是ios手机,于是android就走上了模仿的道路.做这个东西也走了一些弯路,写一篇博客放在这里,以后自己也可用参考,也方便别人学习. 弯路: 1.刚开始本来用PopupWindow去实现 ...

  6. Android实现自定义带文字和图片的Button

    Android实现自定义带文字和图片的Button 在Android开发中经常会需要用到带文字和图片的button,下面来讲解一下常用的实现办法. 一.用系统自带的Button实现 最简单的一种办法就 ...

  7. ToastCustom【自定义显示风格的Toast】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 基于系统Toast的自定义显示风格的Toast. 效果图 代码分析 ToastCustom类基于系统Toast,不是继承Toast, ...

  8. ToastMiui【仿MIUI的带有动画的Toast】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 仿MIUI的带有动画的Toast 效果图 代码分析 ToastMiui类基于WindowManager 为了和Toast用法保持一致 ...

  9. iOS学习笔记-自定义过渡动画

    代码地址如下:http://www.demodashi.com/demo/11678.html 这篇笔记翻译自raywenderlick网站的过渡动画的一篇文章,原文用的swift,由于考虑到swif ...

随机推荐

  1. NSUserDefaults 简介,使用 NSUserDefaults 存储自定义对象

    摘要: NSUserDefaults适合存储轻量级的本地数据,一些简单的数据(NSString类型的)例如密码,网址等,NSUserDefaults肯定是首选,但是如果我们自定义了一个对象,对象保存的 ...

  2. Mysql Specified key was too long; max key length is 767 bytes

    今天导入一个数据库时,看到以下报错信息: Specified key was too bytes 直译就是索引键太长,最大为767字节. 查看sql库表文件,发现有一列定义如下: 列   名:cont ...

  3. Javascript运用函数计算正方形的面积

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  4. heart beat/心跳包

    为什么需要heart beat/心跳包?因为tcp keep-alive不能满足人们的实时性的要求,就是这么简单. socket的长时间连接的话,是需要心跳包.心跳包就是维持双方的连接,每隔一段时间发 ...

  5. asp.net mvc bundle中数组超出索引

    在使用bundle 来加载css的时候报错了, @Styles.Render("~/bundles/appStyles") 第一反应 以为是的css 太多了,可是当我这个style ...

  6. css精灵

    ○ css 精灵(Sprites)技术利用photoshop将图片整合,然后用background-images,background-position,background-repeat技术,对图片 ...

  7. 《C++ Primer》学习笔记【第三部分 类设计者的工具】

    第13章 拷贝控制 使用default:=defult只能修饰默认构造函数或拷贝控制成员,显式地要去编译器生成合成的版本. 使用delete:=delete通知编译器不希望定义这些成员,禁止试图使用它 ...

  8. bootstrap 分页表格插件

    找了两个table的插件,一个是bootstrap table ,另一个是bootstrap-paginator 这里只介绍 bootstrap table 插件 使用及简单案例 文档介绍:http: ...

  9. 转:logBack.xml配置路径

    http://blog.csdn.net/z69183787/article/details/30284391 http://www.cppblog.com/fwxjj/archive/2012/08 ...

  10. eclipse下的,maven+spring+springMVC+mabatis+mysql.创建

    环境:window系统,64位工具:eclipse:下载地址:            链接:http://pan.baidu.com/s/1hr73LE8 密码:vcsa        tomcat: ...