模仿 (微信)消息通知效果


效果:


public void showNotification() {

    // 获取Service
    final WindowManager mWindowManager = (WindowManager) getApplication().getSystemService(Context.WINDOW_SERVICE);
    LayoutInflater layoutInflater = this.getLayoutInflater();
    final View view = layoutInflater.inflate(R.layout.activity_giftmoney_ranklist_footer_item, null);
    final ObjectAnimator o = ObjectAnimator.ofFloat(view, "translationY", -150, 0f);
    o.addListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
            super.onAnimationEnd(animation);

            final ObjectAnimator o2 = ObjectAnimator.ofFloat(view, "translationY", 0f, -150f);
            o2.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    super.onAnimationEnd(animation);
                    mWindowManager.removeView(view);
                    o2.cancel();
                }
            });

            o2.setDuration(1000).setStartDelay(1500);//执行一秒 , 延迟1.5秒
            o2.start();
            o.cancel();

        }
    });
    o.setDuration(1000).start();


    // 设置窗口类型,一共有三种Application windows, Sub-windows, System windows
    WindowManager.LayoutParams mWindowParams = new WindowManager.LayoutParams();
    // API中以TYPE_开头的常量有23个
    mWindowParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
    mWindowParams.flags = WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
    ;
    // 设置期望的bitmap格式
    mWindowParams.format = PixelFormat.TRANSLUCENT; //设置背景透明

    // 以下属性在Layout Params中常见重力、坐标,宽高
    mWindowParams.gravity = Gravity.LEFT | Gravity.TOP;

    mWindowParams.x = 0;
    mWindowParams.y = 0;

    mWindowParams.width = WindowManager.LayoutParams.MATCH_PARENT;
    mWindowParams.height = 150;//WindowManager.LayoutParams. WRAP_CONTENT;

    // 添加指定视图
    mWindowManager.addView(view, mWindowParams);
}

使用: 直接调用方法即可 ,例如:

onclick{

showNotification();

注意:如果没有效果 ,则可能是手机设置问题,可能屏蔽了“悬浮通知” 去设置中设置即可。




  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值