windowManager参数

这段摘要包含了博客的关键信息,忽略信息技术无关的内容。
[url]http://www.cnblogs.com/olvo/archive/2012/05/17/2505776.html[/url]
package com.kotei.service.devicemanager.widget; import android.content.Context; import android.graphics.PixelFormat; import android.os.Build; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.WindowManager; import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.TextView; import androidx.annotation.DrawableRes; import com.kotei.service.devicemanager.R; import com.kotei.service.devicemanager.util.LogUtil; /** * 适用于后台服务的图标+文字悬浮弹窗 * 使用 WindowManager 添加到系统窗口 */ public class IconTextPopup { private static final String TAG = "IconTextPopup"; private Context mContext; private WindowManager windowManager; private View popupView; private static IconTextPopup instance; private ImageView iconView; private TextView textView; private WindowManager.LayoutParams params; private boolean isShowing = false; private FrameLayout fullScreenContainer; // 黑色蒙层 private IconTextPopup(Context context) { this.mContext = context; this.windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); popupView = LayoutInflater.from(mContext).inflate(R.layout.popup_icon_text, null); iconView = popupView.findViewById(R.id.popup_icon); textView = popupView.findViewById(R.id.popup_text); } public static IconTextPopup getInstance(Context context) { if (instance == null) { instance = new IconTextPopup(context); } return instance; } /** * 显示弹窗 */ public void show() { LogUtil.d(TAG, "show in , isShowing = " + isShowing); if (isShowing || popupView == null) return; // 1. 创建黑色蒙层(只创建一次即可) if (fullScreenContainer == null) { fullScreenContainer = new FrameLayout(popupView.getContext()); fullScreenContainer.setBackgroundColor(0xCC000000); // 80% 黑色 fullScreenContainer.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); } // 2. 如果 popupView 的 parent 还存在,先移除(保险) if (popupView.getParent() != null) { ((ViewGroup) popupView.getParent()).removeView(popupView); } // 3. 把 popupView 加到蒙层中央 if (popupView.getLayoutParams() instanceof FrameLayout.LayoutParams) { // 复用已有的 LayoutParams popupView.setLayoutParams(popupView.getLayoutParams()); } else { FrameLayout.LayoutParams pp = new FrameLayout.LayoutParams( 800, 324); // 保持原来的宽高 pp.gravity = Gravity.CENTER; popupView.setLayoutParams(pp); } fullScreenContainer.removeAllViews(); // 防止重复加 fullScreenContainer.addView(popupView); // 4. 构造 WindowManager 参数(只创建一次) if (params == null) { params = new WindowManager.LayoutParams( WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, PixelFormat.TRANSLUCENT); params.gravity = Gravity.START | Gravity.TOP; } // 5. 把「蒙层」贴到 WindowManager try { windowManager.addView(fullScreenContainer, params); isShowing = true; } catch (Exception e) { e.printStackTrace(); } } /** * 隐藏/移除弹窗 */ public void dismiss() { LogUtil.d(TAG, "dismiss in , isShowing = " + isShowing); if (isShowing && popupView != null && windowManager != null) { try { windowManager.removeView(fullScreenContainer); // 移除蒙层(连带 popupView) } catch (Exception e) { e.printStackTrace(); } finally { isShowing = false; // 可选:把 popupView 从蒙层里摘出来,方便复用 fullScreenContainer.removeAllViews(); } } } /** * 设置显示的文本 */ public void setText(String text) { if (textView != null) { textView.setText(text); } } /** * 设置图标 (资源ID) */ public void setIcon(@DrawableRes int drawableResId) { if (iconView != null) { iconView.setImageResource(drawableResId); } } /** * 是否正在显示 */ public boolean isShowing() { return isShowing; } /** * (可选)设置弹窗位置 */ public void setGravity(int gravity) { if (params != null) { params.gravity = gravity; if (isShowing) { windowManager.updateViewLayout(popupView, params); } } } } public void openIconTextPopup(Context context,String text, int iconId) { IconTextPopup iconTextPopup = IconTextPopup.getInstance(context); LogUtil.i(TAG,"hashcode: " + iconTextPopup.hashCode()); iconTextPopup.setText(text); iconTextPopup.setIcon(iconId); iconTextPopup.show(); } public void closeIconTextPopup(Context context) { IconTextPopup iconTextPopup = IconTextPopup.getInstance(context); if (iconTextPopup.isShowing()) { iconTextPopup.dismiss(); } } 现在出现了一个问题,iconTextPopup hashcode值一直保持不变 openIconTextPopup 是打卡弹窗,closeIconTextPopup是关闭弹窗,现在存在一个问题:这个弹窗不会随着主题模式的改变而改变(已经进行白天黑夜配置),这个问题与hashcode值一直不变有没有关系,以及这个问题应该怎么处理 这样修改可以吗, public void dismiss() { LogUtil.d(TAG, "dismiss in , isShowing = " + isShowing); if (isShowing && popupView != null && windowManager != null) { try { windowManager.removeView(fullScreenContainer); // 移除蒙层(连带 popupView) } catch (Exception e) { e.printStackTrace(); } finally { isShowing = false; // 可选:把 popupView 从蒙层里摘出来,方便复用 fullScreenContainer.removeAllViews(); popupView = null; } } } 在这里将 popupView = null;置为null 这样修改会导致一个问题,该弹窗后续都不显示了
10-12
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值