Android自定义覆盖层控件 悬浮窗控件
作者:lan_hz007 发布时间:2021-10-21 01:14:40
在我们移动应用开发过程中,偶尔有可能会接到这种需求:
1、在手机桌面创建一个窗口,类似于360的悬浮窗口,点击这个窗口可以响应(至于窗口拖动我们可以后面再扩展)。
2、自己开发的应用去启动一个非本应用B,在B应用的某个界面增加一个引导窗口。
3、在应用的页面上触发启动这个窗口,该窗口悬浮在这个页面上,但又不会影响界面的其他操作。即不像PopupWindow那样要么窗口消失要么页面不可响应
以上需求都有几个共同特点,1、窗口的承载页面不一定不是本应用页面(Activity),即不是类似dialog, PopupWindow之类的页面。2、窗口的显示不会影响用户对其他界面的操作。
根据以上特点,我们发现这类的窗口其不影响其他界面操作特点有点像Toast,但又不完全是,因为Toast是自己消失的。其界面可以恒显示又有点像popupwindow,只当调用了消失方法才会消失。所以我们在做这样的控件的时候可以去参考一下Toast和PopupWIndow如何实现。最主要的时候Toast。好了说了这么多大概的思路我们已经明白了。
透过Toast,PopupWindow源码我们发现,Toast,Popup的实现都是通过windowManager的addview和removeView以及通过设置LayoutParams实现的。因此后面设计就该从这里入手,废话不说了----去实现。
第一步设计类似Toast的类FloatWindow
package com.floatwindowtest.john.floatwindowtest.wiget;
import android.app.Activity;
import android.content.Context;
import android.graphics.PixelFormat;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
import static android.view.WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;
/**
* Created by john on 2017/3/10.
*/
class FloatWindow {
private final Context mContext;
private WindowManager windowManager;
private View floatView;
private WindowManager.LayoutParams params;
public FloatWindow(Context mContext) {
this.mContext = mContext;
this.params = new WindowManager.LayoutParams();
}
/**
* 显示浮动窗口
* @param view
* @param x view距离左上角的x距离
* @param y view距离左上角的y距离
*/
void show(View view, int x, int y) {
this.windowManager = (WindowManager) this.mContext.getSystemService(Context.WINDOW_SERVICE);
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
params.gravity = Gravity.TOP | Gravity.LEFT;
params.format = PixelFormat.TRANSLUCENT;
params.x = x;
params.y = y;
params.type = WindowManager.LayoutParams.TYPE_TOAST;
params.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | FLAG_NOT_FOCUSABLE | FLAG_WATCH_OUTSIDE_TOUCH
| WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
floatView = view;
windowManager.addView(floatView, params);
}
/**
* 显示浮动窗口
* @param view
* @param x
* @param y
* @param listener 窗体之外的监听
* @param backListener 返回键盘监听
*/
void show(View view, int x, int y, OutsideTouchListener listener, KeyBackListener backListener) {
this.windowManager = (WindowManager) this.mContext.getSystemService(Context.WINDOW_SERVICE);
final FloatWindowContainerView containerView = new FloatWindowContainerView(this.mContext, listener, backListener);
containerView.addView(view, WRAP_CONTENT, WRAP_CONTENT);
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
params.gravity = Gravity.TOP | Gravity.LEFT;
params.format = PixelFormat.TRANSLUCENT;
params.x = x;
params.y = y;
params.type = WindowManager.LayoutParams.TYPE_TOAST;
//
// params.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
// | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
// | WindowManager.LayoutParams. FLAG_NOT_FOCUSABLE ;
params.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
| WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
floatView = containerView;
windowManager.addView(floatView, params);
}
/**
* 更新view对象文职
*
* @param offset_X x偏移量
* @param offset_Y Y偏移量
*/
public void updateWindowLayout(float offset_X, float offset_Y) {
params.x += offset_X;
params.y += offset_Y;
windowManager.updateViewLayout(floatView, params);
}
/**
* 关闭界面
*/
void dismiss() {
if (this.windowManager == null) {
this.windowManager = (WindowManager) this.mContext.getSystemService(Context.WINDOW_SERVICE);
}
if (floatView != null) {
windowManager.removeView(floatView);
}
floatView = null;
}
public void justHideWindow() {
this.floatView.setVisibility(View.GONE);
}
private class FloatWindowContainerView extends FrameLayout {
private OutsideTouchListener listener;
private KeyBackListener backListener;
public FloatWindowContainerView(Context context, OutsideTouchListener listener, KeyBackListener backListener) {
super(context);
this.listener = listener;
this.backListener = backListener;
}
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
if (getKeyDispatcherState() == null) {
if (backListener != null) {
backListener.onKeyBackPressed();
}
return super.dispatchKeyEvent(event);
}
if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) {
KeyEvent.DispatcherState state = getKeyDispatcherState();
if (state != null) {
state.startTracking(event, this);
}
return true;
} else if (event.getAction() == KeyEvent.ACTION_UP) {
KeyEvent.DispatcherState state = getKeyDispatcherState();
if (state != null && state.isTracking(event) && !event.isCanceled()) {
System.out.println("dsfdfdsfds");
if (backListener != null) {
backListener.onKeyBackPressed();
}
return super.dispatchKeyEvent(event);
}
}
return super.dispatchKeyEvent(event);
} else {
return super.dispatchKeyEvent(event);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
final int x = (int) event.getX();
final int y = (int) event.getY();
if ((event.getAction() == MotionEvent.ACTION_DOWN)
&& ((x < 0) || (x >= getWidth()) || (y < 0) || (y >= getHeight()))) {
return true;
} else if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
if (listener != null) {
listener.onOutsideTouch();
}
System.out.println("dfdf");
return true;
} else {
return super.onTouchEvent(event);
}
}
}
}
大家可能会注意到
// params.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
// | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
// | WindowManager.LayoutParams. FLAG_NOT_FOCUSABLE ;
params.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
| WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
这些设置有所不同,这就是我们要实现既能够监听窗口之外的触目事件,又不会影响他们自己的操作的关键地方 ,同时| WindowManager.LayoutParams. FLAG_NOT_FOCUSABLE ;和| WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL; 窗体是否监听到返回键的关键设置 需要指出的是一旦窗体监听到返回键事件,则当前Activity不会再监听到返回按钮事件了,所以大家可根据自己的实际情况出发做出选择。
为了方便管理这些浮动窗口的显示和消失,还写了一个管理窗口显示的类FloatWindowManager。这是一个单例模式 对应的显示窗口也是只显示一个。大家可以根据自己的需求是改变 这里不再明细。
package com.floatwindowtest.john.floatwindowtest.wiget;
import android.content.Context;
import android.view.View;
/**
*
* Created by john on 2017/3/10.
*/
public class FloatWindowManager {
private static FloatWindowManager manager;
private FloatWindow floatWindow;
private FloatWindowManager(){
}
public static synchronized FloatWindowManager getInstance(){
if(manager==null){
manager=new FloatWindowManager();
}
return manager;
}
public void showFloatWindow(Context context, View view,int x,int y){
if(floatWindow!=null){
floatWindow.dismiss();
}
floatWindow=new FloatWindow(context);
floatWindow.show(view,x,y);
}
public void showFloatWindow(Context context, View view, int x, int y, OutsideTouchListener listener,KeyBackListener backListener){
if(floatWindow!=null){
floatWindow.dismiss();
}
floatWindow=new FloatWindow(context);
floatWindow.show(view,0,0,listener,backListener);
}
public void dismissFloatWindow(){
if(floatWindow!=null){
floatWindow.dismiss();
}
}
public void justHideWindow(){
floatWindow.justHideWindow();
}
/**
* 更新位置
* @param offsetX
* @param offsetY
*/
public void updateWindowLayout(float offsetX, float offsetY){
floatWindow.updateWindowLayout(offsetX,offsetY);
};
}
还有设计类似悬浮球的窗口等 大家可以自己运行一遍比这里看千遍更有用。
附件:Android浮动窗口


