Android WindowManager工具类

简介: 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为0
        show(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;
    }
}

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

相关文章
|
3月前
|
API Android开发 数据安全/隐私保护
Android经典实战之窗口和WindowManager
本文介绍了Android开发中“窗口”的基本概念及其重要性。窗口是承载用户界面的基础单位,而`WindowManager`系统服务则负责窗口的创建、更新和移除等操作。了解这些概念有助于开发复杂且用户体验良好的应用。
72 2
|
3月前
|
Android开发 UED 开发者
Android经典实战之WindowManager和创建系统悬浮窗
本文详细介绍了Android系统服务`WindowManager`,包括其主要功能和工作原理,并提供了创建系统悬浮窗的完整步骤。通过示例代码,展示了如何添加权限、请求权限、实现悬浮窗口及最佳实践,帮助开发者轻松掌握悬浮窗开发技巧。
478 1
|
API Android开发 Windows
Android P下WindowManager与LayoutParams的详解
WindowManager是什么?WindowManager与window的关系是什么?
811 0
|
XML 存储 设计模式
Android Framework知识整理:WindowManager体系(上)
本篇是Android framework的第一讲《WindowManager体系-上》,重在讲解Window在被添加到WindowManagerService前的流程。
|
Android开发
Android | 理解Window 和 WindowManager(下)
Android | 理解Window 和 WindowManager(下)
Android | 理解Window 和 WindowManager(下)
|
存储 Android开发 索引
Android | 理解Window 和 WindowManager(上)
Android | 理解Window 和 WindowManager(上)
Android | 理解Window 和 WindowManager(上)
Java&Android获取当前日期、时间、星期几、获取指定格式的日期时间、时间戳工具类包含使用示例
Java&Android获取当前日期、时间、星期几、获取指定格式的日期时间、时间戳工具类包含使用示例
730 0
Java&Android获取当前日期、时间、星期几、获取指定格式的日期时间、时间戳工具类包含使用示例
|
数据安全/隐私保护 Android开发
Android为图片添加水印,裁剪图片,旋转图片工具类
Android为图片添加水印,裁剪图片,旋转图片工具类
179 0
Java&Android像素px、dip转换工具类
Java&Android像素px、dip转换工具类
371 0
|
Android开发
Android WIFI是否连接,网络状态监测工具类
Android WIFI是否连接,网络状态监测工具类
316 0