广告
返回顶部
首页 > 资讯 > 精选 >怎么中Android中自定义一个悬浮窗控件
  • 792
分享到

怎么中Android中自定义一个悬浮窗控件

androidroi%d 2023-05-31 09:05:20 792人浏览 薄情痞子
摘要

今天就跟大家聊聊有关怎么中Android中自定义一个悬浮窗控件,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。第一步设计类似Toast的类FloatWindowpackage 

今天就跟大家聊聊有关怎么中Android中自定义一个悬浮窗控件,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

第一步设计类似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;   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();  }      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);  }      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);  }     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;    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();  }    public void updateWindowLayout(float offsetX, float offsetY){   floatWindow.updateWindowLayout(offsetX,offsetY);  }; }

看完上述内容,你们对怎么中Android中自定义一个悬浮窗控件有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注编程网精选频道,感谢大家的支持。

--结束END--

本文标题: 怎么中Android中自定义一个悬浮窗控件

本文链接: https://www.lsjlt.com/news/224675.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 怎么中Android中自定义一个悬浮窗控件
    今天就跟大家聊聊有关怎么中Android中自定义一个悬浮窗控件,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。第一步设计类似Toast的类FloatWindowpackage ...
    99+
    2023-05-31
    android roi %d
  • Android自定义覆盖层控件 悬浮窗控件
    在我们移动应用开发过程中,偶尔有可能会接到这种需求: 1、在手机桌面创建一个窗口,类似于360的悬浮窗口,点击这个窗口可以响应(至于窗口拖动我们可以后面再扩展)。 2、自己...
    99+
    2022-06-06
    Android
  • 怎么在Android中自定义一个控件
    怎么在Android中自定义一个控件?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。代码class SleepDayChart(context: Contex...
    99+
    2023-06-14
  • 怎么在Android中自定义一个ProgressBar控件
    这篇文章将为大家详细讲解有关怎么在Android中自定义一个ProgressBar控件,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。首先加载Drawable,在onMeasure设置好其区域...
    99+
    2023-05-30
    android progressbar
  • 怎么在Android中实现一个自定义控件
    今天就跟大家聊聊有关怎么在Android中实现一个自定义控件,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。首先定义一个layout实现按钮内部布局:<xml vers...
    99+
    2023-05-31
    android
  • android6.0版本中怎么实现一个悬浮窗口
    这篇文章将为大家详细讲解有关android6.0版本中怎么实现一个悬浮窗口,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。代码如下:public class MainAct...
    99+
    2023-05-31
    android roi 悬浮窗口
  • Android项目中项目实现一个控件悬浮效果
    今天就跟大家聊聊有关Android项目中项目实现一个控件悬浮效果,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。效果图:新建一个Android项目,取名MeiTuanDemo,先看立即...
    99+
    2023-05-31
    android roi 目中
  • 怎么在Android中实现一个悬浮按钮
    怎么在Android中实现一个悬浮按钮?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。具体实现代码:import android.content.Context;i...
    99+
    2023-05-31
    android roi %d
  • 如何在Android应用中自定义一个控件
    本篇文章为大家展示了如何在Android应用中自定义一个控件,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。开发自定义控件的步骤:了解View的工作原理 2、 编写继承自View的子类3、 为自定义V...
    99+
    2023-05-31
    android roi
  • Android中怎么自定义Progress控件
    Android中怎么自定义Progress控件,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。主要就是需求就是椭圆进度,百分比跟随渐变背景,这样一想其实就是一个布局,然后控制...
    99+
    2023-05-31
    android progress
  • 怎么在Android中利用marker自定义一个弹框窗口
    怎么在Android中利用marker自定义一个弹框窗口?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。Android是什么Android是一种基于Linux内核的自由及开放源代...
    99+
    2023-06-14
  • Android中怎么自定义选择控件
    本篇文章为大家展示了Android中怎么自定义选择控件,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。一、自定义DialogDialog布局文件<xml version="1...
    99+
    2023-05-30
    android
  • 如何在Android项目中创建一个自定义控件
    本篇文章为大家展示了如何在Android项目中创建一个自定义控件,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。仿iPhone 的风格,在界面的顶部放置一个标题栏。<&#63;xml v...
    99+
    2023-05-31
    android roi 目中
  • Android怎么在XML文件中自定义控件
    今天小编给大家分享一下Android怎么在XML文件中自定义控件的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。一、为什么需要...
    99+
    2023-07-05
  • Android中怎么自定义一个数字键盘
    这篇文章给大家介绍Android中怎么自定义一个数字键盘,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。1. 实现键盘的 xml 布局网格样式的布局用 GridView 或者 RecyclerView 都可以实现,其实用...
    99+
    2023-05-30
    android
  • 怎么在android中自定义一个PagerAdapter方法
    这篇文章给大家介绍怎么在android中自定义一个PagerAdapter方法,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。首先,如果继承pageradapter,至少必须重写下面的四个方法  &n...
    99+
    2023-05-30
    android pageradapter
  • 如何在Android中使用PopupWindow制作一个自定义弹窗
    本篇文章给大家分享的是有关如何在Android中使用PopupWindow制作一个自定义弹窗,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。代码:PopupWindow ...
    99+
    2023-05-31
    android popupwindow
  • 怎么在Android中利用FloatingActionButton实现一个悬浮按钮效果
    今天就跟大家聊聊有关怎么在Android中利用FloatingActionButton实现一个悬浮按钮效果,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。首先是这个最小的Tag:这个T...
    99+
    2023-05-31
    android floatingbutton roi
  • 怎么在android应用中实现一个RecyclerView悬浮吸顶效果
    本篇文章为大家展示了怎么在android应用中实现一个RecyclerView悬浮吸顶效果,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。MultiType-Adapter打造悬浮吸顶效果注:当前版本...
    99+
    2023-05-31
    android recyclerview recycle
  • Android中怎么自定义一个环形LoadingView效果
    这期内容当中小编将会给大家带来有关Android中怎么自定义一个环形LoadingView效果,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。控件实现:这个控件继承Rela...
    99+
    2022-10-19
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作