Android 悬浮窗
2015-12-21 09:47
387 查看
iPhone有个很好用的白色圆点,今天就来研究下Android中的悬浮框,这里主要是实现一个快捷键的功能,当然也可以在悬浮框中做想做的事!
悬浮窗的实现主要是通过WindowManager实现,当然WindowManager只是一个接口,想了解源码的同志们可以去看WindowManagerImpl,悬浮框主要是通过WindowManager中的addView,updateView,removeView实现
WindowManager.LayoutParams这个类用于提供悬浮窗所需的参数
WindowManager.LayoutParams参数说明:
type 用于确定悬浮窗的类型(window类型,window有三种类型,应用window,子window,系统window,其中悬浮窗中使用的是系统window),一般使用TYPE_PHONE,表示在所有应用程序之上,状态栏之下,当然这里还提供了很多类型,TYPE_STATUS_BAR(状态栏)TYPE_SEARCH_BAR(搜索框)TYPE_SYSTEM_ALERT(系统提示框,例如电量很低时提示)等等,有很多,根据需求去选择
flags 用于确定悬浮窗的行为,FLAG_NOT_FOCUSABLE(window不需要获得焦点,也不需要接收各种输入事件)FLAG_NOT_TOUCHABLE(不可点击)FLAG_NOT_TOUCH_MODAL(系统会通过当前window区域以外的单击事件传递给底层的window,当前window区域以内的单击事件则自己处理)FLAG_SHOW_WHEN_LOCKED(显示在锁屏的界面上)等等
gravity 用于确定悬浮窗的对齐方式
x 用于确定悬浮窗的横坐标
y 用于确定悬浮窗的纵坐标
width 值用于指定悬浮窗的宽度
height 值用于指定悬浮窗的高度
了解这些参数基本就能创建一个悬浮框了
以上就是悬浮框的过程,当然使用系统window必须要申请权限:
通过上面这个过程知道了如何通过WindowManager创建悬浮框
下面给你一个Demo,其中有两个悬浮窗,第一个悬浮窗就是普通的悬浮窗,第二个悬浮窗加了一些判断,可以直接在app中使用,这个悬浮窗在桌面和你自己的app中显示
地址:https://github.com/zimoguo/FloatWindow
悬浮窗的实现主要是通过WindowManager实现,当然WindowManager只是一个接口,想了解源码的同志们可以去看WindowManagerImpl,悬浮框主要是通过WindowManager中的addView,updateView,removeView实现
WindowManager.LayoutParams这个类用于提供悬浮窗所需的参数
WindowManager.LayoutParams参数说明:
type 用于确定悬浮窗的类型(window类型,window有三种类型,应用window,子window,系统window,其中悬浮窗中使用的是系统window),一般使用TYPE_PHONE,表示在所有应用程序之上,状态栏之下,当然这里还提供了很多类型,TYPE_STATUS_BAR(状态栏)TYPE_SEARCH_BAR(搜索框)TYPE_SYSTEM_ALERT(系统提示框,例如电量很低时提示)等等,有很多,根据需求去选择
flags 用于确定悬浮窗的行为,FLAG_NOT_FOCUSABLE(window不需要获得焦点,也不需要接收各种输入事件)FLAG_NOT_TOUCHABLE(不可点击)FLAG_NOT_TOUCH_MODAL(系统会通过当前window区域以外的单击事件传递给底层的window,当前window区域以内的单击事件则自己处理)FLAG_SHOW_WHEN_LOCKED(显示在锁屏的界面上)等等
gravity 用于确定悬浮窗的对齐方式
x 用于确定悬浮窗的横坐标
y 用于确定悬浮窗的纵坐标
width 值用于指定悬浮窗的宽度
height 值用于指定悬浮窗的高度
了解这些参数基本就能创建一个悬浮框了
private void createFloatView() { mWindowManager = (WindowManager) getApplication().getSystemService(getApplication().WINDOW_SERVICE); wmParams = new WindowManager.LayoutParams(); wmParams.type = WindowManager.LayoutParams.TYPE_PHONE; wmParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; wmParams.gravity = Gravity.LEFT | Gravity.TOP; wmParams.x = 100; wmParams.y = 100; wmParams.width = WindowManager.LayoutParams.WRAP_CONTENT; wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT; LayoutInflater inflater = LayoutInflater.from(getApplication()); mFloatLayout = (LinearLayout) inflater.inflate(R.layout.layout_float_window, null); mWindowManager.addView(mFloatLayout, wmParams); mFloatView = (ImageView) mFloatLayout.findViewById(R.id.img_float_window); mFloatView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { wmParams.x = (int) event.getRawX() - mFloatView.getMeasuredWidth() / 2; wmParams.y = (int) event.getRawY() - mFloatView.getMeasuredHeight() / 2 - 25; mWindowManager.updateViewLayout(mFloatLayout, wmParams); return false; } }); mFloatView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(FloatWindowService.this, SearchActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); getApplication().startActivity(intent); } }); }
if (mFloatLayout != null) { mWindowManager.removeView(mFloatLayout); }
以上就是悬浮框的过程,当然使用系统window必须要申请权限:
<code class="hljs xml has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial; "><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box; "><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136); ">uses-permission</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102); ">android:name</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0); ">"android.permission.SYSTEM_ALERT_WINDOW"</span> /></span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; "><li style="box-sizing: border-box; padding: 0px 5px; ">1</li></ul>
通过上面这个过程知道了如何通过WindowManager创建悬浮框
下面给你一个Demo,其中有两个悬浮窗,第一个悬浮窗就是普通的悬浮窗,第二个悬浮窗加了一些判断,可以直接在app中使用,这个悬浮窗在桌面和你自己的app中显示
地址:https://github.com/zimoguo/FloatWindow
相关文章推荐
- android-USB Host and Accessory(附加)
- android 布局中Fragment嵌套Fragment
- Android使用Sensor感应器实现线程中刷新UI创建android测力计的功能
- Android Paint和Color类
- Android轻量级便签应用项目源码
- Android天天数钱游戏源码
- Android基于Sensor感应器获取重力感应加速度的方法
- android错误:Installation error: INSTALL_FAILED_VERSION_DOWNGRADE
- Android IOS WebRTC 音视频开发总结(五五)-- 音视频通讯中的抗丢包与带宽自适应原理
- Android如何制作漂亮的自适布局的键盘
- Java服务器对外提供接口以及Android端向服务器请求数据
- RecyclerView的长按多选功能
- android studio 首字母提示 设置 大小写敏感
- android 录音笔demo!!!(Baseadapter、file、MediaRecorder、AlertDialog)
- Android 性能分析工具dumpsys的使用(自己增加一部分在后面)
- Android 性能分析工具dumpsys的使用(自己增加一部分在后面)
- Android Touch事件传递机制全面解析(从WMS到View树)
- Android RecyclerView使用详解四
- android 滑动返回效果
- 让AndroidStudio运行加快