自定义Toast效果,windows层添加view,多个Toast效果

一直想写手游那种 疯狂点击 多Toast一起出现的快感。原生Toast其实有点卡,尤其是多次Toast的时候,而且不能一起出现。现在使用windows动态添加view的方法写了一个Toast
效果如下

  • -

使用方法 showMsgAdd(String msg, final Activity context)

如果资源文件没有 比如字体 可以暂时屏蔽

粉碎效果是用的https://github.com/lenve/ExplosionFieldTest/tree/master/ViewExplosion

核心代码 无非就是动态布局

/**
 * Created by  Rex on 2016/9/9.
 * 类似手游中的无限点击上浮的Toast
 */
public class RexToast {

    private static List<TextView> items = new ArrayList<>();
    private static LinearLayout ll;
    private static ExplosionField explosionField;

    public static void init(Activity context) {
        explosionField = ExplosionField.attach2Window(context);
        ;
//        View view = LayoutInflater.from(context).inflate();
//        当然,如果你想自定义一下这个动画效果,那就必须在代码中自己写了:新建一个LayoutTransition对象,调用setLayoutTransition()方法来为layout设置动画。
        ll = new LinearLayout(context);
        ll.setBackgroundColor(Color.TRANSPARENT);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        ll.setLayoutParams(layoutParams);
        ll.setPadding(0, 500, 0, 0);
        ll.setGravity(Gravity.CENTER_HORIZONTAL);
        ll.setOrientation(LinearLayout.VERTICAL);
        LayoutTransition layoutTransition = new LayoutTransition();
//or, if previously enabled in xml, use getLayoutTransition()

//enable animation
        ll.setLayoutTransition(layoutTransition);


//disable animation
//        setLayoutTransition(null);

        context.getWindow().addContentView(ll, new
                ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

    }

    public static void showMsgAdd(String msg, final Activity context) {
        if (ll == null) {
            init(context);
        }
        final TextView item = new TextView(context);
        item.setGravity(Gravity.CENTER);
        item.setTextColor(Color.parseColor("#EB7A77"));
        item.setText(msg);
        item.setPadding(5, 5, 5, 5);
        item.setTextSize(18);
        Typeface mTypeface = Typeface.createFromAsset(context.getAssets(), "fonts/bygf3500.ttf");
        item.setTypeface(mTypeface);
//        ll.setLayoutTransition();
//        android:animateLayoutChanges="true"

        ll.addView(item);
        final TranslateAnimation animation = new TranslateAnimation(item.getWidth(),
                item.getWidth() + 10, item.getHeight(), item.getHeight());

// 利用 CycleInterpolator 参数 为float 的数  表示 抖动的次数,而抖动的快慢是由 duration 和 CycleInterpolator 的参数的大小 联合确定的
        animation.setInterpolator(new CycleInterpolator(2f));
        animation.setDuration(1000);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                ll.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        explosionField.explode(item);
                        if (ll.getChildCount() > 0) {
                            ll.removeView(item);
//                            ll.postDelayed(this,1800);

                        }
                    }
                }, 1800);
                Log.i("rex", "onAnimationEnd");
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                Log.i("rex", "onAnimationRepeat");
            }
        });
        item.startAnimation(animation);


    }


}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值