本文主要是介绍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工具类的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!