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浮动窗口

标签:Android,悬浮窗控件
0
投稿

猜你喜欢

  • java.math包下计算浮点数和整数的类的实例

    2023-02-06 16:19:39
  • WPF使用DrawingContext实现绘制刻度条

    2023-06-19 08:35:17
  • 使用java代码获取新浪微博应用的access token代码实例

    2023-12-01 20:18:30
  • java接口自动化测试框架及断言详解

    2022-01-23 20:01:36
  • Android自定义View实现微信语音界面

    2022-03-27 20:35:27
  • 详解Java注解的实现与使用方法

    2023-10-31 12:33:20
  • anroid开发教程之spinner下拉列表的使用示例

    2023-10-05 05:42:22
  • 分析Android内存泄漏的几种可能

    2022-06-01 21:55:48
  • java处理图片背景颜色的方法

    2023-11-27 04:38:20
  • Android判断当前App是在前台还是在后台

    2022-09-20 10:37:49
  • 关于Maven混合配置私有仓库和公共仓库的问题

    2021-10-15 20:30:51
  • C# 嵌入dll 的方法

    2022-11-06 08:39:10
  • SpringBoot如何实现分离资源文件并打包

    2023-02-18 12:01:49
  • C#实现学生成绩管理系统

    2021-06-05 22:51:31
  • C++深入探究引用的使用

    2023-04-28 18:02:03
  • 在idea中显示springboot面板的方法

    2022-01-02 22:00:57
  • 基于Lucene的Java搜索服务器Elasticsearch安装使用教程

    2022-05-31 05:05:10
  • C#导出数据到CSV文件的通用类实例

    2023-05-22 09:23:16
  • springboot注解Aspect实现方案

    2022-12-17 19:32:06
  • Java实现超大Excel文件解析(XSSF,SXSSF,easyExcel)

    2023-11-27 11:02:17
  • asp之家 软件编程 m.aspxhome.com