关于安卓中 WindowManager.LayoutParams()的两种方式

本文详细介绍了Android中的WindowManager服务,包括其作用、如何实现悬浮窗口、获取屏幕大小以及改变Dialog背景透明度等操作,并提供了具体的代码示例。

一、WindowManager是什么

       WindowManager是Android中一个重要的Service,是全局且唯一的。WindowManager继承自ViewManager。主要用来管理窗口的一些状态、属性、view增加、删除、更新、窗口顺序、消息收集和处理等。通过Context.getSystemService(Context.WINDOW_SERVICE)的方式可以获得WindowManager的实例。Android中真正展示给用户的是window和view,activity所起的作用主要是处理一些逻辑问题,比如生命周期管理及建立窗口。

二、WindowManager的作用 (参考

(1)、实现悬浮窗口:

1.获取WindowManager服务:

WindowManager wmManager=(WindowManager) getSystemService(Context.WINDOW_SERVICE); 

2.设置WindowManager.LayoutParams参数

WindowManager.LayoutParams wmParams = new WindowManager.LayoutParams(); 

wmParams.type = LayoutParams.TYPE_PHONE; // 设置window type
      wmParams.format = PixelFormat.RGBA_8888; // 设置图片格式,效果为背景透明

wmParams.flags=LayoutParams.FLAG_NOT_TOUCH_MODAL | LayoutParams.FLAG_NOT_FOCUSABLE | LayoutParams.FLAG_NOT_TOUCHABLE;

wmParams.gravity = Gravity.RIGHT| Gravity. CENTER_VERTICAL; // 调整悬浮窗口至右侧中间
      wmParams.x = 0;// 以屏幕左上角为原点,设置x、y初始值
      wmParams.y = 0;

wmParams.width = WindowManager.LayoutParams.WRAP_CONTENT;// 设置悬浮窗口长宽数据
      wmParams.height =WindowManager.LayoutParams.WRAP_CONTENT;

3.添加view到屏幕

wmManager.addView(view,wmParams);

4.从屏幕上删除view

wmManager.removeView(view);

5.悬浮窗口需添加权限

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

 

(2)、通过WindowManager中的Display获取屏幕大小:

wmManager.getDefaultDisplay().getWidth();

wmManager.getDefaultDisplay().getHeight();

 

(3)、改变Dialog背景透明度:

Dialog dg = new Dialog(this); 

Window window = dg.getWindow(); 

WindowManager.LayoutParams lp = window.getAttributes(); 

lp.alpha = 0.5f; 

window.setAttributes(lp);


二、WindowManager的使用

WindowManager的方法很简单,基本用到的就三个addView,removeView,updateViewLayout  

在WindowManager中还有一个重要的静态类LayoutParams.通过它可以设置和获得当前窗口的一些属性。通过WindowManager的addView方法创建View,这样产生出来的View根据WindowManager.LayoutParams属性不同,效果也就不同了。比如创建系统顶级窗口,实现悬浮窗口效果(顶级窗口控)!


以悬浮窗口为例

addView方法用于添加一个悬浮窗,updateViewLayout方法用于更新悬浮窗的参数,removeView用于移除悬浮窗。其中悬浮窗的参数有必要详细说明一下。

WindowManager.LayoutParams这个类用于提供悬浮窗所需的参数,其中有几个经常会用到的变量:

type值用于确定悬浮窗的类型,一般设为2002,表示在所有应用程序之上,但在状态栏之下。

flags值用于确定悬浮窗的行为,比如说不可聚焦,非模态对话框等等,属性非常多,大家可以查看文档。

gravity值用于确定悬浮窗的对齐方式,一般设为左上角对齐,这样当拖动悬浮窗的时候方便计算坐标。

x值用于确定悬浮窗的位置,如果要横向移动悬浮窗,就需要改变这个值。

y值用于确定悬浮窗的位置,如果要纵向移动悬浮窗,就需要改变这个值。

width值用于指定悬浮窗的宽度。

height值用于指定悬浮窗的高度。


Button bb=new Button(this);
  bb.setText("nimei");
  
   //.获取WindowManager服务:"window"
  WindowManager wManager = (WindowManager)getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
  
      /**
         *设置WindowManager.LayoutParams的相关属性
         * 具体用途请参考SDK文档
         */
  WindowManager.LayoutParams wParams = new WindowManager.LayoutParams();

  wParams.type = LayoutParams.TYPE_PHONE ; //设置window type,type是关键,这里的"2002" 表示系统级窗口,你也可以试试2003
  wParams.format =  PixelFormat.RGBA_8888; //// 设置图片格式,1  效果为背景透明
  

  wParams.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL | LayoutParams.FLAG_NOT_FOCUSABLE | LayoutParams.FLAG_NOT_TOUCHABLE;
  wParams.gravity = Gravity.RIGHT| Gravity. CENTER_VERTICAL;// 调整悬浮窗口至右侧中间
  wParams.width = WindowManager.LayoutParams.WRAP_CONTENT;// 设置悬浮窗口长宽数据
  wParams.height =WindowManager.LayoutParams.WRAP_CONTENT;
  wManager.addView(bb, wParams);//bb添加view到屏幕
  
  //wManager.removeView(bb); //从屏幕上删除bb(view)
  //悬浮窗口需添加权限<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />


 

package com.example.myapplicationdemo4.listView10; import android.content.Context; import android.graphics.PixelFormat; import android.os.Build; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.WindowManager; import android.widget.TextView; import com.example.myapplicationdemo4.R; public class FloatingWindowManager { private Context context; private WindowManager windowManager; private View floatingView; // 关键:保存悬浮窗View的引用 private WindowManager.LayoutParams params; public FloatingWindowManager(Context context) { this.context = context.getApplicationContext(); windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); } // 创建悬浮窗 public void createFloatingWindow() { WindowManager.LayoutParams params = new WindowManager.LayoutParams( WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT, Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ? WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY : WindowManager.LayoutParams.TYPE_PHONE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN, PixelFormat.TRANSLUCENT); params.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL; params.x = 0; params.y = 0; // 关键:Inflate布局并保存引用 floatingView = LayoutInflater.from(context).inflate(R.layout.floating_layout, null); // 初始内容 TextView tvContent = floatingView.findViewById(R.id.tvContent); tvContent.setText("初始内容"); windowManager.addView(floatingView, params); } // 更新悬浮窗内容(核心方法) public void updateContent(String newText) { if (floatingView == null) return; // 通过保存的View引用直接修改内容 TextView tvContent = floatingView.findViewById(R.id.tvContent); tvContent.setText(newText); } // 移除悬浮窗 public void removeFloatingWindow() { if (floatingView != null) { windowManager.removeView(floatingView); floatingView = null; } } } 请帮我修改一下这个代码,来实现这个悬浮窗点击穿透到悬浮窗下层的界面
07-10
private void createSecureDisplayContent(int displayId) { DisplayManager dm = (DisplayManager) getSystemService(Context.DISPLAY_SERVICE); Display display = dm.getDisplay(displayId); if (display == null || !display.isValid()) { Log.e(TAG, "Invalid display: " + displayId); return; } try { Context displayContext = createDisplayContext(display); WindowManager wm = (WindowManager) displayContext.getSystemService(Context.WINDOW_SERVICE); View view = new View(displayContext); view.setBackgroundColor(Color.BLACK); // 设置布局参数 WindowManager.LayoutParams params = new WindowManager.LayoutParams( WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT, // 关键:使用比应用更低的层级 1010,//WindowManager.LayoutParams.TYPE_DISPLAY_OVERLAY // 关键:FLAG_SECURE 允许在安全屏显示,但不响应输入 WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_SECURE | WindowManager.LayoutParams.FLAG_FULLSCREEN, // 可选 PixelFormat.RGBA_8888 ); // 可选:设置窗口标题(调试用) params.setTitle("InstrumentBackground_" + displayId); // ✅ 5. 使用目标屏幕的 WindowManager 添加视图 wm.addView(view, params); Log.i(TAG, "成功在屏幕 (ID=" + displayId + ") 添加黑屏"); } catch (Exception e) { Log.e(TAG, "无法在 SECURE 屏幕添加视图", e); } }这个能不能遮住整个桌面,包括状态栏,但是不能遮住应用
08-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值