有个需求是要做一个悬浮窗,从屏幕顶端外淡入飞进来,这是考虑到用WindowManager来实现。期间遇到个问题,无论如何都无法将View在WindowManager.addVIew的时候初始化在屏幕顶端外面。及设置WindowManager.LayoutParams.y为负值也没有用。
经过排查分析,原来在使用WindowManager显示View的时候,如果 WindowManager.LayoutParams.Type的层级值大于2007(TYPE_PRIORITY_PHONE)这时无论如何View都不可能绘制在屏幕之外。及WindowManager.LayoutParams.y设置为负值也没有用,效果最小值为0;
所以,如果有此需求
- 1、需排查flags是否为WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS 允许在屏幕外绘制
- 2、WindowManager.LayoutParams.Type的层级值是否大于2007(TYPE_PRIORITY_PHONE
windowManager = (WindowManager) context.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
layoutParams = new WindowManager.LayoutParams();
layoutView = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.floatview, null);
// 设置图片格式,效果为背景透明 (否则为黑色)
layoutParams.format = PixelFormat.RGBA_8888;
layoutParams.type = WindowManager.LayoutParams.TYPE_PRIORITY_PHONE;
layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
layoutParams.gravity = Gravity.TOP;
layoutParams.x = 0;
layoutParams.y = -DisplayUtil.dip2px(weakReferenceContext.get(), 200);
layoutParams.width = 1080;
layoutParams.height = DisplayUtil.dip2px(weakReferenceContext.get(), 200);
windowManager.addView(layoutView, layoutParams);