猜你喜欢
- 前言真的一秒就可以实现么?是的,因为我们直接复制粘贴工具类拿来用就可以。 工具类 WaterMarkUtil.java&
- MyBatis 是一款优秀的持久层框架,被各大互联网公司使用,本文使用Spring Boot整合Mybatis,并完成CRUD操作。为什么要
- @schedule 注解 是springboot 常用的定时任务注解,使用起来简单方便,但是如果定时任务非常多,或者有的任务很耗时
- 标识符和关键字标识符读音 biao zhi fu什么是标识符包、类、变量、方法…等等,只要是起名的地方,那个名字就是标
- Java集合ArrayDeque类实例分析前言ArrayDeque类是双端队列的实现类,类的继承结构如下面,继承自AbastractColl
- 前言最近在逛博客的时候看到了有关Redis方面的面试题,其中提到了Redis在内存达到最大限制的时候会使用LRU等淘汰机制,然后找了这方面的
- 一、背景有时我们在做开发的时候需要记录每个任务执行时间,或者记录一段代码执行时间,最简单的方法就是打印当前时间与执行完时间的差值,一般我们检
- 前言需求是上传Excel文件并读取Excel文件中的内容,根据获取的数据执行完某些业务操作后再将一些数据写回到excel中。前台使用Form
- 详解Java对象的强、软、弱和虚引用+ReferenceQueue一、强引用(StrongReference)强引用是使用最普遍的引用。如果
- 本文实例讲述了C#接口在派生类和外部类中的调用方法。分享给大家供大家参考,具体如下:C#的接口通过interface关键字进行创建,在接口中
- 本文实例讲述了DevExpress设置饼状图的Lable位置的方法。分享给大家供大家参考。具体实现方法如下:关键代码如下:/// <s
- 用过spring框架进行开发的人,多多少少会使用过它的AOP功能,都知道有@Before、@Around和@After等advice。最近,
- 引言 在一些项目中或是一些特殊的业务场景中,需要用到显示系统的当前时间,以及一些
- 1、CS、BS架构定义CS(Client/Server):客户端----服务器结构。C/S结构在技术上很成熟,它的主要特点是交互性强、具有安
- 这篇文章主要介绍了springboot跨域CORS处理代码解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,
- C# 输出参数out什么是输出参数方法声明时,使用out修饰符声明的形参,成为输出参数。输出参数的特点1、输出参数不创建新的储存位置。2、输
- 在SpringAMQP的发送方法中,接收消息的类型是Object,也就是说我们可以发送任意对象类型的消息,SpringAMQP会帮我们序列化
- Optional在JAVA中被定义为一个容器类,更确切的说只存一个元素的容器。container object which may or m
- 实例如下所示:public class JsonExtracter { public static void main(String[] a
- 1.easy-captcha工具包生成验证码的方式有许多种,这里选择的是easy-captcha工具包。github开原地址为:easy-c