Android WindowManager工具类

2024-03-31 19:52

本文主要是介绍Android 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;}
}

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

推荐几个:

浮窗中addView()不显示 分析思路_android windowmanager addview后窗口不显示-CSDN博客

这篇关于Android WindowManager工具类的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/865194

相关文章

基于Python实现一个Windows Tree命令工具

《基于Python实现一个WindowsTree命令工具》今天想要在Windows平台的CMD命令终端窗口中使用像Linux下的tree命令,打印一下目录结构层级树,然而还真有tree命令,但是发现... 目录引言实现代码使用说明可用选项示例用法功能特点添加到环境变量方法一:创建批处理文件并添加到PATH1

使用jenv工具管理多个JDK版本的方法步骤

《使用jenv工具管理多个JDK版本的方法步骤》jenv是一个开源的Java环境管理工具,旨在帮助开发者在同一台机器上轻松管理和切换多个Java版本,:本文主要介绍使用jenv工具管理多个JD... 目录一、jenv到底是干啥的?二、jenv的核心功能(一)管理多个Java版本(二)支持插件扩展(三)环境隔

Python使用smtplib库开发一个邮件自动发送工具

《Python使用smtplib库开发一个邮件自动发送工具》在现代软件开发中,自动化邮件发送是一个非常实用的功能,无论是系统通知、营销邮件、还是日常工作报告,Python的smtplib库都能帮助我们... 目录代码实现与知识点解析1. 导入必要的库2. 配置邮件服务器参数3. 创建邮件发送类4. 实现邮件

CnPlugin是PL/SQL Developer工具插件使用教程

《CnPlugin是PL/SQLDeveloper工具插件使用教程》:本文主要介绍CnPlugin是PL/SQLDeveloper工具插件使用教程,具有很好的参考价值,希望对大家有所帮助,如有错... 目录PL/SQL Developer工具插件使用安装拷贝文件配置总结PL/SQL Developer工具插

Android学习总结之Java和kotlin区别超详细分析

《Android学习总结之Java和kotlin区别超详细分析》Java和Kotlin都是用于Android开发的编程语言,它们各自具有独特的特点和优势,:本文主要介绍Android学习总结之Ja... 目录一、空安全机制真题 1:Kotlin 如何解决 Java 的 NullPointerExceptio

Python使用FFmpeg实现高效音频格式转换工具

《Python使用FFmpeg实现高效音频格式转换工具》在数字音频处理领域,音频格式转换是一项基础但至关重要的功能,本文主要为大家介绍了Python如何使用FFmpeg实现强大功能的图形化音频转换工具... 目录概述功能详解软件效果展示主界面布局转换过程截图完成提示开发步骤详解1. 环境准备2. 项目功能结

Linux系统之stress-ng测压工具的使用

《Linux系统之stress-ng测压工具的使用》:本文主要介绍Linux系统之stress-ng测压工具的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、理论1.stress工具简介与安装2.语法及参数3.具体安装二、实验1.运行8 cpu, 4 fo

Maven项目中集成数据库文档生成工具的操作步骤

《Maven项目中集成数据库文档生成工具的操作步骤》在Maven项目中,可以通过集成数据库文档生成工具来自动生成数据库文档,本文为大家整理了使用screw-maven-plugin(推荐)的完... 目录1. 添加插件配置到 pom.XML2. 配置数据库信息3. 执行生成命令4. 高级配置选项5. 注意事

Python使用pynput模拟实现键盘自动输入工具

《Python使用pynput模拟实现键盘自动输入工具》在日常办公和软件开发中,我们经常需要处理大量重复的文本输入工作,所以本文就来和大家介绍一款使用Python的PyQt5库结合pynput键盘控制... 目录概述:当自动化遇上可视化功能全景图核心功能矩阵技术栈深度效果展示使用教程四步操作指南核心代码解析

如何基于Python开发一个微信自动化工具

《如何基于Python开发一个微信自动化工具》在当今数字化办公场景中,自动化工具已成为提升工作效率的利器,本文将深入剖析一个基于Python的微信自动化工具开发全过程,有需要的小伙伴可以了解下... 目录概述功能全景1. 核心功能模块2. 特色功能效果展示1. 主界面概览2. 定时任务配置3. 操作日志演示