├── .gitignore ├── .idea ├── gradle.xml ├── misc.xml ├── modules.xml ├── runConfigurations.xml └── vcs.xml ├── LICENSE.txt ├── README.md ├── back.gif ├── build.gradle ├── floatwindow ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── example │ │ └── fixedfloatwindow │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── yhao │ │ │ └── floatwindow │ │ │ ├── FloatActivity.java │ │ │ ├── FloatLifecycle.java │ │ │ ├── FloatPhone.java │ │ │ ├── FloatToast.java │ │ │ ├── FloatView.java │ │ │ ├── FloatWindow.java │ │ │ ├── IFloatWindow.java │ │ │ ├── IFloatWindowImpl.java │ │ │ ├── LifecycleListener.java │ │ │ ├── LogUtil.java │ │ │ ├── Miui.java │ │ │ ├── MoveType.java │ │ │ ├── PermissionListener.java │ │ │ ├── PermissionUtil.java │ │ │ ├── ResumedListener.java │ │ │ ├── Rom.java │ │ │ ├── Screen.java │ │ │ ├── Util.java │ │ │ ├── ViewStateListener.java │ │ │ └── ViewStateListenerAdapter.java │ └── res │ │ └── values │ │ ├── strings.xml │ │ └── style.xml │ └── test │ └── java │ └── com │ └── example │ └── fixedfloatwindow │ └── ExampleUnitTest.java ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── img.gif ├── pay.jpg ├── sample ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── example │ │ └── yhao │ │ └── fixedfloatwindow │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── yhao │ │ │ └── floatwindow │ │ │ ├── A_Activity.java │ │ │ ├── B_Activity.java │ │ │ ├── BaseActivity.java │ │ │ ├── BaseApplication.java │ │ │ └── C_Activity.java │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ ├── ic_launcher_background.xml │ │ └── icon.png │ │ ├── layout │ │ ├── activity_a.xml │ │ ├── activity_b.xml │ │ ├── activity_c.xml │ │ └── activity_d.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── example │ └── yhao │ └── fixedfloatwindow │ └── ExampleUnitTest.java ├── settings.gradle └── slide.gif /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | .externalNativeBuild 10 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 18 | 19 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 27 | 28 | 29 | 30 | 31 | 32 | 34 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright 2017 Wang YingHao 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # FloatWindow 安卓任意界面悬浮窗 2 | [![](https://jitpack.io/v/yhaolpz/FloatWindow.svg)](https://jitpack.io/#yhaolpz/FloatWindow) 3 | 4 | 5 | ![悬浮按钮图](https://raw.githubusercontent.com/yhaolpz/FixedFloatWindow/master/slide.gif) 6 | 7 | 特性: 8 | === 9 | 10 | 1.支持拖动,提供自动贴边等动画 11 | 12 | 2.内部自动进行权限申请操作 13 | 14 | 3.可自由指定要显示悬浮窗的界面 15 | 16 | 4.应用退到后台时,悬浮窗会自动隐藏 17 | 18 | 5.除小米外,4.4~7.0 无需权限申请 19 | 20 | 6.位置及宽高可设置百分比值,轻松适配各分辨率 21 | 22 | 7.支持权限申请结果、位置等状态监听 23 | 24 | 8.链式调用,简洁清爽 25 | 26 | 27 | 集成: 28 | === 29 | 30 | 第 1 步、在工程的 build.gradle 中添加: 31 | 32 | ``` 33 | allprojects { 34 | repositories { 35 | ... 36 | maven { url 'https://jitpack.io' } 37 | } 38 | } 39 | ``` 40 | 第 2 步、在应用的 build.gradle 中添加: 41 | 42 | ``` 43 | dependencies { 44 | compile 'com.github.yhaolpz:FloatWindow:1.0.9' 45 | } 46 | ``` 47 | 48 | 使用: 49 | === 50 | 51 | **0.声明权限** 52 | 53 | ```java 54 | 55 | 56 | 57 | ``` 58 | 59 | 60 | **1.基础使用** 61 | 62 | ```java 63 | FloatWindow 64 | .with(getApplicationContext()) 65 | .setView(view) 66 | .setWidth(100) //设置控件宽高 67 | .setHeight(Screen.width,0.2f) 68 | .setX(100) //设置控件初始位置 69 | .setY(Screen.height,0.3f) 70 | .setDesktopShow(true) //桌面显示 71 | .setViewStateListener(mViewStateListener) //监听悬浮控件状态改变 72 | .setPermissionListener(mPermissionListener) //监听权限申请结果 73 | .build(); 74 | 75 | ``` 76 | 77 | 宽高及位置可设像素值或屏幕宽/高百分比,默认宽高为 wrap_content;默认位置为屏幕左上角,x、y 为偏移量。 78 | 79 | 80 | **2.指定界面显示** 81 | 82 | ```java 83 | .setFilter(true, A_Activity.class, C_Activity.class) 84 | 85 | ``` 86 | 此方法表示 A_Activity、C_Activity 显示悬浮窗,其他界面隐藏。 87 | 88 | ```java 89 | .setFilter(false, B_Activity.class) 90 | ``` 91 | 此方法表示 B_Activity 隐藏悬浮窗,其他界面显示。 92 | 93 | 注意:setFilter 方法参数可以识别该 Activity 的子类 94 | 95 | 也就是说,如果 A_Activity、C_Activity 继承自 BaseActivity,你可以这样设置: 96 | 97 | ```java 98 | .setFilter(true, BaseActivity.class) 99 | ``` 100 | 101 | 102 | **3.可拖动悬浮窗及回弹动画** 103 | 104 | ```java 105 | .setMoveType(MoveType.slide) 106 | .setMoveStyle(500, new AccelerateInterpolator()) //贴边动画时长为500ms,加速插值器 107 | 108 | ``` 109 | 110 | 共提供 4 种 MoveType : 111 | 112 | MoveType.slide : 可拖动,释放后自动贴边 (默认) 113 | 114 | MoveType.back : 可拖动,释放后自动回到原位置 115 | 116 | MoveType.active : 可拖动 117 | 118 | MoveType.inactive : 不可拖动 119 | 120 | 121 | setMoveStyle 方法可设置动画效果,只在 MoveType.slide 或 MoveType.back 模式下设置此项才有意义。默认减速插值器,默认动画时长为 300ms。 122 | 123 | 124 | **4.后续操作** 125 | 126 | ```java 127 | //手动控制 128 | FloatWindow.get().show(); 129 | FloatWindow.get().hide(); 130 | 131 | //修改显示位置 132 | FloatWindow.get().updateX(100); 133 | FloatWindow.get().updateY(100); 134 | 135 | //销毁 136 | FloatWindow.destroy(); 137 | 138 | ``` 139 | 140 | 以上操作应待悬浮窗初始化后进行。 141 | 142 | 143 | **5.多个悬浮窗** 144 | 145 | ```java 146 | 147 | FloatWindow 148 | .with(getApplicationContext()) 149 | .setView(imageView) 150 | .build(); 151 | 152 | FloatWindow 153 | .with(getApplicationContext()) 154 | .setView(button) 155 | .setTag("new") 156 | .build(); 157 | 158 | 159 | FloatWindow.get("new").show(); 160 | FloatWindow.get("new").hide(); 161 | FloatWindow.destroy("new"); 162 | 163 | ``` 164 | 165 | 创建第一个悬浮窗不需加 tag,之后再创建就需指定唯一 tag ,以此区分,方便进行后续操作。 166 | 167 | 168 | 举个栗子 169 | === 170 | 171 | 点击查看 : [示例代码](https://github.com/yhaolpz/FloatWindow/blob/master/sample/src/main/java/com/example/yhao/floatwindow/BaseApplication.java) 。 172 | 173 | 最后: 174 | -- 175 | ![悬浮按钮图](https://raw.githubusercontent.com/yhaolpz/FixedFloatWindow/master/pay.jpg) 176 | 177 | 本人已尽量去兼容更多机型,但经济有限,如果你想帮助此库,提 Issues 标出当前版本不适配的机型即可,感谢~ 178 | 179 | 180 | **更新日志** 181 | -- 182 | **v1.0.10** 183 | 184 | 分离生命周期和创建浮动框,需要在Application中注册生命周期。 185 | 这样就可以在用到浮动框的地方才去创建浮动框。 186 | 解决必须在Application中创建浮动框,否则会出问题的bug。 187 | 188 | 在Applicaion中先注册生命周期:FloatWindow.initLifecycle(this); 189 | 190 | **v1.0.9** 191 | 192 | 修复拖动点击事件冲突 193 | 194 | 添加权限结果监听、位置等状态监听 195 | 196 | 支持贴边边距设置 197 | 198 | 199 | **v1.0.8** 200 | 201 | 适配 4.4~8.0 及各大国产机型 202 | 203 | 支持桌面显示 204 | 205 | 206 | **v1.0.7** 207 | 208 | 适配 Android 8.0 209 | 210 | 211 | **v1.0.6** 212 | 213 | 支持悬浮窗拖动及相关动效 214 | 215 | 位置及宽高可设置百分比值 216 | 217 | 更改相关类名及使用方法 218 | 219 | 220 | **v1.0.5** 221 | 222 | 修复未调用show显示悬浮窗bug 223 | 224 | 225 | **v1.0.4** 226 | 227 | 返回桌面将会自动隐藏控件,无需再监听应用退到后台等操作 228 | 229 | 新增 Activity 过滤器,可自由指定哪些界面显示,哪些界面不显示 230 | 231 | FixedFloatWindow 类改为 FFWindow 232 | 233 | 234 | **v1.0.3** 235 | 236 | 修复已知 bug 237 | 238 | 新增 dismiss 方法 239 | 240 | 新增其他方案,如:所有版本都申请权限 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | 254 | 255 | 256 | 257 | 258 | 259 | -------------------------------------------------------------------------------- /back.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/baneyue/FloatWindow/86c249edaa5f537e8a03b3a9e427090e072d0d2d/back.gif -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | 5 | repositories { 6 | jcenter() 7 | } 8 | dependencies { 9 | classpath 'com.android.tools.build:gradle:2.3.3' 10 | 11 | 12 | // NOTE: Do not place your application dependencies here; they belong 13 | // in the individual module build.gradle files 14 | 15 | classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5' 16 | } 17 | } 18 | 19 | allprojects { 20 | repositories { 21 | jcenter() 22 | maven { url "https://jitpack.io" } 23 | } 24 | } 25 | 26 | task clean(type: Delete) { 27 | delete rootProject.buildDir 28 | } 29 | -------------------------------------------------------------------------------- /floatwindow/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /floatwindow/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | android { 4 | compileSdkVersion 26 5 | buildToolsVersion "26.0.2" 6 | 7 | 8 | defaultConfig { 9 | minSdkVersion 19 10 | targetSdkVersion 26 11 | versionCode 1 12 | versionName "1.0" 13 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 14 | 15 | } 16 | 17 | buildTypes { 18 | release { 19 | minifyEnabled false 20 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 21 | } 22 | } 23 | 24 | } 25 | 26 | dependencies { 27 | compile fileTree(dir: 'libs', include: ['*.jar']) 28 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 29 | exclude group: 'com.android.support', module: 'support-annotations' 30 | }) 31 | compile 'com.android.support:appcompat-v7:26.+' 32 | testCompile 'junit:junit:4.12' 33 | } 34 | 35 | // JitPack Maven 36 | apply plugin: 'com.github.dcendents.android-maven' 37 | // Your Group 38 | group='com.github.yhaolpz' 39 | -------------------------------------------------------------------------------- /floatwindow/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /floatwindow/src/androidTest/java/com/example/fixedfloatwindow/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.example.fixedfloatwindow; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumented test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.example.fixedfloatwindow.test", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /floatwindow/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 10 | 11 | -------------------------------------------------------------------------------- /floatwindow/src/main/java/com/yhao/floatwindow/FloatActivity.java: -------------------------------------------------------------------------------- 1 | package com.yhao.floatwindow; 2 | 3 | import android.app.Activity; 4 | import android.content.Context; 5 | import android.content.Intent; 6 | import android.net.Uri; 7 | import android.os.Build; 8 | import android.os.Bundle; 9 | import android.provider.Settings; 10 | import android.support.annotation.RequiresApi; 11 | 12 | import java.util.ArrayList; 13 | import java.util.List; 14 | 15 | /** 16 | * 用于在内部自动申请权限 17 | * https://github.com/yhaolpz 18 | */ 19 | 20 | public class FloatActivity extends Activity { 21 | 22 | private static List mPermissionListenerList; 23 | private static PermissionListener mPermissionListener; 24 | 25 | 26 | @Override 27 | protected void onCreate(Bundle savedInstanceState) { 28 | super.onCreate(savedInstanceState); 29 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 30 | requestAlertWindowPermission(); 31 | } 32 | } 33 | 34 | @RequiresApi(api = Build.VERSION_CODES.M) 35 | private void requestAlertWindowPermission() { 36 | Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION); 37 | intent.setData(Uri.parse("package:" + getPackageName())); 38 | startActivityForResult(intent, 756232212); 39 | } 40 | 41 | 42 | @Override 43 | protected void onActivityResult(int requestCode, int resultCode, Intent data) { 44 | super.onActivityResult(requestCode, resultCode, data); 45 | if (requestCode == 756232212) { 46 | if (PermissionUtil.hasPermissionOnActivityResult(this)) { 47 | mPermissionListener.onSuccess(); 48 | } else { 49 | mPermissionListener.onFail(); 50 | } 51 | } 52 | finish(); 53 | } 54 | 55 | static synchronized void request(Context context, PermissionListener permissionListener) { 56 | if (PermissionUtil.hasPermission(context)) { 57 | permissionListener.onSuccess(); 58 | return; 59 | } 60 | if (mPermissionListenerList == null) { 61 | mPermissionListenerList = new ArrayList<>(); 62 | mPermissionListener = new PermissionListener() { 63 | @Override 64 | public void onSuccess() { 65 | for (PermissionListener listener : mPermissionListenerList) { 66 | listener.onSuccess(); 67 | } 68 | mPermissionListenerList.clear(); 69 | } 70 | 71 | @Override 72 | public void onFail() { 73 | for (PermissionListener listener : mPermissionListenerList) { 74 | listener.onFail(); 75 | } 76 | mPermissionListenerList.clear(); 77 | } 78 | }; 79 | Intent intent = new Intent(context, FloatActivity.class); 80 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 81 | context.startActivity(intent); 82 | } 83 | mPermissionListenerList.add(permissionListener); 84 | } 85 | 86 | 87 | } 88 | -------------------------------------------------------------------------------- /floatwindow/src/main/java/com/yhao/floatwindow/FloatLifecycle.java: -------------------------------------------------------------------------------- 1 | package com.yhao.floatwindow; 2 | 3 | import android.app.Activity; 4 | import android.app.Application; 5 | import android.content.BroadcastReceiver; 6 | import android.content.Context; 7 | import android.content.Intent; 8 | import android.content.IntentFilter; 9 | import android.os.Bundle; 10 | import android.os.Handler; 11 | 12 | import java.util.HashSet; 13 | import java.util.Set; 14 | 15 | /** 16 | * Created by yhao on 17-12-1. 17 | * 用于控制悬浮窗显示周期 18 | * 使用了三种方法针对返回桌面时隐藏悬浮按钮 19 | * 1.startCount计数,针对back到桌面可以及时隐藏 20 | * 2.监听home键,从而及时隐藏 21 | * 3.resumeCount计时,针对一些只执行onPause不执行onStop的奇葩情况 22 | * 23 | * modify by bond on 2019-08-29 解耦和{@link IFloatWindowImpl} 24 | */ 25 | 26 | public class FloatLifecycle extends BroadcastReceiver implements Application.ActivityLifecycleCallbacks { 27 | 28 | private static final String SYSTEM_DIALOG_REASON_KEY = "reason"; 29 | private static final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey"; 30 | private static final long delay = 300; 31 | private Handler mHandler; 32 | private int startCount; 33 | private int resumeCount; 34 | private boolean appBackground; 35 | private static ResumedListener sResumedListener; 36 | private static int num = 0; 37 | 38 | private static Set set = new HashSet<>(); 39 | 40 | public static void register(IFloatWindowImpl floatWindow){ 41 | set.add(floatWindow); 42 | } 43 | 44 | public static void unregister(IFloatWindowImpl floatWindow){ 45 | set.remove(floatWindow); 46 | } 47 | 48 | public FloatLifecycle(Context applicationContext) { 49 | num++; 50 | mHandler = new Handler(); 51 | ((Application) applicationContext).registerActivityLifecycleCallbacks(this); 52 | applicationContext.registerReceiver(this, new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)); 53 | } 54 | 55 | public static void setResumedListener(ResumedListener resumedListener) { 56 | sResumedListener = resumedListener; 57 | } 58 | 59 | public void showOrHide(Activity activity){ 60 | for(IFloatWindowImpl window : set){ 61 | if(window.needShow(activity)){ 62 | window.show(); 63 | } else { 64 | window.hide(); 65 | } 66 | } 67 | } 68 | 69 | public void onBackToDesktop(){ 70 | for(IFloatWindowImpl window : set){ 71 | window.onBackToDesktop(); 72 | } 73 | } 74 | 75 | @Override 76 | public void onActivityResumed(Activity activity) { 77 | if (sResumedListener != null) { 78 | num--; 79 | if (num == 0) { 80 | sResumedListener.onResumed(); 81 | sResumedListener = null; 82 | } 83 | } 84 | resumeCount++; 85 | showOrHide(activity); 86 | if (appBackground) { 87 | appBackground = false; 88 | } 89 | } 90 | 91 | @Override 92 | public void onActivityPaused(final Activity activity) { 93 | resumeCount--; 94 | mHandler.postDelayed(new Runnable() { 95 | @Override 96 | public void run() { 97 | if (resumeCount == 0) { 98 | appBackground = true; 99 | onBackToDesktop(); 100 | } 101 | } 102 | }, delay); 103 | } 104 | 105 | @Override 106 | public void onActivityStarted(Activity activity) { 107 | startCount++; 108 | } 109 | 110 | 111 | @Override 112 | public void onActivityStopped(Activity activity) { 113 | startCount--; 114 | if (startCount == 0) { 115 | onBackToDesktop(); 116 | } 117 | } 118 | 119 | @Override 120 | public void onReceive(Context context, Intent intent) { 121 | String action = intent.getAction(); 122 | if (action != null && action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) { 123 | String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY); 124 | if (SYSTEM_DIALOG_REASON_HOME_KEY.equals(reason)) { 125 | onBackToDesktop(); 126 | } 127 | } 128 | } 129 | 130 | 131 | @Override 132 | public void onActivityCreated(Activity activity, Bundle savedInstanceState) { 133 | 134 | } 135 | 136 | 137 | @Override 138 | public void onActivitySaveInstanceState(Activity activity, Bundle outState) { 139 | 140 | } 141 | 142 | @Override 143 | public void onActivityDestroyed(Activity activity) { 144 | 145 | } 146 | 147 | 148 | } 149 | -------------------------------------------------------------------------------- /floatwindow/src/main/java/com/yhao/floatwindow/FloatPhone.java: -------------------------------------------------------------------------------- 1 | package com.yhao.floatwindow; 2 | 3 | import android.content.Context; 4 | import android.graphics.PixelFormat; 5 | import android.os.Build; 6 | import android.view.View; 7 | import android.view.WindowManager; 8 | 9 | /** 10 | * Created by yhao on 17-11-14. 11 | * https://github.com/yhaolpz 12 | */ 13 | 14 | class FloatPhone extends FloatView { 15 | 16 | private final Context mContext; 17 | 18 | private final WindowManager mWindowManager; 19 | private final WindowManager.LayoutParams mLayoutParams; 20 | private View mView; 21 | private int mX, mY; 22 | private boolean isRemove = false; 23 | private PermissionListener mPermissionListener; 24 | 25 | FloatPhone(Context applicationContext, PermissionListener permissionListener) { 26 | mContext = applicationContext; 27 | mPermissionListener = permissionListener; 28 | mWindowManager = (WindowManager) applicationContext.getSystemService(Context.WINDOW_SERVICE); 29 | mLayoutParams = new WindowManager.LayoutParams(); 30 | mLayoutParams.format = PixelFormat.RGBA_8888; 31 | mLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL 32 | | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE 33 | | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS; 34 | mLayoutParams.windowAnimations = 0; 35 | } 36 | 37 | @Override 38 | public void setSize(int width, int height) { 39 | mLayoutParams.width = width; 40 | mLayoutParams.height = height; 41 | } 42 | 43 | @Override 44 | public void setView(View view) { 45 | mView = view; 46 | } 47 | 48 | @Override 49 | public void setGravity(int gravity, int xOffset, int yOffset) { 50 | mLayoutParams.gravity = gravity; 51 | mLayoutParams.x = mX = xOffset; 52 | mLayoutParams.y = mY = yOffset; 53 | } 54 | 55 | 56 | @Override 57 | public void init() { 58 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { 59 | req(); 60 | } else if (Miui.rom()) { 61 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 62 | req(); 63 | } else { 64 | mLayoutParams.type = WindowManager.LayoutParams.TYPE_PHONE; 65 | Miui.req(mContext, new PermissionListener() { 66 | @Override 67 | public void onSuccess() { 68 | mWindowManager.addView(mView, mLayoutParams); 69 | if (mPermissionListener != null) { 70 | mPermissionListener.onSuccess(); 71 | } 72 | } 73 | 74 | @Override 75 | public void onFail() { 76 | if (mPermissionListener != null) { 77 | mPermissionListener.onFail(); 78 | } 79 | } 80 | }); 81 | } 82 | } else { 83 | try { 84 | mLayoutParams.type = WindowManager.LayoutParams.TYPE_TOAST; 85 | mWindowManager.addView(mView, mLayoutParams); 86 | } catch (Exception e) { 87 | mWindowManager.removeView(mView); 88 | LogUtil.e("TYPE_TOAST 失败"); 89 | req(); 90 | } 91 | } 92 | } 93 | 94 | private void req() { 95 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { 96 | mLayoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY; 97 | } else { 98 | mLayoutParams.type = WindowManager.LayoutParams.TYPE_PHONE; 99 | } 100 | FloatActivity.request(mContext, new PermissionListener() { 101 | @Override 102 | public void onSuccess() { 103 | mWindowManager.addView(mView, mLayoutParams); 104 | if (mPermissionListener != null) { 105 | mPermissionListener.onSuccess(); 106 | } 107 | } 108 | 109 | @Override 110 | public void onFail() { 111 | if (mPermissionListener != null) { 112 | mPermissionListener.onFail(); 113 | } 114 | } 115 | }); 116 | } 117 | 118 | @Override 119 | public void dismiss() { 120 | isRemove = true; 121 | mWindowManager.removeView(mView); 122 | } 123 | 124 | @Override 125 | public void updateXY(int x, int y) { 126 | if (isRemove) return; 127 | mLayoutParams.x = mX = x; 128 | mLayoutParams.y = mY = y; 129 | mWindowManager.updateViewLayout(mView, mLayoutParams); 130 | } 131 | 132 | @Override 133 | void updateX(int x) { 134 | if (isRemove) return; 135 | mLayoutParams.x = mX = x; 136 | mWindowManager.updateViewLayout(mView, mLayoutParams); 137 | } 138 | 139 | @Override 140 | void updateY(int y) { 141 | if (isRemove) return; 142 | mLayoutParams.y = mY = y; 143 | mWindowManager.updateViewLayout(mView, mLayoutParams); 144 | } 145 | 146 | @Override 147 | int getX() { 148 | return mX; 149 | } 150 | 151 | @Override 152 | int getY() { 153 | return mY; 154 | } 155 | 156 | 157 | } 158 | -------------------------------------------------------------------------------- /floatwindow/src/main/java/com/yhao/floatwindow/FloatToast.java: -------------------------------------------------------------------------------- 1 | package com.yhao.floatwindow; 2 | 3 | import android.content.Context; 4 | import android.view.View; 5 | import android.view.WindowManager; 6 | import android.widget.Toast; 7 | 8 | import java.lang.reflect.Field; 9 | import java.lang.reflect.Method; 10 | 11 | /** 12 | * 自定义 toast 方式,无需申请权限 13 | * 当前版本暂时用 TYPE_TOAST 代替,后续版本可能会再融入此方式 14 | */ 15 | 16 | class FloatToast extends FloatView { 17 | 18 | 19 | private Toast toast; 20 | 21 | private Object mTN; 22 | private Method show; 23 | private Method hide; 24 | 25 | private int mWidth; 26 | private int mHeight; 27 | 28 | 29 | FloatToast(Context applicationContext) { 30 | toast = new Toast(applicationContext); 31 | } 32 | 33 | 34 | @Override 35 | public void setSize(int width, int height) { 36 | mWidth = width; 37 | mHeight = height; 38 | } 39 | 40 | @Override 41 | public void setView(View view) { 42 | toast.setView(view); 43 | initTN(); 44 | } 45 | 46 | @Override 47 | public void setGravity(int gravity, int xOffset, int yOffset) { 48 | toast.setGravity(gravity, xOffset, yOffset); 49 | } 50 | 51 | @Override 52 | public void init() { 53 | try { 54 | show.invoke(mTN); 55 | } catch (Exception e) { 56 | e.printStackTrace(); 57 | } 58 | } 59 | 60 | @Override 61 | public void dismiss() { 62 | try { 63 | hide.invoke(mTN); 64 | } catch (Exception e) { 65 | e.printStackTrace(); 66 | } 67 | } 68 | 69 | 70 | private void initTN() { 71 | try { 72 | Field tnField = toast.getClass().getDeclaredField("mTN"); 73 | tnField.setAccessible(true); 74 | mTN = tnField.get(toast); 75 | show = mTN.getClass().getMethod("show"); 76 | hide = mTN.getClass().getMethod("hide"); 77 | Field tnParamsField = mTN.getClass().getDeclaredField("mParams"); 78 | tnParamsField.setAccessible(true); 79 | WindowManager.LayoutParams params = (WindowManager.LayoutParams) tnParamsField.get(mTN); 80 | params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL 81 | | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; 82 | params.width = mWidth; 83 | params.height = mHeight; 84 | params.windowAnimations = 0; 85 | Field tnNextViewField = mTN.getClass().getDeclaredField("mNextView"); 86 | tnNextViewField.setAccessible(true); 87 | tnNextViewField.set(mTN, toast.getView()); 88 | 89 | } catch (Exception e) { 90 | e.printStackTrace(); 91 | } 92 | } 93 | 94 | 95 | } 96 | -------------------------------------------------------------------------------- /floatwindow/src/main/java/com/yhao/floatwindow/FloatView.java: -------------------------------------------------------------------------------- 1 | package com.yhao.floatwindow; 2 | 3 | import android.view.View; 4 | 5 | /** 6 | * Created by yhao on 17-11-14. 7 | * https://github.com/yhaolpz 8 | */ 9 | 10 | abstract class FloatView { 11 | 12 | abstract void setSize(int width, int height); 13 | 14 | abstract void setView(View view); 15 | 16 | abstract void setGravity(int gravity, int xOffset, int yOffset); 17 | 18 | abstract void init(); 19 | 20 | abstract void dismiss(); 21 | 22 | void updateXY(int x, int y) { 23 | } 24 | 25 | void updateX(int x) { 26 | } 27 | 28 | void updateY(int y) { 29 | } 30 | 31 | int getX() { 32 | return 0; 33 | } 34 | 35 | int getY() { 36 | return 0; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /floatwindow/src/main/java/com/yhao/floatwindow/FloatWindow.java: -------------------------------------------------------------------------------- 1 | package com.yhao.floatwindow; 2 | 3 | import android.animation.TimeInterpolator; 4 | import android.app.Application; 5 | import android.content.Context; 6 | import android.support.annotation.LayoutRes; 7 | import android.support.annotation.MainThread; 8 | import android.support.annotation.NonNull; 9 | import android.support.annotation.Nullable; 10 | import android.view.Gravity; 11 | import android.view.View; 12 | import android.view.ViewGroup; 13 | 14 | import java.util.HashMap; 15 | import java.util.Map; 16 | 17 | /** 18 | * Created by yhao on 2017/12/22. 19 | * https://github.com/yhaolpz 20 | * 21 | * modify by bond on 2019-08-29 分离生命周期注册和浮动框创建逻辑。这样就可以在用到浮动框的地方才去创建浮动框。 22 | * 解决必须在Application中创建浮动框,否则会出问题的bug. 23 | */ 24 | 25 | public class FloatWindow { 26 | static FloatLifecycle floatLifecycle; 27 | 28 | public static void initLifecycle(Application application){ 29 | floatLifecycle = new FloatLifecycle(application); 30 | } 31 | 32 | private FloatWindow() { 33 | 34 | } 35 | 36 | private static final String mDefaultTag = "default_float_window_tag"; 37 | private static Map mFloatWindowMap; 38 | 39 | public static IFloatWindow get() { 40 | return get(mDefaultTag); 41 | } 42 | 43 | public static IFloatWindow get(@NonNull String tag) { 44 | return mFloatWindowMap == null ? null : mFloatWindowMap.get(tag); 45 | } 46 | 47 | private static B mBuilder = null; 48 | 49 | @MainThread 50 | public static B with(@NonNull Context applicationContext) { 51 | return mBuilder = new B(applicationContext); 52 | } 53 | 54 | public static void destroy() { 55 | destroy(mDefaultTag); 56 | } 57 | 58 | public static void destroy(String tag) { 59 | if (mFloatWindowMap == null || !mFloatWindowMap.containsKey(tag)) { 60 | return; 61 | } 62 | mFloatWindowMap.get(tag).dismiss(); 63 | mFloatWindowMap.remove(tag); 64 | } 65 | 66 | public static class B { 67 | Context mApplicationContext; 68 | View mView; 69 | private int mLayoutId; 70 | int mWidth = ViewGroup.LayoutParams.WRAP_CONTENT; 71 | int mHeight = ViewGroup.LayoutParams.WRAP_CONTENT; 72 | int gravity = Gravity.TOP | Gravity.START; 73 | int xOffset; 74 | int yOffset; 75 | boolean mShow = true; 76 | Class[] mActivities; 77 | int mMoveType = MoveType.slide; 78 | int mSlideLeftMargin; 79 | int mSlideRightMargin; 80 | long mDuration = 300; 81 | TimeInterpolator mInterpolator; 82 | private String mTag = mDefaultTag; 83 | boolean mDesktopShow; 84 | PermissionListener mPermissionListener; 85 | ViewStateListener mViewStateListener; 86 | 87 | private B() { 88 | 89 | } 90 | 91 | B(Context applicationContext) { 92 | mApplicationContext = applicationContext; 93 | } 94 | 95 | public B setView(@NonNull View view) { 96 | mView = view; 97 | return this; 98 | } 99 | 100 | public B setView(@LayoutRes int layoutId) { 101 | mLayoutId = layoutId; 102 | return this; 103 | } 104 | 105 | public B setWidth(int width) { 106 | mWidth = width; 107 | return this; 108 | } 109 | 110 | public B setHeight(int height) { 111 | mHeight = height; 112 | return this; 113 | } 114 | 115 | public B setWidth(@Screen.screenType int screenType, float ratio) { 116 | mWidth = (int) ((screenType == Screen.width ? 117 | Util.getScreenWidth(mApplicationContext) : 118 | Util.getScreenHeight(mApplicationContext)) * ratio); 119 | return this; 120 | } 121 | 122 | 123 | public B setHeight(@Screen.screenType int screenType, float ratio) { 124 | mHeight = (int) ((screenType == Screen.width ? 125 | Util.getScreenWidth(mApplicationContext) : 126 | Util.getScreenHeight(mApplicationContext)) * ratio); 127 | return this; 128 | } 129 | 130 | 131 | public B setX(int x) { 132 | xOffset = x; 133 | return this; 134 | } 135 | 136 | public B setY(int y) { 137 | yOffset = y; 138 | return this; 139 | } 140 | 141 | public B setX(@Screen.screenType int screenType, float ratio) { 142 | xOffset = (int) ((screenType == Screen.width ? 143 | Util.getScreenWidth(mApplicationContext) : 144 | Util.getScreenHeight(mApplicationContext)) * ratio); 145 | return this; 146 | } 147 | 148 | public B setY(@Screen.screenType int screenType, float ratio) { 149 | yOffset = (int) ((screenType == Screen.width ? 150 | Util.getScreenWidth(mApplicationContext) : 151 | Util.getScreenHeight(mApplicationContext)) * ratio); 152 | return this; 153 | } 154 | 155 | 156 | /** 157 | * 设置 Activity 过滤器,用于指定在哪些界面显示悬浮窗,默认全部界面都显示 158 | * 159 | * @param show  过滤类型,子类类型也会生效 160 | * @param activities  过滤界面 161 | */ 162 | public B setFilter(boolean show, @NonNull Class... activities) { 163 | mShow = show; 164 | mActivities = activities; 165 | return this; 166 | } 167 | 168 | public B setMoveType(@MoveType.MOVE_TYPE int moveType) { 169 | return setMoveType(moveType, 0, 0); 170 | } 171 | 172 | 173 | /** 174 | * 设置带边距的贴边动画,只有 moveType 为 MoveType.slide,设置边距才有意义,这个方法不标准,后面调整 175 | * 176 | * @param moveType 贴边动画 MoveType.slide 177 | * @param slideLeftMargin 贴边动画左边距,默认为 0 178 | * @param slideRightMargin 贴边动画右边距,默认为 0 179 | */ 180 | public B setMoveType(@MoveType.MOVE_TYPE int moveType, int slideLeftMargin, int slideRightMargin) { 181 | mMoveType = moveType; 182 | mSlideLeftMargin = slideLeftMargin; 183 | mSlideRightMargin = slideRightMargin; 184 | return this; 185 | } 186 | 187 | public B setMoveStyle(long duration, @Nullable TimeInterpolator interpolator) { 188 | mDuration = duration; 189 | mInterpolator = interpolator; 190 | return this; 191 | } 192 | 193 | public B setTag(@NonNull String tag) { 194 | mTag = tag; 195 | return this; 196 | } 197 | 198 | public B setDesktopShow(boolean show) { 199 | mDesktopShow = show; 200 | return this; 201 | } 202 | 203 | public B setPermissionListener(PermissionListener listener) { 204 | mPermissionListener = listener; 205 | return this; 206 | } 207 | 208 | public B setViewStateListener(ViewStateListener listener) { 209 | mViewStateListener = listener; 210 | return this; 211 | } 212 | 213 | public void build() { 214 | if (mFloatWindowMap == null) { 215 | mFloatWindowMap = new HashMap<>(); 216 | } 217 | if (mFloatWindowMap.containsKey(mTag)) { 218 | throw new IllegalArgumentException("FloatWindow of this tag has been added, Please set a new tag for the new FloatWindow"); 219 | } 220 | if (mView == null && mLayoutId == 0) { 221 | throw new IllegalArgumentException("View has not been set!"); 222 | } 223 | if (mView == null) { 224 | mView = Util.inflate(mApplicationContext, mLayoutId); 225 | } 226 | IFloatWindow floatWindowImpl = new IFloatWindowImpl(this); 227 | mFloatWindowMap.put(mTag, floatWindowImpl); 228 | } 229 | 230 | } 231 | } 232 | -------------------------------------------------------------------------------- /floatwindow/src/main/java/com/yhao/floatwindow/IFloatWindow.java: -------------------------------------------------------------------------------- 1 | package com.yhao.floatwindow; 2 | 3 | import android.view.View; 4 | 5 | /** 6 | * Created by yhao on 2017/12/22. 7 | * https://github.com/yhaolpz 8 | */ 9 | 10 | public abstract class IFloatWindow { 11 | public abstract void show(); 12 | 13 | public abstract void hide(); 14 | 15 | public abstract boolean isShowing(); 16 | 17 | public abstract int getX(); 18 | 19 | public abstract int getY(); 20 | 21 | public abstract void updateX(int x); 22 | 23 | public abstract void updateX(@Screen.screenType int screenType,float ratio); 24 | 25 | public abstract void updateY(int y); 26 | 27 | public abstract void updateY(@Screen.screenType int screenType,float ratio); 28 | 29 | public abstract View getView(); 30 | 31 | abstract void dismiss(); 32 | } 33 | -------------------------------------------------------------------------------- /floatwindow/src/main/java/com/yhao/floatwindow/IFloatWindowImpl.java: -------------------------------------------------------------------------------- 1 | package com.yhao.floatwindow; 2 | 3 | import android.animation.Animator; 4 | import android.animation.AnimatorListenerAdapter; 5 | import android.animation.ObjectAnimator; 6 | import android.animation.PropertyValuesHolder; 7 | import android.animation.TimeInterpolator; 8 | import android.animation.ValueAnimator; 9 | import android.annotation.SuppressLint; 10 | import android.app.Activity; 11 | import android.os.Build; 12 | import android.view.MotionEvent; 13 | import android.view.View; 14 | import android.view.ViewConfiguration; 15 | import android.view.animation.DecelerateInterpolator; 16 | 17 | /** 18 | * Created by yhao on 2017/12/22. 19 | * https://github.com/yhaolpz 20 | */ 21 | 22 | public class IFloatWindowImpl extends IFloatWindow { 23 | private FloatWindow.B mB; 24 | private FloatView mFloatView; 25 | private boolean isShow; 26 | private boolean once = true; 27 | private ValueAnimator mAnimator; 28 | private TimeInterpolator mDecelerateInterpolator; 29 | private float downX; 30 | private float downY; 31 | private float upX; 32 | private float upY; 33 | private boolean mClick = false; 34 | private int mSlop; 35 | 36 | private IFloatWindowImpl() { 37 | 38 | } 39 | 40 | IFloatWindowImpl(FloatWindow.B b) { 41 | mB = b; 42 | if (mB.mMoveType == MoveType.fixed) { 43 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { 44 | mFloatView = new FloatPhone(b.mApplicationContext, mB.mPermissionListener); 45 | } else { 46 | mFloatView = new FloatToast(b.mApplicationContext); 47 | } 48 | } else { 49 | mFloatView = new FloatPhone(b.mApplicationContext, mB.mPermissionListener); 50 | initTouchEvent(); 51 | } 52 | mFloatView.setSize(mB.mWidth, mB.mHeight); 53 | mFloatView.setGravity(mB.gravity, mB.xOffset, mB.yOffset); 54 | mFloatView.setView(mB.mView); 55 | FloatLifecycle.register(this); 56 | } 57 | 58 | @Override 59 | public void show() { 60 | if (once) { 61 | mFloatView.init(); 62 | once = false; 63 | isShow = true; 64 | } else { 65 | if (isShow) { 66 | return; 67 | } 68 | getView().setVisibility(View.VISIBLE); 69 | isShow = true; 70 | } 71 | if (mB.mViewStateListener != null) { 72 | mB.mViewStateListener.onShow(); 73 | } 74 | } 75 | 76 | @Override 77 | public void hide() { 78 | if (once || !isShow) { 79 | return; 80 | } 81 | getView().setVisibility(View.INVISIBLE); 82 | isShow = false; 83 | if (mB.mViewStateListener != null) { 84 | mB.mViewStateListener.onHide(); 85 | } 86 | } 87 | 88 | @Override 89 | public boolean isShowing() { 90 | return isShow; 91 | } 92 | 93 | @Override 94 | void dismiss() { 95 | mFloatView.dismiss(); 96 | isShow = false; 97 | if (mB.mViewStateListener != null) { 98 | mB.mViewStateListener.onDismiss(); 99 | } 100 | FloatLifecycle.unregister(this); 101 | } 102 | 103 | @Override 104 | public void updateX(int x) { 105 | checkMoveType(); 106 | mB.xOffset = x; 107 | mFloatView.updateX(x); 108 | } 109 | 110 | @Override 111 | public void updateY(int y) { 112 | checkMoveType(); 113 | mB.yOffset = y; 114 | mFloatView.updateY(y); 115 | } 116 | 117 | @Override 118 | public void updateX(int screenType, float ratio) { 119 | checkMoveType(); 120 | mB.xOffset = (int) ((screenType == Screen.width ? 121 | Util.getScreenWidth(mB.mApplicationContext) : 122 | Util.getScreenHeight(mB.mApplicationContext)) * ratio); 123 | mFloatView.updateX(mB.xOffset); 124 | 125 | } 126 | 127 | @Override 128 | public void updateY(int screenType, float ratio) { 129 | checkMoveType(); 130 | mB.yOffset = (int) ((screenType == Screen.width ? 131 | Util.getScreenWidth(mB.mApplicationContext) : 132 | Util.getScreenHeight(mB.mApplicationContext)) * ratio); 133 | mFloatView.updateY(mB.yOffset); 134 | 135 | } 136 | 137 | @Override 138 | public int getX() { 139 | return mFloatView.getX(); 140 | } 141 | 142 | @Override 143 | public int getY() { 144 | return mFloatView.getY(); 145 | } 146 | 147 | 148 | @Override 149 | public View getView() { 150 | mSlop = ViewConfiguration.get(mB.mApplicationContext).getScaledTouchSlop(); 151 | return mB.mView; 152 | } 153 | 154 | 155 | private void checkMoveType() { 156 | if (mB.mMoveType == MoveType.fixed) { 157 | throw new IllegalArgumentException("FloatWindow of this tag is not allowed to move!"); 158 | } 159 | } 160 | 161 | 162 | private void initTouchEvent() { 163 | switch (mB.mMoveType) { 164 | case MoveType.inactive: 165 | break; 166 | default: 167 | getView().setOnTouchListener(new View.OnTouchListener() { 168 | float lastX, lastY, changeX, changeY; 169 | int newX, newY; 170 | 171 | @SuppressLint("ClickableViewAccessibility") 172 | @Override 173 | public boolean onTouch(View v, MotionEvent event) { 174 | 175 | switch (event.getAction()) { 176 | case MotionEvent.ACTION_DOWN: 177 | downX = event.getRawX(); 178 | downY = event.getRawY(); 179 | lastX = event.getRawX(); 180 | lastY = event.getRawY(); 181 | cancelAnimator(); 182 | break; 183 | case MotionEvent.ACTION_MOVE: 184 | changeX = event.getRawX() - lastX; 185 | changeY = event.getRawY() - lastY; 186 | newX = (int) (mFloatView.getX() + changeX); 187 | newY = (int) (mFloatView.getY() + changeY); 188 | mFloatView.updateXY(newX, newY); 189 | if (mB.mViewStateListener != null) { 190 | mB.mViewStateListener.onPositionUpdate(newX, newY); 191 | } 192 | lastX = event.getRawX(); 193 | lastY = event.getRawY(); 194 | break; 195 | case MotionEvent.ACTION_UP: 196 | upX = event.getRawX(); 197 | upY = event.getRawY(); 198 | mClick = (Math.abs(upX - downX) > mSlop) || (Math.abs(upY - downY) > mSlop); 199 | switch (mB.mMoveType) { 200 | case MoveType.slide: 201 | int startX = mFloatView.getX(); 202 | int endX = (startX * 2 + v.getWidth() > Util.getScreenWidth(mB.mApplicationContext)) ? 203 | Util.getScreenWidth(mB.mApplicationContext) - v.getWidth() - mB.mSlideRightMargin : 204 | mB.mSlideLeftMargin; 205 | mAnimator = ObjectAnimator.ofInt(startX, endX); 206 | mAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 207 | @Override 208 | public void onAnimationUpdate(ValueAnimator animation) { 209 | int x = (int) animation.getAnimatedValue(); 210 | mFloatView.updateX(x); 211 | if (mB.mViewStateListener != null) { 212 | mB.mViewStateListener.onPositionUpdate(x, (int) upY); 213 | } 214 | } 215 | }); 216 | startAnimator(); 217 | break; 218 | case MoveType.back: 219 | PropertyValuesHolder pvhX = PropertyValuesHolder.ofInt("x", mFloatView.getX(), mB.xOffset); 220 | PropertyValuesHolder pvhY = PropertyValuesHolder.ofInt("y", mFloatView.getY(), mB.yOffset); 221 | mAnimator = ObjectAnimator.ofPropertyValuesHolder(pvhX, pvhY); 222 | mAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 223 | @Override 224 | public void onAnimationUpdate(ValueAnimator animation) { 225 | int x = (int) animation.getAnimatedValue("x"); 226 | int y = (int) animation.getAnimatedValue("y"); 227 | mFloatView.updateXY(x, y); 228 | if (mB.mViewStateListener != null) { 229 | mB.mViewStateListener.onPositionUpdate(x, y); 230 | } 231 | } 232 | }); 233 | startAnimator(); 234 | break; 235 | default: 236 | break; 237 | } 238 | break; 239 | default: 240 | break; 241 | } 242 | return mClick; 243 | } 244 | }); 245 | } 246 | } 247 | 248 | 249 | private void startAnimator() { 250 | if (mB.mInterpolator == null) { 251 | if (mDecelerateInterpolator == null) { 252 | mDecelerateInterpolator = new DecelerateInterpolator(); 253 | } 254 | mB.mInterpolator = mDecelerateInterpolator; 255 | } 256 | mAnimator.setInterpolator(mB.mInterpolator); 257 | mAnimator.addListener(new AnimatorListenerAdapter() { 258 | @Override 259 | public void onAnimationEnd(Animator animation) { 260 | mAnimator.removeAllUpdateListeners(); 261 | mAnimator.removeAllListeners(); 262 | mAnimator = null; 263 | if (mB.mViewStateListener != null) { 264 | mB.mViewStateListener.onMoveAnimEnd(); 265 | } 266 | } 267 | }); 268 | mAnimator.setDuration(mB.mDuration).start(); 269 | if (mB.mViewStateListener != null) { 270 | mB.mViewStateListener.onMoveAnimStart(); 271 | } 272 | } 273 | 274 | private void cancelAnimator() { 275 | if (mAnimator != null && mAnimator.isRunning()) { 276 | mAnimator.cancel(); 277 | } 278 | } 279 | 280 | public boolean needShow(Activity activity) { 281 | if (mB.mActivities == null) { 282 | return true; 283 | } 284 | for (Class a : mB.mActivities) { 285 | if (a.isInstance(activity)) { 286 | return mB.mShow; 287 | } 288 | } 289 | return !mB.mShow; 290 | } 291 | 292 | public void onBackToDesktop() { 293 | if (!mB.mDesktopShow) { 294 | hide(); 295 | } 296 | if (mB.mViewStateListener != null) { 297 | mB.mViewStateListener.onBackToDesktop(); 298 | } 299 | } 300 | } 301 | -------------------------------------------------------------------------------- /floatwindow/src/main/java/com/yhao/floatwindow/LifecycleListener.java: -------------------------------------------------------------------------------- 1 | package com.yhao.floatwindow; 2 | 3 | /** 4 | * Created by yhao on 2017/12/22. 5 | * https://github.com/yhaolpz 6 | */ 7 | 8 | interface LifecycleListener { 9 | 10 | void onShow(); 11 | 12 | void onHide(); 13 | 14 | void onBackToDesktop(); 15 | } 16 | -------------------------------------------------------------------------------- /floatwindow/src/main/java/com/yhao/floatwindow/LogUtil.java: -------------------------------------------------------------------------------- 1 | package com.yhao.floatwindow; 2 | 3 | import android.util.Log; 4 | 5 | 6 | /** 7 | * Created by yhao on 2017/12/29. 8 | * https://github.com/yhaolpz 9 | */ 10 | 11 | class LogUtil { 12 | 13 | private static final String TAG = "FloatWindow"; 14 | 15 | 16 | static void e(String message) { 17 | 18 | Log.e(TAG, message); 19 | } 20 | 21 | 22 | static void d(String message) { 23 | 24 | Log.d(TAG, message); 25 | } 26 | 27 | 28 | } 29 | -------------------------------------------------------------------------------- /floatwindow/src/main/java/com/yhao/floatwindow/Miui.java: -------------------------------------------------------------------------------- 1 | package com.yhao.floatwindow; 2 | 3 | import android.content.Context; 4 | import android.content.Intent; 5 | import android.net.Uri; 6 | import android.os.Build; 7 | import android.provider.Settings; 8 | import android.view.View; 9 | import android.view.WindowManager; 10 | 11 | import java.lang.reflect.Field; 12 | import java.util.ArrayList; 13 | import java.util.List; 14 | 15 | import static com.yhao.floatwindow.Rom.isIntentAvailable; 16 | 17 | /** 18 | * Created by yhao on 2017/12/30. 19 | * https://github.com/yhaolpz 20 | *

