zoukankan      html  css  js  c++  java
  • 自定义带动画的Toast

    一、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();

              

  • 相关阅读:
    P4374 [USACO18OPEN]Disruption P
    POJ
    Git
    SpringBoot集成RabbitMQ
    GIS类型文件剖析
    SpringBoot全局异常处理
    SpringCloud Feign异常处理
    SpringBoot注解
    Restful风格接口定义
    LOD技术的理解
  • 原文地址:https://www.cnblogs.com/BrightPoplar/p/5098951.html
Copyright © 2011-2022 走看看