Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/getactivity/easywindow

Android 悬浮窗框架,好用不解释
https://github.com/getactivity/easywindow

android android-toast dialog easyfloat float floating floating-window floatwindow popup popupwindow toast toastutils window-manager windowmanager xtoast

Last synced: 6 days ago
JSON representation

Android 悬浮窗框架,好用不解释

Awesome Lists containing this project

README

        

# 悬浮窗框架

* 项目地址:[Github](https://github.com/getActivity/EasyWindow)

* 博客地址:[悬浮窗需求终结者](https://www.jianshu.com/p/247d705b87b6)

* 可以扫码下载 Demo 进行演示或者测试,如果扫码下载不了的,[点击此处可直接下载](https://github.com/getActivity/EasyWindow/releases/download/10.62/EasyWindow.apk)

![](picture/demo_code.png)

![](picture/dynamic_figure.gif)

#### 本框架意在解决一些悬浮窗的需求,如果是普通的 Toast 封装推荐使用 [Toaster](https://github.com/getActivity/Toaster)

#### 集成步骤

* 如果你的项目 Gradle 配置是在 `7.0 以下`,需要在 `build.gradle` 文件中加入

```groovy
allprojects {
repositories {
// JitPack 远程仓库:https://jitpack.io
maven { url 'https://jitpack.io' }
}
}
```

* 如果你的 Gradle 配置是 `7.0 及以上`,则需要在 `settings.gradle` 文件中加入

```groovy
dependencyResolutionManagement {
repositories {
// JitPack 远程仓库:https://jitpack.io
maven { url 'https://jitpack.io' }
}
}
```

* 配置完远程仓库后,在项目 app 模块下的 `build.gradle` 文件中加入远程依赖

```groovy
android {
// 支持 JDK 1.8
compileOptions {
targetCompatibility JavaVersion.VERSION_1_8
sourceCompatibility JavaVersion.VERSION_1_8
}
}

dependencies {
// 悬浮窗框架:https://github.com/getActivity/EasyWindow
implementation 'com.github.getActivity:EasyWindow:10.62'
}
```

#### 使用案例

* Java 用法

```java
// 传入 Activity 对象表示设置成局部的,不需要有悬浮窗权限
// 传入 Application 对象表示设置成全局的,但需要有悬浮窗权限
EasyWindow.with(this)
.setContentView(R.layout.toast_hint)
// 设置成可拖拽的
//.setDraggable()
// 设置显示时长
.setDuration(1000)
// 设置动画样式
//.setAnimStyle(android.R.style.Animation_Translucent)
// 设置外层是否能被触摸
//.setOutsideTouchable(false)
// 设置窗口背景阴影强度
//.setBackgroundDimAmount(0.5f)
.setImageDrawable(android.R.id.icon, R.mipmap.ic_dialog_tip_finish)
.setText(android.R.id.message, "点我消失")
.setOnClickListener(android.R.id.message, new EasyWindow.OnClickListener() {

@Override
public void onClick(EasyWindow> easyWindow, TextView view) {
// 点击这个 View 后消失
easyWindow.cancel();
// 跳转到某个Activity
// easyWindow.startActivity(intent);
}
})
.show();
```

* Kotlin 用法(二选一)

```kotlin
EasyWindow.with(activity).apply {
setContentView(R.layout.toast_hint)
// 设置成可拖拽的
//setDraggable()
// 设置显示时长
setDuration(1000)
// 设置动画样式
//setAnimStyle(android.R.style.Animation_Translucent)
// 设置外层是否能被触摸
//setOutsideTouchable(false)
// 设置窗口背景阴影强度
//setBackgroundDimAmount(0.5f)
setImageDrawable(android.R.id.icon, R.mipmap.ic_dialog_tip_finish)
setText(android.R.id.message, "点我消失")
setOnClickListener(android.R.id.message, EasyWindow.OnClickListener { easyWindow: EasyWindow<*>, view: TextView? ->
// 点击这个 View 后消失
easyWindow.cancel()
// 跳转到某个Activity
// easyWindow.startActivity(intent);
})
}.show()
```

```kotlin
EasyWindow.with(activity)
.setContentView(R.layout.toast_hint)
// 设置成可拖拽的
//.setDraggable()
// 设置显示时长
.setDuration(1000)
// 设置动画样式
//.setAnimStyle(android.R.style.Animation_Translucent)
// 设置外层是否能被触摸
//.setOutsideTouchable(false)
// 设置窗口背景阴影强度
//.setBackgroundDimAmount(0.5f)
.setImageDrawable(android.R.id.icon, R.mipmap.ic_dialog_tip_finish)
.setText(android.R.id.message, "点我消失")
.setOnClickListener(android.R.id.message, EasyWindow.OnClickListener { easyWindow: EasyWindow<*>, view: TextView? ->
// 点击这个 View 后消失
easyWindow.cancel()
// 跳转到某个Activity
// easyWindow.startActivity(intent);
})
.show()
```

#### 没有悬浮窗权限如何全局显示?

* 没有悬浮窗权限是不能全局显示在其他应用上的,但是全局显示在自己的应用上是可以实现的

* 但是当前 Activity 创建的悬浮窗只能在当前 Activity 上面显示,如果想在所有的 Activity 都显示需要做特殊处理

* 我们可以通过 Application 来监听所有 Activity 的生命周期方法,然后在每个 Activity.onCreate 时创建悬浮窗

```java
public final class ActivityWindowLifecycle implements Application.ActivityLifecycleCallbacks {

static void with(Application application) {
application.registerActivityLifecycleCallbacks(new FloatingLifecycle());
}

@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
EasyWindow.with(activity)
.setView(R.layout.xxx)
.show();
}

@Override
public void onActivityStarted(Activity activity) {}

@Override
public void onActivityResumed(Activity activity) {}

@Override
public void onActivityPaused(Activity activity) {}

@Override
public void onActivityStopped(Activity activity) {}

@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {}

@Override
public void onActivityDestroyed(Activity activity) {}
}
```

#### 框架的 API 介绍

* 对象方法

```java
// 设置悬浮窗宽度
setWidth(int width)
// 设置悬浮窗高度
setHeight(int height)

// 设置悬浮窗显示的重心
setGravity(int gravity)
// 设置水平偏移量
setXOffset(int px)
// 设置垂直偏移量
setYOffset(int px)

// 设置悬浮窗外层是否可触摸
setOutsideTouchable(boolean touchable)
// 设置悬浮窗背景阴影强度
setBackgroundDimAmount(float amount)

// 添加窗口标记
addWindowFlags(int flags)
// 移除窗口标记
removeWindowFlags(int flags)
// 设置窗口标记
setWindowFlags(int flags)
// 是否存在某个窗口标记
hasWindowFlags(int flags)

// 设置悬浮窗的显示类型
setWindowType(int type)

// 几乎涵盖了所有 WindowManager 的参数
// 更多 API 方法请查看 EasyWindow 类的源码
......
```

* 静态方法

```java
// 取消所有正在显示的悬浮窗
EasyWindow.cancelAll()

// 取消特定类名的悬浮窗
EasyWindow.cancelByClass(Class extends EasyWindow>> clazz)

// 取消特定标记的悬浮窗
EasyWindow.cancelByTag(String tag)

// 回收所有正在显示的悬浮窗
EasyWindow.recycleAll()

// 回收特定类名的悬浮窗
EasyWindow.recycleByClass(Class extends EasyWindow>> clazz)

// 回收特定标记的悬浮窗
EasyWindow.recycleByTag(String tag)

// 判断当前是否有悬浮窗正在显示
EasyWindow.existShowing()

// 判断当前是否有特定类名的悬浮窗正在显示
EasyWindow.existShowingByClass(Class extends EasyWindow>> clazz)

// 判断当前是否有特定标记的悬浮窗正在显示
EasyWindow.existShowingByTag(String tag)
```

#### 框架混淆规则

* 在混淆规则文件 `proguard-rules.pro` 中加入

```text
-keep class com.hjq.window.** {*;}
```

#### 作者的其他开源项目

* 安卓技术中台:[AndroidProject](https://github.com/getActivity/AndroidProject) ![](https://img.shields.io/github/stars/getActivity/AndroidProject.svg) ![](https://img.shields.io/github/forks/getActivity/AndroidProject.svg)

* 安卓技术中台 Kt 版:[AndroidProject-Kotlin](https://github.com/getActivity/AndroidProject-Kotlin) ![](https://img.shields.io/github/stars/getActivity/AndroidProject-Kotlin.svg) ![](https://img.shields.io/github/forks/getActivity/AndroidProject-Kotlin.svg)

* 权限框架:[XXPermissions](https://github.com/getActivity/XXPermissions) ![](https://img.shields.io/github/stars/getActivity/XXPermissions.svg) ![](https://img.shields.io/github/forks/getActivity/XXPermissions.svg)

* 吐司框架:[Toaster](https://github.com/getActivity/Toaster) ![](https://img.shields.io/github/stars/getActivity/Toaster.svg) ![](https://img.shields.io/github/forks/getActivity/Toaster.svg)

* 网络框架:[EasyHttp](https://github.com/getActivity/EasyHttp) ![](https://img.shields.io/github/stars/getActivity/EasyHttp.svg) ![](https://img.shields.io/github/forks/getActivity/EasyHttp.svg)

* 标题栏框架:[TitleBar](https://github.com/getActivity/TitleBar) ![](https://img.shields.io/github/stars/getActivity/TitleBar.svg) ![](https://img.shields.io/github/forks/getActivity/TitleBar.svg)

* ShapeView 框架:[ShapeView](https://github.com/getActivity/ShapeView) ![](https://img.shields.io/github/stars/getActivity/ShapeView.svg) ![](https://img.shields.io/github/forks/getActivity/ShapeView.svg)

* ShapeDrawable 框架:[ShapeDrawable](https://github.com/getActivity/ShapeDrawable) ![](https://img.shields.io/github/stars/getActivity/ShapeDrawable.svg) ![](https://img.shields.io/github/forks/getActivity/ShapeDrawable.svg)

* 语种切换框架:[MultiLanguages](https://github.com/getActivity/MultiLanguages) ![](https://img.shields.io/github/stars/getActivity/MultiLanguages.svg) ![](https://img.shields.io/github/forks/getActivity/MultiLanguages.svg)

* Gson 解析容错:[GsonFactory](https://github.com/getActivity/GsonFactory) ![](https://img.shields.io/github/stars/getActivity/GsonFactory.svg) ![](https://img.shields.io/github/forks/getActivity/GsonFactory.svg)

* 日志查看框架:[Logcat](https://github.com/getActivity/Logcat) ![](https://img.shields.io/github/stars/getActivity/Logcat.svg) ![](https://img.shields.io/github/forks/getActivity/Logcat.svg)

* 嵌套滚动布局框架:[NestedScrollLayout](https://github.com/getActivity/NestedScrollLayout) ![](https://img.shields.io/github/stars/getActivity/NestedScrollLayout.svg) ![](https://img.shields.io/github/forks/getActivity/NestedScrollLayout.svg)

* Android 版本适配:[AndroidVersionAdapter](https://github.com/getActivity/AndroidVersionAdapter) ![](https://img.shields.io/github/stars/getActivity/AndroidVersionAdapter.svg) ![](https://img.shields.io/github/forks/getActivity/AndroidVersionAdapter.svg)

* Android 代码规范:[AndroidCodeStandard](https://github.com/getActivity/AndroidCodeStandard) ![](https://img.shields.io/github/stars/getActivity/AndroidCodeStandard.svg) ![](https://img.shields.io/github/forks/getActivity/AndroidCodeStandard.svg)

* Android 资源大汇总:[AndroidIndex](https://github.com/getActivity/AndroidIndex) ![](https://img.shields.io/github/stars/getActivity/AndroidIndex.svg) ![](https://img.shields.io/github/forks/getActivity/AndroidIndex.svg)

* Android 开源排行榜:[AndroidGithubBoss](https://github.com/getActivity/AndroidGithubBoss) ![](https://img.shields.io/github/stars/getActivity/AndroidGithubBoss.svg) ![](https://img.shields.io/github/forks/getActivity/AndroidGithubBoss.svg)

* Studio 精品插件:[StudioPlugins](https://github.com/getActivity/StudioPlugins) ![](https://img.shields.io/github/stars/getActivity/StudioPlugins.svg) ![](https://img.shields.io/github/forks/getActivity/StudioPlugins.svg)

* 表情包大集合:[EmojiPackage](https://github.com/getActivity/EmojiPackage) ![](https://img.shields.io/github/stars/getActivity/EmojiPackage.svg) ![](https://img.shields.io/github/forks/getActivity/EmojiPackage.svg)

* AI 资源大汇总:[AiIndex](https://github.com/getActivity/AiIndex) ![](https://img.shields.io/github/stars/getActivity/AiIndex.svg) ![](https://img.shields.io/github/forks/getActivity/AiIndex.svg)

* 省市区 Json 数据:[ProvinceJson](https://github.com/getActivity/ProvinceJson) ![](https://img.shields.io/github/stars/getActivity/ProvinceJson.svg) ![](https://img.shields.io/github/forks/getActivity/ProvinceJson.svg)

* Markdown 语法文档:[MarkdownDoc](https://github.com/getActivity/MarkdownDoc) ![](https://img.shields.io/github/stars/getActivity/MarkdownDoc.svg) ![](https://img.shields.io/github/forks/getActivity/MarkdownDoc.svg)

#### 微信公众号:Android轮子哥

![](https://raw.githubusercontent.com/getActivity/Donate/master/picture/official_ccount.png)

#### Android 技术 Q 群:10047167

#### 如果您觉得我的开源库帮你节省了大量的开发时间,请扫描下方的二维码随意打赏,要是能打赏个 10.24 :monkey_face:就太:thumbsup:了。您的支持将鼓励我继续创作:octocat:([点击查看捐赠列表](https://github.com/getActivity/Donate))

![](https://raw.githubusercontent.com/getActivity/Donate/master/picture/pay_ali.png) ![](https://raw.githubusercontent.com/getActivity/Donate/master/picture/pay_wechat.png)

## License

```text
Copyright 2019 Huang JinQun

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
```