一、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. 如何运用CSS写小三角

    <html> <div class="con"></div> </html> <style> .con{width:0; ...

  2. Win8下Visual Studio编译报“无法注册程序集***dll- 拒绝访问。请确保您正在以管理员身份运行应用程序。对注册表项”***“的访问被拒绝。”问题修正(转)

    原来在Win7下Visual Studio跑的好好的程序,现在在Win8下编译报“无法注册程序集***dll- 拒绝访问.请确保您正在以管理员身份运行应用程序.对注册表项”***“的访问被拒绝.”的错 ...

  3. item布局里有button之类的子控件时,会截获焦点

    需要以上问题时,网上查了很多,结果都是使用android:descendantFocusability属性,设为blocksDescendants就ok了,(具体不详叙述) 可是我给item的根布局设 ...

  4. nginx基本配置和参数说明

    #运行用户user nobody;#启动进程,通常设置成和cpu的数量相等worker_processes 1; #全局错误日志及PID文件#error_log logs/error.log;#err ...

  5. jQuery(一)

    1,浏览器内核不同-->兼容性问题-->不同浏览器相对应不同代码 2,javascript框架, 只写代码,不用考虑浏览器兼容问题  prototype.mootools.jQuery(目 ...

  6. hadoop修改MR的提交的代码程序的副本数

    hadoop修改MR的提交的代码程序的副本数 Under-Replicated Blocks的数量很多,有7万多个.hadoop fsck -blocks 检查发现有很多replica missing ...

  7. YYYY-mm-dd HH:MM:SS

    备忘:YYYY-mm-dd HH:MM:SS部分解释 d               月中的某一天.一位数的日期没有前导零.    dd             月中的某一天.一位数的日期有一个前导零 ...

  8. Android Fragment 深度解析

    1.Fragment的产生与介绍 Android运行在各种各样的设备中,有小屏幕的手机,超大屏的平板甚至电视.针对屏幕尺寸的差距,很多情况下,都是先针对手机开发一套app,然后拷贝一份,修改布局以适应 ...

  9. Apache 403 error, (13)Permission denied: access to / denied问题

    Apache 配置Alias 后,无法访问 CentOS系统 检查了一圈httpd.conf和目录权限,均没有发现问题. 最后,看了这篇文章,发现是因为系统启动了SELINUX导致的. http:// ...

  10. 软件工程线上课程(C语言实践篇)学习心得总结

    林牧 + 原创作品转载请注明出处 + <软件工程(C编码实践篇)>MOOC课程http://mooc.study.163.com/course/USTC-1000002006 软件工程的理 ...