本框架意在解决一些悬浮窗的需求,如果是普通的 Toast 封装推荐使用 Toaster
- 如果你的项目 Gradle 配置是在
7.0 以下,需要在build.gradle文件中加入
allprojects {
repositories {
// JitPack 远程仓库:https://jitpack.io
maven { url 'https://jitpack.io' }
}
}- 如果你的 Gradle 配置是
7.0 及以上,则需要在settings.gradle文件中加入
dependencyResolutionManagement {
repositories {
// JitPack 远程仓库:https://jitpack.io
maven { url 'https://jitpack.io' }
}
}- 配置完远程仓库后,在项目 app 模块下的
build.gradle文件中加入远程依赖
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:15.8'
}- 方案一:沿用旧版本框架的远程依赖
dependencies {
// 悬浮窗框架:https://github.com/getActivity/EasyWindow
implementation 'com.github.getActivity:EasyWindow:13.5'
}
方案二:如果你的项目仍处于 Support 阶段,目前不方便转到 AndroidX 中来,但又想用最新版本的框架,可以使用 Google 提供的 JetifierStandalone 工具将已发布版本 Release 中的 aar 包通过反向模式转成 Support 版本的 aar 包来使用。
上述两种方案任选其一即可,但是仍旧不推荐你那样做,因为这些只是权宜之计,并非长久之计,框架后续的版本已不再支持 Support 项目,最好的方案是将项目迁移到 AndroidX。
将项目从 Support 迁移 AndroidX 相关的教程:AndroidX 踩坑指南
- Java 用法
// 传入 Activity 对象表示设置成局部的,不需要有悬浮窗权限// 传入 Application 对象表示设置成全局的,但需要有悬浮窗权限EasyWindow.with(this)
.setContentView(R.layout.toast_hint)
// 设置成可拖拽的//.setWindowDraggableRule()// 设置显示时长
.setWindowDuration(1000)
// 设置动画样式//.setWindowAnim(android.R.style.Animation_Translucent)// 设置外层是否能被触摸//.setOutsideTouchable(false)// 设置窗口背景阴影强度//.setBackgroundDimAmount(0.5f)
.setImageDrawableByImageView(android.R.id.icon, R.mipmap.ic_dialog_tip_finish)
.setTextByTextView(android.R.id.message, "点我消失")
.setOnClickListenerByView(android.R.id.message, newOnWindowViewClickListener<TextView>() {
@OverridepublicvoidonClick(@NonNullEasyWindow<?> easyWindow, @NonNullTextViewview) {
easyWindow.cancel();
// 跳转到某个Activity// easyWindow.startActivity(intent);
}
})
.show();- Kotlin 用法(二选一)
EasyWindow.with(activity).apply {
setContentView(R.layout.toast_hint)
// 设置成可拖拽的//setWindowDraggableRule()// 设置显示时长
setWindowDuration(1000)
// 设置动画样式//setWindowAnim(android.R.style.Animation_Translucent)// 设置外层是否能被触摸//setOutsideTouchable(false)// 设置窗口背景阴影强度//setBackgroundDimAmount(0.5f)
setImageDrawableByImageView(android.R.id.icon, R.mipmap.ic_dialog_tip_finish)
setTextByTextView(android.R.id.message, "点我消失")
setOnClickListenerByView(android.R.id.message, OnWindowViewClickListener<TextView?> { easyWindow:EasyWindow<*>, view:TextView->
easyWindow.cancel()
// 跳转到某个Activity// easyWindow.startActivity(intent)
})
}.show()EasyWindow.with(activity)
.setContentView(R.layout.toast_hint)
// 设置成可拖拽的//.setWindowDraggableRule()// 设置显示时长
.setWindowDuration(1000)
// 设置动画样式//.setWindowAnim(android.R.style.Animation_Translucent)// 设置外层是否能被触摸//.setOutsideTouchable(false)// 设置窗口背景阴影强度//.setBackgroundDimAmount(0.5f)
.setImageDrawableByImageView(android.R.id.icon, R.mipmap.ic_dialog_tip_finish)
.setTextByTextView(android.R.id.message, "点我消失")
.setOnClickListenerByView(android.R.id.message, OnWindowViewClickListener<TextView?> { easyWindow:EasyWindow<*>, view:TextView->
easyWindow.cancel()
// 跳转到某个Activity// easyWindow.startActivity(intent)
})
.show()没有悬浮窗权限是不能全局显示在其他应用上的,但是全局显示在自己的应用上是可以实现的
但是当前 Activity 创建的悬浮窗只能在当前 Activity 上面显示,如果想在所有的 Activity 都显示需要做特殊处理
我们可以通过 Application 来监听所有 Activity 的生命周期方法,然后在每个 Activity.onCreate 时创建悬浮窗
publicfinalclassWindowLifecycleControlimplementsApplication.ActivityLifecycleCallbacks {
staticvoidwith(Applicationapplication) {
application.registerActivityLifecycleCallbacks(newFloatingLifecycle());
}
@OverridepublicvoidonActivityCreated(Activityactivity, BundlesavedInstanceState) {
EasyWindow.with(activity)
.setContentView(R.layout.xxx)
.show();
}
......
}- 对象方法
// 显示悬浮窗easyWindow.show();
// 延迟显示悬浮窗(可在子线程中调用,不怕频繁调用)easyWindow.delayShow();
easyWindow.delayShow(longdelayMillis);
// 将悬浮窗显示在指定 View 的旁边easyWindow.showAsDropDown(@NonNullViewanchorView, int showGravity, int xOff, intyOff);
// 取消显示悬浮窗easyWindow.cancel();
// 延迟取消显示悬浮窗(可在子线程中调用,不怕频繁调用)easyWindow.delayCancel();
easyWindow.delayCancel(longdelayMillis);
// 取消显示并回收悬浮窗easyWindow.recycle();
// 延迟回收悬浮窗(可在子线程中调用,不怕频繁调用)easyWindow.delayRecycle();
easyWindow.delayRecycle(longdelayMillis);
// 更新悬浮窗(在更新了悬浮窗参数才需要调用)easyWindow.update();
// 延迟更新悬浮窗(可在子线程中调用,不怕频繁调用)easyWindow.delayUpdate();
easyWindow.delayUpdate(longdelayMillis);
// 当前悬浮窗是否正在显示easyWindow.isShowing();
// 设置窗口生命周期回调监听easyWindow.setOnWindowLifecycleCallback(@NullableOnWindowLifecycleCallbackcallback);
// 设置窗口屏幕旋转回调监听easyWindow.setOnWindowScreenRotationCallback(@NullableOnWindowScreenRotationCallbackcallback);
// 设置悬浮窗拖拽规则(框架内部提供了两种拖拽规则,MovingWindowDraggableRule 和 SpringBackWindowDraggableRule )easyWindow.setWindowDraggableRule(@NullableIWindowDraggableRuledraggableRule);
// 设置悬浮窗拖拽规则(可能为空)easyWindow.getWindowDraggableRule();
// 设置悬浮窗内容布局easyWindow.setContentView(@LayoutResintlayoutId);
easyWindow.setContentView(@LayoutResintlayoutId, @NullableOnWindowLayoutInflateListenerlistener);
easyWindow.setContentView(@NonNullViewview);
// 获取内容布局(可能为空)easyWindow.getContentView();
// 限定悬浮窗显示时长easyWindow.setWindowDuration(@IntRange(from = 0) int delayMillis);
// 设置悬浮窗 tageasyWindow.setWindowTag(@NullableStringtag);
// 获取悬浮窗 tageasyWindow.getWindowTag();
// 设置悬浮窗宽度和高度easyWindow.setWindowSize(intwidth, intheight);
// 设置悬浮窗大小(按照屏幕百分比)easyWindow.setWindowSizePercent(@FloatRange(from = 0, to = 1) floatwidthPercent, @FloatRange(from = 0, to = 1) float heightPercent);
// 设置悬浮窗的位置easyWindow.setWindowLocation(@Pxintx, @Px int y);
easyWindow.setWindowLocation(@GravityFlagintgravity, @Px int x, @Px int y);
// 设置窗口位置(偏移量按照屏幕百分比)easyWindow.setWindowLocationPercent(@FloatRange(from = 0, to = 1) @Px float horizontalPercent, @FloatRange(from = 0, to = 1) @Px float verticalPercent);
easyWindow.setWindowLocationPercent(@GravityFlag int gravity, @FloatRange(from = 0, to = 1) @Px float horizontalPercent, @FloatRange(from = 0, to = 1) @Px float verticalPercent);
// 设置悬浮窗外层是否可触摸easyWindow.setOutsideTouchable(booleantouchable);
// 设置悬浮窗背景阴影强度easyWindow.setBackgroundDimAmount(@FloatRange(from = 0.0, to = 1.0) float amount);
// 添加窗口标记easyWindow.addWindowFlags(int flags);
// 移除窗口标记easyWindow.removeWindowFlags(int flags);
// 设置窗口标记easyWindow.setWindowFlags(int flags);
// 是否存在某个窗口标记easyWindow.hasWindowFlags(int flags);
// 设置悬浮窗的显示类型easyWindow.setWindowType(int type);
// 设置悬浮窗动画样式easyWindow.setWindowAnim(int id);
// 设置悬浮窗软键盘模式easyWindow.setSoftInputMode(int softInputMode);
// 设置悬浮窗 TokeneasyWindow.setWindowToken(@NullableIBindertoken);
// 设置悬浮窗透明度easyWindow.setWindowAlpha(@FloatRange(from = 0.0, to = 1.0) float alpha);
// 设置容器和窗口小部件之间的垂直边距easyWindow.setVerticalMargin(float verticalMargin);
// 设置容器和窗口小部件之间的水平边距easyWindow.setHorizontalMargin(float horizontalMargin);
// 设置位图格式easyWindow.setBitmapFormat(int format);
// 设置状态栏的可见性easyWindow.setSystemUiVisibility(int systemUiVisibility);
// 设置垂直权重easyWindow.setVerticalWeight(float verticalWeight);
// 设置悬浮窗标题easyWindow.setWindowTitle(@NullableCharSequencetitle);
// 设置屏幕的亮度easyWindow.setScreenBrightness(@FloatRange(from = -1.0, to = 1.0) float screenBrightness);
// 设置键盘背光的亮度easyWindow.setButtonBrightness(@FloatRange(from = -1.0, to = 1.0) float buttonBrightness);
// 设置悬浮窗屏幕方向easyWindow.setScreenOrientation(int screenOrientation);
// 设置窗口旋转动画(Android 4.3 及以上才支持)easyWindow.setRotationAnimation(int rotationAnimation);
// 设置悬浮窗的刷新率(Android 5.0 及以上才支持)easyWindow.setPreferredRefreshRate(float preferredRefreshRate);
// 设置悬浮窗在哪个显示屏上显示(Android 6.0 及以上才支持)easyWindow.setPreferredDisplayModeId(int preferredDisplayModeId);
// 设置悬浮窗的颜色模式(Android 8.0 及以上才支持)easyWindow.setColorMode(int colorMode);
// 设置挖孔屏下的显示模式(Android 9.0 及以上才支持)easyWindow.setLayoutInDisplayCutoutMode(int layoutInDisplayCutoutMode);
// 设置悬浮窗画面低延迟开关(Android 11 及以上才支持)easyWindow.setPreferMinimalPostProcessing(booleanenabled);
// 设置悬浮窗背后的高斯模糊半径大小(Android 12 及以上才支持)easyWindow.setBlurBehindRadius(@IntRange(from = 0) int blurBehindRadius);
// 设置悬浮窗是否可以播放移动动画(Android 14 及以上才支持,框架兼容到 Android 4.3)easyWindow.setCanPlayMoveAnimation(booleanenable);
// 设置悬浮窗可见性easyWindow.setWindowViewVisibility(int visibility);
// 获取悬浮窗可见性easyWindow.getWindowViewVisibility();
// 设置悬浮窗根布局(一般情况下推荐使用 {@link #setContentView} 方法来填充布局)easyWindow.setRootLayout(@NonNullViewGroupviewGroup);
// 重新设置 WindowManager 参数集easyWindow.setWindowParams(@NonNullWindowManager.LayoutParamsparams);
// 重新设置 WindowManager 对象easyWindow.setWindowManager(@NonNullWindowManagerwindowManager);
// 获取当前窗口视图宽度easyWindow.getWindowViewWidth();
// 获取当前窗口视图高度easyWindow.getWindowViewHeight();
// 设置可见性状态给 VieweasyWindow.setVisibilityByView(@IdRes int viewId, int visibility);
// 设置背景 Drawable 给 VieweasyWindow.setBackgroundDrawableByView(@IdRes int viewId, @DrawableRes int drawableId);
easyWindow.setBackgroundDrawableByView(@IdRes int viewId, @NullableDrawabledrawable);
// 设置文本给 TextVieweasyWindow.setTextByTextView(@IdRes int viewId, @StringRes int stringId);
easyWindow.setTextByTextView(@IdRes int viewId, @NullableCharSequencetext);
// 设置字体颜色给 TextVieweasyWindow.setTextColorByTextView(@IdRes int viewId, @ColorInt int colorValue);
// 设置字体大小给 TextVieweasyWindow.setTextSizeByTextView(@IdRes int viewId, float textSize);
easyWindow.setTextSizeByTextView(@IdRes int viewId, int unit, float textSize);
// 设置提示文本给 TextVieweasyWindow.setHintTextByTextView(@IdRes int viewId, @StringRes int stringId);
easyWindow.setHintTextByTextView(@IdRes int viewId, @NullableCharSequencetext);
// 设置提示文本颜色给 TextVieweasyWindow.setHintTextColorByTextView(@IdRes int viewId, @ColorInt int colorValue);
// 设置图片 Drawable 给 ImageVieweasyWindow.setImageDrawableByImageView(@IdRes int viewId, @DrawableRes int drawableId);
easyWindow.setImageDrawableByImageView(@IdRes int viewId, @NullableDrawabledrawable);
// 设置点击监听(没有传入 View 的方法默认使用窗口根布局)easyWindow.setOnClickListener(@NullableOnWindowViewClickListener<? extendsView> listener);
easyWindow.setOnClickListenerByView(@IdRes int id, @NullableOnWindowViewClickListener<? extendsView> listener);
// 设置长按监听(没有传入 View 的方法默认使用窗口根布局)easyWindow.setOnLongClickListener(@NullableOnWindowViewLongClickListener<? extendsView> listener);
easyWindow.setOnLongClickListenerByView(@IdRes int id, @NullableOnWindowViewLongClickListener<? extendsView> listener);
// 设置触摸监听(没有传入 View 的方法默认使用窗口根布局)easyWindow.setOnTouchListener(@NullableOnWindowViewTouchListener<? extendsView> listener);
easyWindow.setOnTouchListenerByView(@IdRes int id, @NullableOnWindowViewTouchListener<? extendsView> listener);
// 设置按键监听(没有传入 View 的方法默认使用窗口根布局)easyWindow.setOnKeyListener(@NullableOnWindowViewKeyListener<? extendsView> listener);
easyWindow.setOnKeyListenerByView(@IdRes int id, @NullableOnWindowViewKeyListener<? extendsView> listener);
// 跳转 ActivityeasyWindow.startActivity(@NullableClass<? extendsActivity> clazz);
easyWindow.startActivity(@NullableIntentintent);
// 延迟执行任务easyWindow.sendTask(Runnablerunnable);
// 延迟一段时间执行任务easyWindow.sendTask(@NonNullRunnablerunnable, long delayMillis);
// 移除指定的任务easyWindow.cancelTask(@NonNullRunnablerunnable);
// 移除所有的任务easyWindow.cancelAllTask();- 静态方法
// 取消所有正在显示的悬浮窗EasyWindowManager.cancelAllWindow();
// 取消特定类名的悬浮窗EasyWindowManager.cancelWindowByClass(@NullableClass<? extendsEasyWindow<?>> clazz);
// 取消特定标记的悬浮窗EasyWindowManager.cancelWindowByTag(@NullableStringtag);
// 显示所有已取消但未回收的悬浮窗EasyWindowManager.showAllWindow();
// 显示特定类名已取消但未回收的悬浮窗EasyWindowManager.showWindowByClass(@NullableClass<? extendsEasyWindow<?>> clazz);
// 显示特定标记已取消但未回收的悬浮窗EasyWindowManager.showWindowByTag(@NullableStringtag);
// 回收所有正在显示的悬浮窗EasyWindowManager.recycleAllWindow();
// 回收特定类名的悬浮窗EasyWindowManager.recycleWindowByClass(@NullableClass<? extendsEasyWindow<?>> clazz);
// 回收特定标记的悬浮窗EasyWindowManager.recycleWindowByTag(@NullableStringtag);
// 判断当前是否有悬浮窗正在显示EasyWindowManager.existAnyWindowShowing();
// 判断当前是否有特定类名的悬浮窗正在显示EasyWindowManager.existWindowShowingByClass(@NullableClass<? extendsEasyWindow<?>> clazz);
// 判断当前是否有特定标记的悬浮窗正在显示EasyWindowManager.existWindowShowingByTag(@NullableStringtag);
// 寻找特定类名的悬浮窗EasyWindowManager.findWindowInstancesByClass(@NullableClass<? extendsEasyWindow<?>> clazz);
// 寻找特定标记的悬浮窗EasyWindowManager.findWindowInstancesByTag(@NullableStringtag);
// 获取所有的悬浮窗EasyWindowManager.getAllWindowInstances();安卓技术中台:AndroidProject
安卓技术中台 Kt 版:AndroidProject-Kotlin
权限框架:XXPermissions
吐司框架:Toaster
网络框架:EasyHttp
标题栏框架:TitleBar
设备兼容框架:DeviceCompat
ShapeView 框架:ShapeView
ShapeDrawable 框架:ShapeDrawable
语种切换框架:MultiLanguages
Gson 解析容错:GsonFactory
日志查看框架:Logcat
嵌套滚动布局框架:NestedScrollLayout
Android 命令行工具集:AndroidCmdTools
Android 版本适配:AndroidVersionAdapter
Android 代码规范:AndroidCodeStandard
Android 资源大汇总:AndroidIndex
Android 开源排行榜:AndroidGithubBoss
Studio 精品插件:StudioPlugins
表情包大集合:EmojiPackage
AI 资源大汇总:AiIndex
省市区 Json 数据:ProvinceJson
Markdown 语法文档:MarkdownDoc
如果您觉得我的开源库帮你节省了大量的开发时间,请扫描下方的二维码随意打赏,要是能打赏个 10.24 🐵就太👍了。您的支持将鼓励我继续创作
(点击查看捐赠列表)
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.




