WindowManager工具类

WindowManager提供三个方法: addView()、updateLayout()、removeView()。分别对应是添加view、更新view、移除view。

    <!--悬浮窗权限--><uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

WindowManagerUtil 

package cn.jzvd.demo.utils;import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.PixelFormat;
import android.hardware.display.DisplayManager;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;import java.util.ArrayList;
import java.util.Collection;
import java.util.List;/*** 注意申请悬浮窗权限*/
public class WindowManagerUtil {private final String TAG = "GuiViewManager";private final List<WindowBean> mWindowBeans = new ArrayList<>();private static volatile GuiViewManager mInstance = null;private WindowBean mPreWindowBean;private Context mContext = null;private int mType = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;public void init(Context context) {mContext = context;createWindowManager();}public void setWindowType(int windowType) {mType = windowType;}private WindowManagerUtil() {}public static WindowManagerUtil getInstance() {if (mInstance == null) {synchronized (WindowManagerUtil.class) {if (mInstance == null) {mInstance = new WindowManagerUtil();}}}return mInstance;}/*** 获取WindowManager** @return*/private void createWindowManager() {try {Display[] displays = ((DisplayManager) mContext.getSystemService(Context.DISPLAY_SERVICE)).getDisplays();LogUtil.d(TAG,"displays.length " + displays.length);//displays.length<2说明只有一个屏幕if (null != displays) {for (Display display : displays) {Context context = mContext.createDisplayContext(display);WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);DisplayMetrics outMetrics = new DisplayMetrics();windowManager.getDefaultDisplay().getMetrics(outMetrics);WindowManager.LayoutParams lp = new WindowManager.LayoutParams();lp.type = mType;lp.flags = WindowManager.LayoutParams.FLAG_FULLSCREEN| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE| WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS| WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;//lp.width = WindowManager.LayoutParams.MATCH_PARENT;//lp.height = WindowManager.LayoutParams.MATCH_PARENT;lp.format = PixelFormat.TRANSPARENT;//将alpha设置为最大遮挡不透明度//lp.alpha = 0.8f;lp.gravity = Gravity.TOP | Gravity.START;mWindowBeans.add(new WindowBean(display.getDisplayId(), windowManager, lp));}}} catch (Exception e) {LogUtil.e(TAG,"createWindowManager  error " + e);}}/*** 获取状态栏高度** @return*/private int getStatusBarHeight() {int statusBarHeight = -1;//获取status_bar_height资源的ID@SuppressLint("InternalInsetResource")int resourceId = mContext.getResources().getIdentifier("status_bar_height", "dimen", "android");if (resourceId > 0) {//根据资源ID获取响应的尺寸值statusBarHeight = mContext.getResources().getDimensionPixelSize(resourceId);}return statusBarHeight;}public void show(View view, int width, int height, int startX, int startY) {//目前不考虑两个屏幕,在只有一个屏幕的情况下,displayId为0show(0, view, width, height, startX, startY);}public void show(int displayId, View view, int width, int height, int startX, int startY) {LogUtil.d(TAG, "show width " + width +" height " +height + " startX " + startX +" startY " +startY );try {WindowBean windowBean = getWindowBean(displayId);if (windowBean != null) {WindowManager windowManager = windowBean.getWindowManager();WindowManager.LayoutParams lp = windowBean.getLp();if (windowManager != null && lp != null) {dismiss();lp.x = startX;lp.y = startY;lp.width = width;lp.height = height;lp.type = mType;windowManager.addView(view, lp);windowBean.setView(view);mPreWindowBean = windowBean;}}} catch (Exception e) {LogUtil.e(TAG, "show error ", e);}}public void dismiss() {if (mPreWindowBean != null) {WindowManager windowManager = mPreWindowBean.getWindowManager();View view = mPreWindowBean.getView();if (windowManager != null && view != null) {windowManager.removeView(view);}mPreWindowBean = null;}}public WindowBean getWindowBean(int displayId) {if (isEmptyArray(mWindowBeans)) {createWindowManager();}for (WindowBean windowBean : mWindowBeans) {if (displayId == windowBean.getDisplayId()) {return windowBean;}}return null;}private boolean isEmptyArray(Collection list) {return list == null || list.isEmpty();}public void destroy() {mWindowBeans.clear();mContext = null;}
}

WindowBean 

public class WindowBean {private int displayId;private WindowManager windowManager;private WindowManager.LayoutParams lp;private View view;public WindowBean(int displayId, WindowManager windowManager, WindowManager.LayoutParams lp) {this.displayId = displayId;this.windowManager = windowManager;this.lp = lp;}public int getDisplayId() {return this.displayId;}public WindowManager getWindowManager() {return this.windowManager;}public WindowManager.LayoutParams getLp() {return this.lp;}public View getView() {return this.view;}public void setView(View view) {this.view = view;}
}

其他工具类大家搜索一个就可以了。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/779361.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【ENSP】交换机和交换机之间实现静态路由

1.概念 三层交换机只能在Vlanif逻辑口配置iP地址 2.实现方法 交换机允许对应vlan通行&#xff0c;配置vlanif的ip地址&#xff0c;做静态路由 3.静态路由配置方法 ip route-static 目的网段 子网掩码 下一跳设备 LSW1三层交换机配置 u t m sys vlan batch 10 20 …

HarmonyOS 应用开发之UIAbility组件生命周期

概述 当用户打开、切换和返回到对应应用时&#xff0c;应用中的UIAbility实例会在其生命周期的不同状态之间转换。UIAbility类提供了一系列回调&#xff0c;通过这些回调可以知道当前UIAbility实例的某个状态发生改变&#xff0c;会经过UIAbility实例的创建和销毁&#xff0c;…

LeetCode_1.两数之和

一、题目描述 二、方法 1.方法1&#xff08;暴力枚举法&#xff09; 利用两个for循环&#xff0c;对数组进行逐一的遍历&#xff0c;直到找到两个数的和为目标值时返回这两个数的下标。以下为c实现的完整代码。 # include<iostream> using namespace std; #include<…

【Linux】nmcli命令详解(文末送书)

目录 一、概述 二、常用参数使用 2.1 nmcli networking 1.显示NM是否接管网络 2.查看网络连接状态 3.开/关网络连接 2.2 general ​编辑 1.显示系统网络状态 2.显示主机名 3.更改主机名 2.3 nmcli connection ​编辑1.显示所有网络连接 2.显示某个网卡的详细信息…

Linux文件IO(2):使用标准IO进行文件的打开、关闭、读写、流定位等相关操作

目录 前言 文件的打开和关闭的概念 文件的打开 文件的打开函数 文件打开的模式 文件的关闭 文件的关闭函数 注意事项 字符的输入&#xff08;读单个字符&#xff09; 字符输入的函数 注意事项 字符的输出&#xff08;写单个字符&#xff09; 字符输出的函数 注意…

探索海外应用加速的作用与优势

随着互联网的快速发展&#xff0c;海外应用加速作为一种提高网络连接速度和性能的技术手段越来越受到关注。那么&#xff0c;海外应用加速究竟有什么作用呢&#xff1f;以下是这种加速技术的主要作用&#xff1a; 降低延迟&#xff1a; 海外应用加速在降低数据传输延迟方面发挥…

项目模块—实现抑郁测评(小程序)

script <script setup> import { ref } from "vue";//控制轮播图页码 let current ref(0);//答题逻辑 const add (value) > {if (current.value < 9) {current.value current.value 1;} else {uni.switchTab({url: "/pages/my/my",});} }…

双端队列deque和vector以及list的优缺点比较

参考:https://blog.csdn.net/TWRenHao/article/details/123483085 一、vector vector具体用法详情点这里 优点&#xff1a; 支持随机访问 CPU高速环缓存命中率很高 缺点&#xff1a; 空间不够&#xff0c;便需要增容。而增容代价很大&#xff0c;还存在一定的空间浪费。 头部…

redis在docker安装并启动流程

1、启动server docker run -d -p 6379:6379 --name redis01 redis:7.2.4以上命令&#xff0c;每次启动新的Redis容器&#xff0c;数据会丢失。 我们需要挂载数据文件&#xff0c;在宿主机上面&#xff0c;这样就可以持久化数据. 2、挂载数据文件&#xff08;可根据需求选择…

Git相关命令(一)

一、简介 Git 是一个开源的分布式版本控制系统。 当然&#xff0c; git 不会傻傻的把你的每一个版本完整的存储下来&#xff0c;他仅仅会存储每次修改的位置和内容&#xff08;可持久化&#xff09;&#xff0c;每一次 commit 可以理解为产生一个版本&#xff0c;接下来的版本…

vivado 生成比特流或器件镜像

在生成比特流或器件镜像之前 &#xff0c; 请复查其设置 &#xff0c; 确保这些设置对于您的设计都正确无误 &#xff0c; 这一点至关重要。 Vivado IDE 中的比特流和器件镜像设置分为 2 种类型 &#xff1a; 1. 比特流或器件镜像文件格式设置。 2. 器件配置设置。 在 V…

数据结构刷题篇 之 【力扣二叉树基础OJ】详细讲解(含每道题链接及递归图解)

有没有一起拼用银行卡的&#xff0c;取钱的时候我用&#xff0c;存钱的时候你用 1、相同的树 难度等级&#xff1a;⭐ 直达链接&#xff1a;相同的树 2、单值二叉树 难度等级&#xff1a;⭐ 直达链接&#xff1a;单值二叉树 3、对称二叉树 难度等级&#xff1a;⭐⭐ 直达…

【Godot4自学手册】第三十一节使用WorldEnvironment为地宫入口粒子系统添加辉光

本节&#xff0c;首先我将使用WorldEnvironment节点为地宫入口的例子系统添加辉光&#xff0c;让游戏看上去效果更加灿烂。其次加入相应提示信息&#xff0c;白天到达地宫附近、未杀死怪物进入地宫&#xff0c;都有提示信息&#xff0c;达到条件后地宫方可进入。先看一下效果&a…

CSS之动画

一&#xff0c;动画的制作 实现盒子绕圈走 二&#xff0c; 动画的常用属性 三&#xff0c;动画简写属性 前面两个属性一定要写&#xff0c;第三个linear是指匀速的意思&#xff08;默认是ease&#xff09;

Machine Learning机器学习之数据可视化

目录 前言 一、 数据预处理与清洗 二、常见可视化技术 三、可视化工具和平台 博主介绍&#xff1a;✌专注于前后端、机器学习、人工智能应用领域开发的优质创作者、秉着互联网精神开源贡献精神&#xff0c;答疑解惑、坚持优质作品共享。本人是掘金/腾讯云/阿里云等平台优质作者…

tls和ssl的区别,ssh和ssl区别

在网络通信和安全领域&#xff0c;TLS&#xff08;Transport Layer Security&#xff09;、SSL&#xff08;Secure Sockets Layer&#xff09;和SSH&#xff08;Secure Shell&#xff09;是常见的加密协议&#xff0c;它们都起着保护数据安全的重要作用。在本文中&#xff0c;我…

PPP、RRE、MGRE综合实验

一、实验拓扑图 二、实验要求 1.R5为ISP&#xff0c;只能进行IP地址配置&#xff0c;其所有地址均配为公有IP地址&#xff1b; 2.R1和R5间使用PPP的PAP认证&#xff0c;R5为主认证方: R2与R5之间使用ppp的CHAP认证&#xff0c; R5为主认证方;R3与R5之间使用HDLC封装; 3.R1、R2、…

MSTP环路避免实验(思科)

华为设备参考&#xff1a;MSTP环路避免实验&#xff08;华为&#xff09; 一&#xff0c;技术简介 MSTP&#xff08;多生成树协议&#xff09;&#xff0c;MSTP解决了STP和RSTP没有考虑vlan的问题&#xff0c;STP和RSTP将所有的vlan共享为一个生成树实例&#xff0c;无法实现…

linux提权笔记

1 linux提权简介 Linux提权&#xff0c;简单来说&#xff0c;就是用户尝试获取高于其当前权限级别的系统访问权限的过程。在Linux系统中&#xff0c;root用户拥有最高的权限&#xff0c;能够执行任何操作&#xff0c;包括修改系统文件、安装软件、管理用户账户等。而普通用户通…

岭师大数据技术原理与应用-序章-软工版

HeZaoCha-CSDN博客 序章—软工版 一、环境介绍1. VMware Workstation Pro2. CentOS3. Java4. Hadoop5. HBase6. MySQL7. Hive 二、系统安装1. 虚拟网络编辑器2. 操作系统安装 三、结尾 先说说哥们写这系列博客的原因&#xff0c;本来学完咱也没想着再管部署这部分问题的说&…