├── .gitignore ├── .idea ├── gradle.xml ├── misc.xml ├── modules.xml ├── runConfigurations.xml └── vcs.xml ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── fenggit │ │ └── floatwindow │ │ ├── FloatManager.kt │ │ ├── FloatWindowService.kt │ │ ├── MainActivity.kt │ │ └── NextActivity.kt │ └── res │ ├── drawable-v24 │ └── ic_launcher_foreground.xml │ ├── drawable │ ├── float_view_bg.xml │ ├── fm_float_play.xml │ └── ic_launcher_background.xml │ ├── layout │ ├── activity_main.xml │ ├── activity_next.xml │ └── float_view.xml │ ├── mipmap-anydpi-v26 │ ├── ic_launcher.xml │ └── ic_launcher_round.xml │ ├── mipmap-xhdpi │ ├── default_record_album.png │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xxhdpi │ ├── ic_launcher.png │ ├── ic_launcher_round.png │ ├── icon_float_close.png │ ├── icon_float_next.png │ ├── icon_float_pause.png │ └── icon_float_play.png │ ├── mipmap-xxxhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ └── values │ ├── colors.xml │ ├── dimens.xml │ ├── strings.xml │ └── styles.xml ├── build.gradle ├── float_window.gif ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── library ├── .gitignore ├── build.gradle ├── consumer-rules.pro ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── fenggit │ │ └── floatwindow │ │ ├── FLog.java │ │ ├── FloatListener.java │ │ ├── FloatWindow.java │ │ ├── permission │ │ └── FloatPermission.java │ │ └── rom │ │ ├── HuaweiUtils.java │ │ ├── MeizuUtils.java │ │ ├── MiuiUtils.java │ │ ├── OppoUtils.java │ │ ├── QikuUtils.java │ │ └── RomUtils.java │ └── res │ └── values │ └── strings.xml └── settings.gradle /.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 | 5 | 21 | 22 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 15 | -------------------------------------------------------------------------------- /.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 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # FloatWindow 安卓任意界面悬浮窗 2 | 3 | ![效果演示](/float_window.gif) 4 | 5 | 6 | ```java 7 | minSdkVersion 19 8 | targetSdkVersion 29 9 | ``` 10 | 11 | 特性: 12 | === 13 | 14 | 1.支持桌面和App内悬浮,需要权限 15 | 16 | 2.支持仅仅在App内悬浮,不需要权限 17 | 18 | 3.支持自定义悬浮窗样式 19 | 20 | 4.支持拖动,提供自动贴边等动画 21 | 22 | 5.支持权限申请操作 23 | 24 | 25 | 集成: 26 | === 27 | 28 | 第 1 步、在工程的 build.gradle 中添加: 29 | 30 | ``` 31 | allprojects { 32 | repositories { 33 | ... 34 | maven { url 'https://jitpack.io' } 35 | } 36 | } 37 | ``` 38 | 第 2 步、在应用的 build.gradle 中添加: 39 | 40 | ``` 41 | dependencies { 42 | 43 | } 44 | ``` 45 | 46 | 使用: 47 | === 48 | 49 | **0.声明权限** 50 | 51 | ```java 52 | 53 | ``` 54 | 55 | 56 | **1.基础使用** 57 | 58 | ```kotlin 59 | // 自定义的View 60 | var view = LayoutInflater.from(context).inflate(R.layout.float_view, null) 61 | 62 | var float = FloatWindow.With(context, view) 63 | .setAutoAlign(true) //是否自动贴边 64 | .setModality(false) 65 | .setMoveAble(true) // 是否可拖动 66 | .setStartLocation(0, (getScreenHeight(context) * 0.7).toInt()) 67 | .create() 68 | 69 | ``` 70 | 71 | 72 | **2.TODO 指定界面显示** 73 | 74 | ```java 75 | .setFilter(true, A_Activity.class, C_Activity.class) 76 | 77 | ``` 78 | 此方法表示 A_Activity、C_Activity 显示悬浮窗,其他界面隐藏。 79 | 80 | ```java 81 | .setFilter(false, B_Activity.class) 82 | ``` 83 | 84 | **1.知识点** 85 | - app内悬浮窗使用的是:WindowManager.LayoutParams.TYPE_APPLICATION 86 | 87 | - 桌面悬浮使用的是:WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY 和 WindowManager.LayoutParams.TYPE_SYSTEM_ALERT 88 | 89 | 为什么app内悬浮窗使用的没有使用WindowManager.LayoutParams.TYPE_TOAST? 90 | 因为高版本不支持 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | apply plugin: 'kotlin-android-extensions' 3 | apply plugin: 'kotlin-android' 4 | 5 | android { 6 | compileSdkVersion 29 7 | defaultConfig { 8 | applicationId "com.fenggit.floatwindow" 9 | minSdkVersion 19 10 | targetSdkVersion 29 11 | versionCode 1 12 | versionName "1.0" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | implementation fileTree(dir: 'libs', include: ['*.jar']) 24 | implementation 'androidx.appcompat:appcompat:1.2.0' 25 | implementation "androidx.core:core-ktx:1.3.1" 26 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 27 | 28 | implementation project(':library') 29 | implementation 'androidx.constraintlayout:constraintlayout:2.0.1' 30 | } 31 | repositories { 32 | mavenCentral() 33 | } 34 | -------------------------------------------------------------------------------- /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/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /app/src/main/java/com/fenggit/floatwindow/FloatManager.kt: -------------------------------------------------------------------------------- 1 | package com.fenggit.floatwindow 2 | 3 | import android.content.Context 4 | import android.graphics.Point 5 | import android.view.LayoutInflater 6 | import android.view.View 7 | import android.view.WindowManager 8 | import android.widget.ImageView 9 | import android.widget.Toast 10 | 11 | /** 12 | * Author: felixhe 13 | * Date: 2019-12-06 14:53 14 | * Description: 15 | */ 16 | class FloatManager { 17 | var float: FloatWindow? = null 18 | 19 | fun initView(context: Context) { 20 | val view = LayoutInflater.from(context).inflate(R.layout.float_view, null) 21 | val close = view!!.findViewById(R.id.fm_float_close) 22 | val next = view.findViewById(R.id.fm_float_next) 23 | val play = view.findViewById(R.id.fm_float_play) 24 | 25 | close.setOnClickListener { 26 | float?.hidden() 27 | } 28 | 29 | next.setOnClickListener { 30 | Toast.makeText(context, "next", Toast.LENGTH_SHORT).show() 31 | } 32 | 33 | play.setOnClickListener { 34 | Toast.makeText(context, "play", Toast.LENGTH_SHORT).show() 35 | } 36 | 37 | float = FloatWindow.With(context, view) 38 | .setAutoAlign(true) // 是否自动贴边 39 | .setModality(false) // 是否模态窗口(事件是否可穿透当前窗口) 40 | .setMoveAble(true) // 是否可拖动 41 | .setStartLocation(0, (getScreenHeight(context) * 0.7).toInt()) //设置起始位置 42 | .create() 43 | } 44 | 45 | fun getFloatWindow(): FloatWindow? { 46 | return float 47 | } 48 | 49 | fun getScreenHeight(context: Context): Int { 50 | val sPoint = Point() 51 | val wm = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager 52 | wm.defaultDisplay.getSize(sPoint) 53 | return sPoint.y 54 | } 55 | } -------------------------------------------------------------------------------- /app/src/main/java/com/fenggit/floatwindow/FloatWindowService.kt: -------------------------------------------------------------------------------- 1 | package com.fenggit.floatwindow 2 | 3 | import android.app.Service 4 | import android.content.Context 5 | import android.content.Intent 6 | import android.graphics.Point 7 | import android.os.IBinder 8 | import android.util.Log 9 | import android.view.LayoutInflater 10 | import android.view.View 11 | import android.view.WindowManager 12 | import android.widget.ImageView 13 | import android.widget.Toast 14 | 15 | /** 16 | * Author: felixhe 17 | * Date: 2020/11/5 15:20 18 | * Description: 19 | */ 20 | class FloatWindowService : Service() { 21 | var mFloatWindow: FloatWindow? = null 22 | 23 | override fun onCreate() { 24 | super.onCreate() 25 | initView() 26 | Log.e("FloatWindowService", "onCreate:$this") 27 | } 28 | 29 | override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { 30 | Log.e("FloatWindowService", "onStartCommand:$this") 31 | initView() 32 | return super.onStartCommand(intent, flags, startId) 33 | } 34 | 35 | override fun onBind(intent: Intent?): IBinder? { 36 | return null 37 | } 38 | 39 | fun initView() { 40 | if (mFloatWindow == null) { 41 | val view = LayoutInflater.from(this).inflate(R.layout.float_view, null) 42 | val close = view!!.findViewById(R.id.fm_float_close) 43 | val next = view.findViewById(R.id.fm_float_next) 44 | val play = view.findViewById(R.id.fm_float_play) 45 | 46 | close.setOnClickListener { 47 | mFloatWindow?.hidden() 48 | } 49 | 50 | close.setOnClickListener { 51 | mFloatWindow?.hidden() 52 | } 53 | 54 | next.setOnClickListener { 55 | Toast.makeText(this, "next", Toast.LENGTH_SHORT).show() 56 | } 57 | 58 | play.setOnClickListener { 59 | Toast.makeText(this, "play", Toast.LENGTH_SHORT).show() 60 | } 61 | 62 | mFloatWindow = FloatWindow.With(this, view) 63 | .setAutoAlign(true) // 是否自动贴边 64 | .setModality(false) // 是否模态窗口(事件是否可穿透当前窗口) 65 | .setMoveAble(true) // 是否可拖动 66 | .setStartLocation(0, (getScreenHeight(this) * 0.7).toInt()) //设置起始位置 67 | .create() 68 | } 69 | 70 | if (mFloatWindow?.isShowing == false) { 71 | mFloatWindow?.show() 72 | } 73 | } 74 | 75 | fun getScreenHeight(context: Context): Int { 76 | val sPoint = Point() 77 | val wm = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager 78 | wm.defaultDisplay.getSize(sPoint) 79 | return sPoint.y 80 | } 81 | } -------------------------------------------------------------------------------- /app/src/main/java/com/fenggit/floatwindow/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package com.fenggit.floatwindow 2 | 3 | import android.content.Intent 4 | import android.os.Bundle 5 | import android.widget.Toast 6 | import androidx.appcompat.app.AlertDialog 7 | import androidx.appcompat.app.AppCompatActivity 8 | import com.fenggit.floatwindow.permission.FloatPermission 9 | import kotlinx.android.synthetic.main.activity_main.* 10 | 11 | class MainActivity : AppCompatActivity() { 12 | lateinit var mFloatManager: FloatManager 13 | lateinit var mFloatPermission: FloatPermission 14 | 15 | var mFloatWindow: FloatWindow? = null 16 | 17 | override fun onCreate(savedInstanceState: Bundle?) { 18 | super.onCreate(savedInstanceState) 19 | setContentView(R.layout.activity_main) 20 | 21 | mFloatManager = FloatManager() 22 | mFloatManager.initView(this) 23 | mFloatWindow = mFloatManager.getFloatWindow() 24 | 25 | mFloatPermission = FloatPermission() 26 | 27 | mFloatWindow?.setFloatListener { 28 | Toast.makeText(this, "click outside", Toast.LENGTH_SHORT).show() 29 | } 30 | 31 | btn_show.setOnClickListener { 32 | // mFloatWindow?.show() 33 | 34 | startService(Intent(this@MainActivity, FloatWindowService::class.java)) 35 | 36 | // if (mFloatPermission.isHavePermission(this)) { 37 | // mFloatWindow?.show() 38 | // } else { 39 | // AlertDialog.Builder(this) 40 | // .setTitle("授权") 41 | // .setMessage("显示悬浮窗,需要开启悬浮窗权限") 42 | // .setPositiveButton("去授权") { dialog, which -> 43 | // mFloatPermission.gotoPermission(this@MainActivity) 44 | // 45 | // }.setNegativeButton("取消") { dialog, which -> 46 | // dialog.dismiss() 47 | // }.show() 48 | // } 49 | } 50 | 51 | btn_hidden.setOnClickListener { 52 | mFloatWindow?.hidden() 53 | } 54 | 55 | btn_next.setOnClickListener { 56 | startActivity(Intent(this, NextActivity::class.java)) 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /app/src/main/java/com/fenggit/floatwindow/NextActivity.kt: -------------------------------------------------------------------------------- 1 | package com.fenggit.floatwindow 2 | 3 | import androidx.appcompat.app.AppCompatActivity 4 | import android.os.Bundle 5 | 6 | class NextActivity : AppCompatActivity() { 7 | override fun onCreate(savedInstanceState: Bundle?) { 8 | super.onCreate(savedInstanceState) 9 | setContentView(R.layout.activity_next) 10 | } 11 | } -------------------------------------------------------------------------------- /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/float_view_bg.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | 12 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/fm_float_play.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 9 | -------------------------------------------------------------------------------- /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/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 |