Android WindowManager工具类

发布于:2024-04-02 ⋅ 阅读:(60) ⋅ 点赞:(0)

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;
    }
}

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

推荐几个:

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

本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到