├── .gitignore ├── README.md ├── README_EN.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── imuxuan │ │ └── en │ │ └── floatingview │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── imuxuan │ │ │ └── en │ │ │ └── floatingview │ │ │ ├── BaseActivity.java │ │ │ └── TestActivity.java │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ └── ic_launcher_background.xml │ │ ├── layout │ │ └── main.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 │ └── imuxuan │ └── en │ └── floatingview │ └── ExampleUnitTest.java ├── bintray.gradle ├── build.gradle ├── floatingview ├── .gitignore ├── build.gradle ├── gradle.properties ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── imuxuan │ │ └── floatingview │ │ ├── EnFloatingView.java │ │ ├── FloatingMagnetView.java │ │ ├── FloatingView.java │ │ ├── IFloatingView.java │ │ ├── MagnetViewListener.java │ │ └── utils │ │ ├── EnContext.java │ │ └── SystemUtils.java │ └── res │ ├── drawable │ └── imuxuan.png │ ├── layout │ └── en_floating_view.xml │ └── values │ └── strings.xml ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── preview ├── 1.gif ├── 2.gif └── logo.png └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea 5 | .DS_Store 6 | /build 7 | /captures 8 | .externalNativeBuild 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Logo](https://raw.githubusercontent.com/leotyndale/EnFloatingView/master/preview/logo.png)](http://www.imuxuan.com/) 2 | 3 | EnFloatingView 4 | ========================== 5 | [![Jcenter](https://img.shields.io/badge/Jcenter-v1.5-brightgreen.svg?style=flat)](https://bintray.com/leotyndale/Muxuan/EnFloatingView) 6 | [![Muxuan](https://img.shields.io/badge/PoweredBy-Muxuan-green.svg?style=flat)](http://www.imuxuan.com/) 7 | [![Website](https://img.shields.io/website-up-down-green-red/https/shields.io.svg?label=Blog)](http://blog.imuxuan.com) 8 | 9 | [![《移动开发架构设计实战》](https://upload-images.jianshu.io/upload_images/14802001-4864444c478c88ee.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)](https://item.jd.com/12730926.html) 10 | 11 | [🔥🔥🔥 马上重构你的架构思维](https://item.jd.com/12730926.html) 12 | 13 | ========================== 14 | 15 | [English](/README_EN.md) 16 | 17 | 无需一切权限,不受各种国产ROM限制,默认可以显示的应用内悬浮窗。 18 | 19 | ### 传统方案 20 | 21 | 对于传统悬浮窗和一些古老的“黑科技”悬浮窗的实现,想必已经有很多成熟的案例了,实现策略基本为以下两种: 22 | 23 | 1. TYPE_SYSTEM_ALERT类型 24 | 25 | ```java 26 | mWindowManager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE); 27 | WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams() 28 | layoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT; 29 | ``` 30 | 31 | ​ 需要权限: 32 | 33 | ```java 34 | 35 | ``` 36 | 37 | 2. TYPE_TOAST / TYPE_PHONE 类型 38 | 39 | ​ 7.1.1以下不需要权限声明,在魅族、华为、小米等机型上默认隐藏,需要引导用户打开悬浮窗。 40 | 41 | ### 传统方案的问题 42 | 43 | 第一种方案因为存在各种限制,不能被众多开发采纳,故而比较流行的悬浮窗实现方式是第二种。 44 | 45 | 但是,我们有自己的原则: 46 | 47 | - 不能接受7.1.1以上机型,使用第二种方式实现悬浮窗仍需要用户主动授予权限的操作? 48 | - 不能接受在魅族、华为、小米等机型上默认隐藏,需要引导用户打开悬浮窗,就像这样 49 | 50 | ![权限管理](https://github.com/leotyndale/EnFloatingView/blob/master/preview/1.gif) 51 | 52 | ### 功能 53 | 54 | - 应用内显示,无需申请任何权限 55 | - 应用内显示,所有机型都可以默认显示悬浮窗,无需引导用户做更多设置 56 | - 支持拖拽 57 | - 超出屏幕限制移动 58 | - 可自动吸附到屏幕边缘 59 | 60 | ### 基本使用规则 61 | 62 | 1.在gralde的dependencies中加入 63 | ```java 64 | compile 'com.imuxuan:floatingview:1.6' 65 | ``` 66 | 67 | 68 | 2.在基类Activity(注意必须是基类Activity)中的onStart和onStop(或者安卓原生ActivityLifeCycle监听)中添加如下代码 69 | 70 | ```java 71 | @Override 72 | protected void onStart() { 73 | super.onStart(); 74 | FloatingView.get().attach(this); 75 | } 76 | 77 | @Override 78 | protected void onStop() { 79 | super.onStop(); 80 | FloatingView.get().detach(this); 81 | } 82 | ``` 83 | 84 | 85 | 3.展示悬浮窗 86 | 87 | ```java 88 | FloatingView.get().add(); 89 | ``` 90 | 91 | ### 扩展用法 92 | 93 | 1.销毁悬浮窗 94 | 95 | ```java 96 | FloatingView.get().remove(); 97 | ``` 98 | 99 | 2.添加点击事件 100 | 101 | ```java 102 | FloatingView.get().listener(new MagnetViewListener() { 103 | @Override 104 | public void onRemove(FloatingMagnetView magnetView) { 105 | Toast.makeText(TestActivity.this, "我没了", Toast.LENGTH_SHORT).show(); 106 | } 107 | 108 | @Override 109 | public void onClick(FloatingMagnetView magnetView) { 110 | Toast.makeText(TestActivity.this, "点到我了", Toast.LENGTH_SHORT).show(); 111 | } 112 | }); 113 | ``` 114 | 115 | 3.获得悬浮窗View 116 | 117 | ```java 118 | FloatingView.get().getView(); 119 | ``` 120 | 121 | 4.设置悬浮窗icon 122 | 123 | ```java 124 | FloatingView.get().icon(R.drawable.XXXXX); 125 | ``` 126 | 127 | 5.设置悬浮窗View 128 | 129 | ```java 130 | FloatingView.get().customView(new View()); 131 | or 132 | FloatingView.get().customView(R.layout.XXXXX); 133 | ``` 134 | 135 | 6.设置悬浮窗位置等布局参数 136 | 137 | ```java 138 | FloatingView.get().layoutParams(new ViewGroup.LayoutParams()); 139 | ``` 140 | 141 | ### 效果图 142 | ![预览图](https://github.com/leotyndale/EnFloatingView/blob/master/preview/2.gif) 143 | 144 | ### 更新记录 145 | 146 | 1.6 147 | 148 | 修复横竖屏切换错位 & 添加到布局时偶发崩溃 149 | 150 | 1.5 151 | 152 | 修复内存泄露问题 153 | 154 | 1.4 155 | 156 | 适配折叠屏 157 | 158 | 1.3 159 | 160 | 增加自定义layout等API 161 | 162 | 1.2 163 | 164 | 修复拖拽失效 165 | 166 | 1.1 167 | 168 | 点击监听问题处理 169 | 170 | 1.0 171 | 172 | 创建项目 173 | -------------------------------------------------------------------------------- /README_EN.md: -------------------------------------------------------------------------------- 1 | ![Logo](https://raw.githubusercontent.com/leotyndale/EnFloatingView/master/preview/logo.png) 2 | 3 | EnFloatingView 4 | ========================== 5 | [![Jcenter](https://img.shields.io/badge/Jcenter-v1.0-brightgreen.svg?style=flat)](https://bintray.com/leotyndale/Muxuan/EnFloatingView) 6 | [![Muxuan](https://img.shields.io/badge/PoweredBy-Muxuan-green.svg?style=flat)](http://www.imuxuan.com/) 7 | [![Website](https://img.shields.io/website-up-down-green-red/https/shields.io.svg?label=Blog)](http://blog.imuxuan.com) 8 | 9 | 10 | Do not need all permissions, not subject to a variety of domestic ROM restrictions, the default can be displayed within the application suspension window. 11 | 12 | ### Traditional way 13 | 14 | For the implementation of traditional suspension Windows and some ancient "black technology" suspension Windows, there must have been many mature cases, and the implementation strategies are basically the following two: 15 | 16 | 1. TYPE_SYSTEM_ALERT Type 17 | 18 | ```java 19 | mWindowManager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE); 20 | WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams() 21 | layoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT; 22 | ``` 23 | 24 | need permission: 25 | 26 | ```java 27 | 28 | ``` 29 | 30 | 2. TYPE_TOAST / TYPE_PHONE Type 31 | 32 | No permission statement is required below 7.1.1. It is hidden by default on models like meizu, huawei and xiaomi, and users need to be guided to open the suspension window. 33 | 34 | ### Traditional solutions 35 | 36 | The first scheme can not be adopted by many development because of various restrictions, so the more popular way to achieve the suspension window is the second one. 37 | 38 | But we have our own rules: 39 | 40 | - the above models cannot be accepted. The second way to achieve the suspension window still requires users to actively grant permission? 41 | - it is not acceptable to hide by default on models such as meizu, huawei and xiaomi. Users need to be guided to open the suspension window, like this 42 | 43 | ![权限管理](https://github.com/leotyndale/EnFloatingView/blob/master/preview/1.gif) 44 | 45 | ### function 46 | 47 | 48 | - the application is displayed without any permission 49 | - in-app display, all models can display the suspension window by default, without guiding users to do more Settings 50 | - support drag 51 | - move beyond screen limits 52 | - can be automatically attached to the edge of the screen 53 | 54 | ### Instructions 55 | 56 | 1.gralde dependencies 57 | 58 | ```java 59 | compile 'com.imuxuan:floatingview:1.0' 60 | ``` 61 | 62 | 63 | 2.Add the following code to onStart and onStop in the base class Activity (note that this must be the base class Activity) 64 | 65 | ```java 66 | @Override 67 | protected void onStart() { 68 | super.onStart(); 69 | FloatingView.get().attach(this); 70 | } 71 | 72 | @Override 73 | protected void onStop() { 74 | super.onStop(); 75 | FloatingView.get().detach(this); 76 | } 77 | ``` 78 | 79 | 80 | 3.Display 81 | 82 | ```java 83 | FloatingView.get().add(getApplicationContext()); 84 | ``` 85 | 86 | 4.Dismiss 87 | 88 | ```java 89 | FloatingView.get().remove(); 90 | ``` 91 | 92 | ### Rendering 93 | ![预览图](https://github.com/leotyndale/EnFloatingView/blob/master/preview/2.gif) 94 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 26 5 | defaultConfig { 6 | applicationId "com.imuxuan.en.floatingview" 7 | minSdkVersion 14 8 | targetSdkVersion 26 9 | versionCode 1 10 | versionName "1.0" 11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | } 20 | 21 | dependencies { 22 | implementation fileTree(dir: 'libs', include: ['*.jar']) 23 | implementation 'com.android.support:appcompat-v7:26.1.0' 24 | testImplementation 'junit:junit:4.12' 25 | androidTestImplementation 'com.android.support.test:runner:1.0.1' 26 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' 27 | implementation project(path: ':floatingview') 28 | } 29 | -------------------------------------------------------------------------------- /app/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 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/imuxuan/en/floatingview/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.imuxuan.en.floatingview; 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.imuxuan.en.floatingview", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 10 | 11 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /app/src/main/java/com/imuxuan/en/floatingview/BaseActivity.java: -------------------------------------------------------------------------------- 1 | package com.imuxuan.en.floatingview; 2 | 3 | import android.support.v7.app.AppCompatActivity; 4 | 5 | import com.imuxuan.floatingview.FloatingView; 6 | 7 | /** 8 | * @ClassName BaseActivity 9 | * @Description 10 | * @Author Yunpeng Li 11 | * @Creation 2018/3/15 下午5:01 12 | * @Mender Yunpeng Li 13 | * @Modification 2018/3/15 下午5:01 14 | */ 15 | public class BaseActivity extends AppCompatActivity { 16 | 17 | @Override 18 | protected void onStart() { 19 | super.onStart(); 20 | FloatingView.get().attach(this); 21 | } 22 | 23 | @Override 24 | protected void onStop() { 25 | super.onStop(); 26 | FloatingView.get().detach(this); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /app/src/main/java/com/imuxuan/en/floatingview/TestActivity.java: -------------------------------------------------------------------------------- 1 | package com.imuxuan.en.floatingview; 2 | 3 | import android.content.Intent; 4 | import android.net.Uri; 5 | import android.os.Bundle; 6 | import android.support.annotation.Nullable; 7 | import android.view.View; 8 | import android.widget.TextView; 9 | import android.widget.Toast; 10 | 11 | import com.imuxuan.floatingview.FloatingMagnetView; 12 | import com.imuxuan.floatingview.FloatingView; 13 | import com.imuxuan.floatingview.MagnetViewListener; 14 | 15 | /** 16 | * @ClassName TestActivity 17 | * @Description 18 | * @Author Yunpeng Li 19 | * @Creation 2018/3/15 下午5:01 20 | * @Mender Yunpeng Li 21 | * @Modification 2018/3/15 下午5:01 22 | */ 23 | public class TestActivity extends BaseActivity { 24 | static int pageNum = 1; 25 | 26 | @Override 27 | protected void onCreate(@Nullable Bundle savedInstanceState) { 28 | super.onCreate(savedInstanceState); 29 | setContentView(R.layout.main); 30 | ((TextView) findViewById(R.id.page_num)).setText("页面" + pageNum); 31 | FloatingView.get().add(); 32 | FloatingView.get().listener(new MagnetViewListener() { 33 | @Override 34 | public void onRemove(FloatingMagnetView magnetView) { 35 | Toast.makeText(TestActivity.this, "我没了", Toast.LENGTH_SHORT).show(); 36 | } 37 | 38 | @Override 39 | public void onClick(FloatingMagnetView magnetView) { 40 | Toast.makeText(TestActivity.this, "点到我了", Toast.LENGTH_SHORT).show(); 41 | Uri uri = Uri.parse("http://blog.imuxuan.com"); 42 | startActivity(new Intent(Intent.ACTION_VIEW, uri)); 43 | } 44 | }); 45 | } 46 | 47 | public void createActivity(View view) { 48 | pageNum++; 49 | Intent intent = new Intent(); 50 | intent.setClass(this, TestActivity.class); 51 | startActivity(intent); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/main/res/layout/main.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 |