效果:
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();
}
注意:如果没有效果 ,则可能是手机设置问题,可能屏蔽了“悬浮通知” 去设置中设置即可。