21 | * 需要清楚:一个MIUI版本对应小米各种机型,基于不同的安卓版本,但是权限设置页跟MIUI版本有关 22 | * 测试TYPE_TOAST类型: 23 | * 7.0: 24 | * 小米 5 MIUI8 -------------------- 失败 25 | * 小米 Note2 MIUI9 -------------------- 失败 26 | * 6.0.1 27 | * 小米 5 -------------------- 失败 28 | * 小米 红米note3 -------------------- 失败 29 | * 6.0: 30 | * 小米 5 -------------------- 成功 31 | * 小米 红米4A MIUI8 -------------------- 成功 32 | * 小米 红米Pro MIUI7 -------------------- 成功 33 | * 小米 红米Note4 MIUI8 -------------------- 失败 34 | *

35 | * 经过各种横向纵向测试对比,得出一个结论,就是小米对TYPE_TOAST的处理机制毫无规律可言! 36 | * 跟Android版本无关,跟MIUI版本无关,addView方法也不报错 37 | * 所以最后对小米6.0以上的适配方法是:不使用 TYPE_TOAST 类型,统一申请权限 38 | */ 39 | 40 | class Miui { 41 | 42 | private static final String miui = "ro.miui.ui.version.name"; 43 | private static final String miui5 = "V5"; 44 | private static final String miui6 = "V6"; 45 | private static final String miui7 = "V7"; 46 | private static final String miui8 = "V8"; 47 | private static final String miui9 = "V9"; 48 | private static List mPermissionListenerList; 49 | private static PermissionListener mPermissionListener; 50 | 51 | 52 | static boolean rom() { 53 | LogUtil.d(" Miui : " + Miui.getProp()); 54 | return Build.MANUFACTURER.equals("Xiaomi"); 55 | } 56 | 57 | private static String getProp() { 58 | return Rom.getProp(miui); 59 | } 60 | 61 | /** 62 | * Android6.0以下申请权限 63 | */ 64 | static void req(final Context context, PermissionListener permissionListener) { 65 | if (PermissionUtil.hasPermission(context)) { 66 | permissionListener.onSuccess(); 67 | return; 68 | } 69 | if (mPermissionListenerList == null) { 70 | mPermissionListenerList = new ArrayList<>(); 71 | mPermissionListener = new PermissionListener() { 72 | @Override 73 | public void onSuccess() { 74 | for (PermissionListener listener : mPermissionListenerList) { 75 | listener.onSuccess(); 76 | } 77 | mPermissionListenerList.clear(); 78 | } 79 | @Override 80 | public void onFail() { 81 | for (PermissionListener listener : mPermissionListenerList) { 82 | listener.onFail(); 83 | } 84 | mPermissionListenerList.clear(); 85 | } 86 | }; 87 | req_(context); 88 | } 89 | mPermissionListenerList.add(permissionListener); 90 | } 91 | 92 | 93 | private static void req_(final Context context) { 94 | switch (getProp()) { 95 | case miui5: 96 | reqForMiui5(context); 97 | break; 98 | case miui6: 99 | case miui7: 100 | reqForMiui67(context); 101 | break; 102 | case miui8: 103 | case miui9: 104 | reqForMiui89(context); 105 | break; 106 | } 107 | FloatLifecycle.setResumedListener(new ResumedListener() { 108 | @Override 109 | public void onResumed() { 110 | if (PermissionUtil.hasPermission(context)) { 111 | mPermissionListener.onSuccess(); 112 | } else { 113 | mPermissionListener.onFail(); 114 | } 115 | } 116 | }); 117 | } 118 | 119 | 120 | private static void reqForMiui5(Context context) { 121 | String packageName = context.getPackageName(); 122 | Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); 123 | Uri uri = Uri.fromParts("package", packageName, null); 124 | intent.setData(uri); 125 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 126 | if (isIntentAvailable(intent, context)) { 127 | context.startActivity(intent); 128 | } else { 129 | LogUtil.e("intent is not available!"); 130 | } 131 | } 132 | 133 | private static void reqForMiui67(Context context) { 134 | Intent intent = new Intent("miui.intent.action.APP_PERM_EDITOR"); 135 | intent.setClassName("com.miui.securitycenter", 136 | "com.miui.permcenter.permissions.AppPermissionsEditorActivity"); 137 | intent.putExtra("extra_pkgname", context.getPackageName()); 138 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 139 | if (isIntentAvailable(intent, context)) { 140 | context.startActivity(intent); 141 | } else { 142 | LogUtil.e("intent is not available!"); 143 | } 144 | } 145 | 146 | private static void reqForMiui89(Context context) { 147 | Intent intent = new Intent("miui.intent.action.APP_PERM_EDITOR"); 148 | intent.setClassName("com.miui.securitycenter", "com.miui.permcenter.permissions.PermissionsEditorActivity"); 149 | intent.putExtra("extra_pkgname", context.getPackageName()); 150 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 151 | if (isIntentAvailable(intent, context)) { 152 | context.startActivity(intent); 153 | } else { 154 | intent = new Intent("miui.intent.action.APP_PERM_EDITOR"); 155 | intent.setPackage("com.miui.securitycenter"); 156 | intent.putExtra("extra_pkgname", context.getPackageName()); 157 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 158 | if (isIntentAvailable(intent, context)) { 159 | context.startActivity(intent); 160 | } else { 161 | LogUtil.e("intent is not available!"); 162 | } 163 | } 164 | } 165 | 166 | 167 | /** 168 | * 有些机型在添加TYPE-TOAST类型时会自动改为TYPE_SYSTEM_ALERT,通过此方法可以屏蔽修改 169 | * 但是...即使成功显示出悬浮窗,移动的话也会崩溃 170 | */ 171 | private static void addViewToWindow(WindowManager wm, View view, WindowManager.LayoutParams params) { 172 | setMiUI_International(true); 173 | wm.addView(view, params); 174 | setMiUI_International(false); 175 | } 176 | 177 | 178 | private static void setMiUI_International(boolean flag) { 179 | try { 180 | Class BuildForMi = Class.forName("miui.os.Build"); 181 | Field isInternational = BuildForMi.getDeclaredField("IS_INTERNATIONAL_BUILD"); 182 | isInternational.setAccessible(true); 183 | isInternational.setBoolean(null, flag); 184 | } catch (Exception e) { 185 | e.printStackTrace(); 186 | } 187 | } 188 | 189 | 190 | } 191 | -------------------------------------------------------------------------------- /floatwindow/src/main/java/com/yhao/floatwindow/MoveType.java: -------------------------------------------------------------------------------- 1 | package com.yhao.floatwindow; 2 | 3 | import android.support.annotation.IntDef; 4 | 5 | import java.lang.annotation.Retention; 6 | import java.lang.annotation.RetentionPolicy; 7 | 8 | /** 9 | * Created by yhao on 2017/12/22. 10 | * https://github.com/yhaolpz 11 | */ 12 | 13 | public class MoveType { 14 | static final int fixed = 0; 15 | public static final int inactive = 1; 16 | public static final int active = 2; 17 | public static final int slide = 3; 18 | public static final int back = 4; 19 | 20 | @IntDef({fixed, inactive, active, slide, back}) 21 | @Retention(RetentionPolicy.SOURCE) 22 | @interface MOVE_TYPE { 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /floatwindow/src/main/java/com/yhao/floatwindow/PermissionListener.java: -------------------------------------------------------------------------------- 1 | package com.yhao.floatwindow; 2 | 3 | /** 4 | * Created by yhao on 2017/11/14. 5 | * https://github.com/yhaolpz 6 | */ 7 | public interface PermissionListener { 8 | void onSuccess(); 9 | 10 | void onFail(); 11 | } 12 | -------------------------------------------------------------------------------- /floatwindow/src/main/java/com/yhao/floatwindow/PermissionUtil.java: -------------------------------------------------------------------------------- 1 | package com.yhao.floatwindow; 2 | 3 | import android.app.AppOpsManager; 4 | import android.content.Context; 5 | import android.graphics.PixelFormat; 6 | import android.os.Binder; 7 | import android.os.Build; 8 | import android.provider.Settings; 9 | import android.support.annotation.RequiresApi; 10 | import android.view.View; 11 | import android.view.WindowManager; 12 | 13 | import java.lang.reflect.Method; 14 | 15 | /** 16 | * Created by yhao on 2017/12/29. 17 | * https://github.com/yhaolpz 18 | */ 19 | 20 | class PermissionUtil { 21 | 22 | static boolean hasPermission(Context context) { 23 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 24 | return Settings.canDrawOverlays(context); 25 | } else { 26 | return hasPermissionBelowMarshmallow(context); 27 | } 28 | } 29 | 30 | static boolean hasPermissionOnActivityResult(Context context) { 31 | if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O) { 32 | return hasPermissionForO(context); 33 | } 34 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 35 | return Settings.canDrawOverlays(context); 36 | } else { 37 | return hasPermissionBelowMarshmallow(context); 38 | } 39 | } 40 | 41 | /** 42 | * 6.0以下判断是否有权限 43 | * 理论上6.0以上才需处理权限,但有的国内rom在6.0以下就添加了权限 44 | * 其实此方式也可以用于判断6.0以上版本,只不过有更简单的canDrawOverlays代替 45 | */ 46 | static boolean hasPermissionBelowMarshmallow(Context context) { 47 | try { 48 | AppOpsManager manager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE); 49 | Method dispatchMethod = AppOpsManager.class.getMethod("checkOp", int.class, int.class, String.class); 50 | //AppOpsManager.OP_SYSTEM_ALERT_WINDOW = 24 51 | return AppOpsManager.MODE_ALLOWED == (Integer) dispatchMethod.invoke( 52 | manager, 24, Binder.getCallingUid(), context.getApplicationContext().getPackageName()); 53 | } catch (Exception e) { 54 | return false; 55 | } 56 | } 57 | 58 | 59 | /** 60 | * 用于判断8.0时是否有权限,仅用于OnActivityResult 61 | * 针对8.0官方bug:在用户授予权限后Settings.canDrawOverlays或checkOp方法判断仍然返回false 62 | */ 63 | @RequiresApi(api = Build.VERSION_CODES.M) 64 | private static boolean hasPermissionForO(Context context) { 65 | try { 66 | WindowManager mgr = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); 67 | if (mgr == null) return false; 68 | View viewToAdd = new View(context); 69 | WindowManager.LayoutParams params = new WindowManager.LayoutParams(0, 0, 70 | android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O ? 71 | WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY : WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, 72 | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, 73 | PixelFormat.TRANSPARENT); 74 | viewToAdd.setLayoutParams(params); 75 | mgr.addView(viewToAdd, params); 76 | mgr.removeView(viewToAdd); 77 | return true; 78 | } catch (Exception e) { 79 | LogUtil.e("hasPermissionForO e:" + e.toString()); 80 | } 81 | return false; 82 | } 83 | 84 | 85 | } 86 | -------------------------------------------------------------------------------- /floatwindow/src/main/java/com/yhao/floatwindow/ResumedListener.java: -------------------------------------------------------------------------------- 1 | package com.yhao.floatwindow; 2 | 3 | /** 4 | * Created by yhao on 2017/12/30. 5 | * https://github.com/yhaolpz 6 | */ 7 | 8 | interface ResumedListener { 9 | void onResumed(); 10 | } 11 | -------------------------------------------------------------------------------- /floatwindow/src/main/java/com/yhao/floatwindow/Rom.java: -------------------------------------------------------------------------------- 1 | package com.yhao.floatwindow; 2 | 3 | import android.content.Context; 4 | import android.content.Intent; 5 | import android.content.pm.PackageManager; 6 | 7 | import java.io.BufferedReader; 8 | import java.io.IOException; 9 | import java.io.InputStreamReader; 10 | 11 | /** 12 | * Created by yhao on 2017/12/30. 13 | * https://github.com/yhaolpz 14 | */ 15 | 16 | class Rom { 17 | 18 | static boolean isIntentAvailable(Intent intent, Context context) { 19 | return intent != null && context.getPackageManager().queryIntentActivities( 20 | intent, PackageManager.MATCH_DEFAULT_ONLY).size() > 0; 21 | } 22 | 23 | 24 | static String getProp(String name) { 25 | BufferedReader input = null; 26 | try { 27 | Process p = Runtime.getRuntime().exec("getprop " + name); 28 | input = new BufferedReader(new InputStreamReader(p.getInputStream()), 1024); 29 | String line = input.readLine(); 30 | input.close(); 31 | return line; 32 | } catch (IOException ex) { 33 | return null; 34 | } finally { 35 | if (input != null) { 36 | try { 37 | input.close(); 38 | } catch (IOException e) { 39 | e.printStackTrace(); 40 | } 41 | } 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /floatwindow/src/main/java/com/yhao/floatwindow/Screen.java: -------------------------------------------------------------------------------- 1 | package com.yhao.floatwindow; 2 | 3 | import android.support.annotation.IntDef; 4 | 5 | import java.lang.annotation.Retention; 6 | import java.lang.annotation.RetentionPolicy; 7 | 8 | /** 9 | * Created by yhao on 2017/12/23. 10 | * https://github.com/yhaolpz 11 | */ 12 | 13 | public class Screen { 14 | public static final int width = 0; 15 | public static final int height = 1; 16 | 17 | @IntDef({width, height}) 18 | @Retention(RetentionPolicy.SOURCE) 19 | @interface screenType { 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /floatwindow/src/main/java/com/yhao/floatwindow/Util.java: -------------------------------------------------------------------------------- 1 | package com.yhao.floatwindow; 2 | 3 | import android.content.Context; 4 | import android.graphics.PixelFormat; 5 | import android.graphics.Point; 6 | import android.graphics.Rect; 7 | import android.os.Build; 8 | import android.provider.Settings; 9 | import android.support.annotation.RequiresApi; 10 | import android.view.LayoutInflater; 11 | import android.view.View; 12 | import android.view.WindowManager; 13 | 14 | import java.lang.reflect.Method; 15 | 16 | /** 17 | * Created by yhao on 2017/12/22. 18 | * https://github.com/yhaolpz 19 | */ 20 | 21 | class Util { 22 | 23 | 24 | static View inflate(Context applicationContext, int layoutId) { 25 | LayoutInflater inflate = (LayoutInflater) applicationContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 26 | return inflate.inflate(layoutId, null); 27 | } 28 | 29 | private static Point sPoint; 30 | 31 | static int getScreenWidth(Context context) { 32 | if (sPoint == null) { 33 | sPoint = new Point(); 34 | WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); 35 | wm.getDefaultDisplay().getSize(sPoint); 36 | } 37 | return sPoint.x; 38 | } 39 | 40 | static int getScreenHeight(Context context) { 41 | if (sPoint == null) { 42 | sPoint = new Point(); 43 | WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); 44 | wm.getDefaultDisplay().getSize(sPoint); 45 | } 46 | return sPoint.y; 47 | } 48 | 49 | static boolean isViewVisible(View view) { 50 | return view.getGlobalVisibleRect(new Rect()); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /floatwindow/src/main/java/com/yhao/floatwindow/ViewStateListener.java: -------------------------------------------------------------------------------- 1 | package com.yhao.floatwindow; 2 | 3 | /** 4 | * Created by yhao on 2018/5/5 5 | * https://github.com/yhaolpz 6 | */ 7 | public interface ViewStateListener { 8 | void onPositionUpdate(int x, int y); 9 | 10 | void onShow(); 11 | 12 | void onHide(); 13 | 14 | void onDismiss(); 15 | 16 | void onMoveAnimStart(); 17 | 18 | void onMoveAnimEnd(); 19 | 20 | void onBackToDesktop(); 21 | } 22 | -------------------------------------------------------------------------------- /floatwindow/src/main/java/com/yhao/floatwindow/ViewStateListenerAdapter.java: -------------------------------------------------------------------------------- 1 | package com.yhao.floatwindow; 2 | 3 | /** 4 | * Created by yhao on 2018/5/5. 5 | * https://github.com/yhaolpz 6 | */ 7 | public class ViewStateListenerAdapter implements ViewStateListener{ 8 | @Override 9 | public void onPositionUpdate(int x, int y) { 10 | 11 | } 12 | 13 | @Override 14 | public void onShow() { 15 | 16 | } 17 | 18 | @Override 19 | public void onHide() { 20 | 21 | } 22 | 23 | @Override 24 | public void onDismiss() { 25 | 26 | } 27 | 28 | @Override 29 | public void onMoveAnimStart() { 30 | 31 | } 32 | 33 | @Override 34 | public void onMoveAnimEnd() { 35 | 36 | } 37 | 38 | @Override 39 | public void onBackToDesktop() { 40 | 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /floatwindow/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | FFWindow 3 | 4 | -------------------------------------------------------------------------------- /floatwindow/src/main/res/values/style.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | -------------------------------------------------------------------------------- /floatwindow/src/test/java/com/example/fixedfloatwindow/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.example.fixedfloatwindow; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() throws Exception { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | org.gradle.jvmargs=-Xmx1536m 13 | 14 | # When configured, Gradle will run in incubating parallel mode. 15 | # This option should only be used with decoupled projects. More details, visit 16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 17 | # org.gradle.parallel=true 18 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/baneyue/FloatWindow/86c249edaa5f537e8a03b3a9e427090e072d0d2d/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Tue Nov 14 22:07:14 GMT+08:00 2017 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /img.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/baneyue/FloatWindow/86c249edaa5f537e8a03b3a9e427090e072d0d2d/img.gif -------------------------------------------------------------------------------- /pay.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/baneyue/FloatWindow/86c249edaa5f537e8a03b3a9e427090e072d0d2d/pay.jpg -------------------------------------------------------------------------------- /sample/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /sample/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 26 5 | buildToolsVersion "26.0.2" 6 | 7 | defaultConfig { 8 | applicationId "com.example.yhao.fixedfloatwindow" 9 | minSdkVersion 19 10 | targetSdkVersion 26 11 | versionCode 1 12 | versionName "1.0" 13 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 14 | } 15 | buildTypes { 16 | release { 17 | minifyEnabled false 18 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 19 | } 20 | } 21 | } 22 | 23 | dependencies { 24 | compile fileTree(dir: 'libs', include: ['*.jar']) 25 | compile project(':floatwindow') 26 | compile 'com.android.support:appcompat-v7:26.+' 27 | compile 'com.android.support.constraint:constraint-layout:1.0.2' 28 | testCompile 'junit:junit:4.12' 29 | } 30 | -------------------------------------------------------------------------------- /sample/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /sample/src/androidTest/java/com/example/yhao/fixedfloatwindow/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.example.yhao.fixedfloatwindow; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumented test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.example.yhao.fixedfloatwindow", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /sample/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /sample/src/main/java/com/example/yhao/floatwindow/A_Activity.java: -------------------------------------------------------------------------------- 1 | package com.example.yhao.floatwindow; 2 | 3 | import android.content.Intent; 4 | import android.os.Bundle; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.view.View; 7 | import android.view.animation.BounceInterpolator; 8 | import android.widget.ImageView; 9 | import android.widget.Toast; 10 | 11 | import com.example.yhao.fixedfloatwindow.R; 12 | import com.yhao.floatwindow.FloatWindow; 13 | import com.yhao.floatwindow.MoveType; 14 | import com.yhao.floatwindow.Screen; 15 | 16 | public class A_Activity extends AppCompatActivity { 17 | 18 | 19 | @Override 20 | protected void onCreate(Bundle savedInstanceState) { 21 | super.onCreate(savedInstanceState); 22 | setContentView(R.layout.activity_a); 23 | setTitle("A"); 24 | } 25 | 26 | public void change(View view) { 27 | startActivity(new Intent(this, B_Activity.class)); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /sample/src/main/java/com/example/yhao/floatwindow/B_Activity.java: -------------------------------------------------------------------------------- 1 | package com.example.yhao.floatwindow; 2 | 3 | import android.content.Intent; 4 | import android.os.Bundle; 5 | import android.view.View; 6 | 7 | import com.example.yhao.fixedfloatwindow.R; 8 | 9 | public class B_Activity extends BaseActivity { 10 | 11 | 12 | @Override 13 | protected void onCreate(Bundle savedInstanceState) { 14 | super.onCreate(savedInstanceState); 15 | setContentView(R.layout.activity_b); 16 | setTitle("B"); 17 | } 18 | 19 | public void change(View view) { 20 | startActivity(new Intent(this, C_Activity.class)); 21 | } 22 | 23 | public void back(View view) { 24 | finish(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /sample/src/main/java/com/example/yhao/floatwindow/BaseActivity.java: -------------------------------------------------------------------------------- 1 | package com.example.yhao.floatwindow; 2 | 3 | import android.support.v7.app.AppCompatActivity; 4 | import android.os.Bundle; 5 | 6 | import com.example.yhao.fixedfloatwindow.R; 7 | 8 | public class BaseActivity extends AppCompatActivity { 9 | 10 | @Override 11 | protected void onCreate(Bundle savedInstanceState) { 12 | super.onCreate(savedInstanceState); 13 | setContentView(R.layout.activity_d); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /sample/src/main/java/com/example/yhao/floatwindow/BaseApplication.java: -------------------------------------------------------------------------------- 1 | package com.example.yhao.floatwindow; 2 | 3 | import android.app.Application; 4 | import android.os.Build; 5 | import android.util.Log; 6 | import android.view.View; 7 | import android.view.animation.BounceInterpolator; 8 | import android.widget.ImageView; 9 | import android.widget.Toast; 10 | 11 | import com.example.yhao.fixedfloatwindow.R; 12 | import com.yhao.floatwindow.FloatWindow; 13 | import com.yhao.floatwindow.MoveType; 14 | import com.yhao.floatwindow.PermissionListener; 15 | import com.yhao.floatwindow.Screen; 16 | import com.yhao.floatwindow.ViewStateListener; 17 | 18 | /** 19 | * Created by yhao on 2017/12/18. 20 | * https://github.com/yhaolpz 21 | */ 22 | 23 | public class BaseApplication extends Application { 24 | 25 | 26 | private static final String TAG = "FloatWindow"; 27 | 28 | @Override 29 | public void onCreate() { 30 | super.onCreate(); 31 | 32 | ImageView imageView = new ImageView(getApplicationContext()); 33 | imageView.setImageResource(R.drawable.icon); 34 | 35 | // 分离生命周期和创建浮动框 36 | FloatWindow.initLifecycle(this); 37 | FloatWindow 38 | .with(getApplicationContext()) 39 | .setView(imageView) 40 | .setWidth(Screen.width, 0.2f) //设置悬浮控件宽高 41 | .setHeight(Screen.width, 0.2f) 42 | .setX(Screen.width, 0.8f) 43 | .setY(Screen.height, 0.3f) 44 | .setMoveType(MoveType.slide,100,-100) 45 | .setMoveStyle(500, new BounceInterpolator()) 46 | .setFilter(true, A_Activity.class, C_Activity.class) 47 | .setViewStateListener(mViewStateListener) 48 | .setPermissionListener(mPermissionListener) 49 | .setDesktopShow(true) 50 | .build(); 51 | 52 | 53 | imageView.setOnClickListener(new View.OnClickListener() { 54 | @Override 55 | public void onClick(View v) { 56 | Toast.makeText(BaseApplication.this, "onClick", Toast.LENGTH_SHORT).show(); 57 | } 58 | }); 59 | } 60 | 61 | private PermissionListener mPermissionListener = new PermissionListener() { 62 | @Override 63 | public void onSuccess() { 64 | Log.d(TAG, "onSuccess"); 65 | } 66 | 67 | @Override 68 | public void onFail() { 69 | Log.d(TAG, "onFail"); 70 | } 71 | }; 72 | 73 | private ViewStateListener mViewStateListener = new ViewStateListener() { 74 | @Override 75 | public void onPositionUpdate(int x, int y) { 76 | Log.d(TAG, "onPositionUpdate: x=" + x + " y=" + y); 77 | } 78 | 79 | @Override 80 | public void onShow() { 81 | Log.d(TAG, "onShow"); 82 | } 83 | 84 | @Override 85 | public void onHide() { 86 | Log.d(TAG, "onHide"); 87 | } 88 | 89 | @Override 90 | public void onDismiss() { 91 | Log.d(TAG, "onDismiss"); 92 | } 93 | 94 | @Override 95 | public void onMoveAnimStart() { 96 | Log.d(TAG, "onMoveAnimStart"); 97 | } 98 | 99 | @Override 100 | public void onMoveAnimEnd() { 101 | Log.d(TAG, "onMoveAnimEnd"); 102 | } 103 | 104 | @Override 105 | public void onBackToDesktop() { 106 | Log.d(TAG, "onBackToDesktop"); 107 | } 108 | }; 109 | } 110 | -------------------------------------------------------------------------------- /sample/src/main/java/com/example/yhao/floatwindow/C_Activity.java: -------------------------------------------------------------------------------- 1 | package com.example.yhao.floatwindow; 2 | 3 | import android.os.Bundle; 4 | import android.view.View; 5 | 6 | import com.example.yhao.fixedfloatwindow.R; 7 | 8 | public class C_Activity extends BaseActivity { 9 | 10 | @Override 11 | protected void onCreate(Bundle savedInstanceState) { 12 | super.onCreate(savedInstanceState); 13 | setContentView(R.layout.activity_c); 14 | setTitle("C"); 15 | 16 | } 17 | 18 | public void back(View view) { 19 | finish(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /sample/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /sample/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 11 | 16 | 21 | 26 | 31 | 36 | 41 | 46 | 51 | 56 | 61 | 66 | 71 | 76 | 81 | 86 | 91 | 96 | 101 | 106 | 111 | 116 | 121 | 126 | 131 | 136 | 141 | 146 | 151 | 156 | 161 | 166 | 171 | 172 | -------------------------------------------------------------------------------- /sample/src/main/res/drawable/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/baneyue/FloatWindow/86c249edaa5f537e8a03b3a9e427090e072d0d2d/sample/src/main/res/drawable/icon.png -------------------------------------------------------------------------------- /sample/src/main/res/layout/activity_a.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 12 |