Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

License

NotificationsYou must be signed in to change notification settings

getActivity/EasyWindow

Repository files navigation

本框架意在解决一些悬浮窗的需求,如果是普通的 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 {        targetCompatibilityJavaVersion.VERSION_1_8        sourceCompatibilityJavaVersion.VERSION_1_8    }}dependencies {// 悬浮窗框架:https://github.com/getActivity/EasyWindow    implementation'com.github.getActivity:EasyWindow:13.0'}

AndroidX 兼容

  • 如果项目是基于AndroidX 库,请在项目gradle.properties 文件中加入以下配置,如果已经加入则可以忽略
# 表示使用 AndroidXandroid.useAndroidX = true# 表示将第三方库迁移到 AndroidXandroid.enableJetifier = true
  • 如果项目是基于Support 库则不需要加入此配置

使用案例

  • 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) {// 有两种方式取消弹窗:// 1. easyWindow.cancel:顾名思义,取消显示// 2. easyWindow.recycle:在取消显示的基础上,加上了回收// 这两种区别在于,cancel 之后还能 show,但是 recycle 之后不能再 show// 通常情况下,如果你创建的 EasyWindow 对象在 cancel 之后永远不会再显示,取消弹窗建议直接用 recycle 方法,否则用 cancel 方法easyWindow.recycle();// 跳转到某个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->// 有两种方式取消弹窗:// 1. easyWindow.cancel:顾名思义,取消显示// 2. easyWindow.recycle:在取消显示的基础上,加上了回收// 这两种区别在于,cancel 之后还能 show,但是 recycle 之后不能再 show// 通常情况下,如果你创建的 EasyWindow 对象在 cancel 之后永远不会再显示,取消弹窗建议直接用 recycle 方法,否则用 cancel 方法        easyWindow.recycle()// 跳转到某个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->// 有两种方式取消弹窗:// 1. easyWindow.cancel:顾名思义,取消显示// 2. easyWindow.recycle:在取消显示的基础上,加上了回收// 这两种区别在于,cancel 之后还能 show,但是 recycle 之后不能再 show// 通常情况下,如果你创建的 EasyWindow 对象在 cancel 之后永远不会再显示,取消弹窗建议直接用 recycle 方法,否则用 cancel 方法            easyWindow.recycle()// 跳转到某个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)                .setView(R.layout.xxx)                .show();    }@OverridepublicvoidonActivityStarted(Activityactivity) {}@OverridepublicvoidonActivityResumed(Activityactivity) {}@OverridepublicvoidonActivityPaused(Activityactivity) {}@OverridepublicvoidonActivityStopped(Activityactivity) {}@OverridepublicvoidonActivitySaveInstanceState(Activityactivity,BundleoutState) {}@OverridepublicvoidonActivityDestroyed(Activityactivity) {}}

框架的 API 介绍

  • 对象方法
// 显示悬浮窗easyWindow.show();// 延迟显示悬浮窗(可在子线程中调用,不怕频繁调用)easyWindow.delayShow();easyWindow.delayShow(longdelayMillis);// 将悬浮窗显示在指定 View 的旁边easyWindow.showAsDropDown(@NonNullViewanchorView, intshowGravity, intxOff,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);// 设置悬浮窗拖拽规则(框架内部提供了两种拖拽规则,MovingWindowDraggableRule 和 SpringBackWindowDraggableRule )easyWindow.setWindowDraggableRule(@NullableAbstractWindowDraggableRuledraggableRule);// 设置悬浮窗拖拽规则(可能为空)easyWindow.getWindowDraggableRule();// 设置悬浮窗内容布局easyWindow.setContentView(@LayoutResintlayoutId);easyWindow.setContentView(@LayoutResintlayoutId,@NullableOnWindowLayoutInflateListenerlistener);easyWindow.setContentView(@NonNullViewview);// 获取内容布局(可能为空)easyWindow.getContentView();// 限定悬浮窗显示时长easyWindow.setWindowDuration(@IntRange(from =0) intdelayMillis);// 设置悬浮窗 tageasyWindow.setWindowTag(@NullableStringtag);// 获取悬浮窗 tageasyWindow.getWindowTag();// 设置悬浮窗宽度和高度easyWindow.setWindowSize(intwidth,intheight);// 设置悬浮窗大小(按照屏幕百分比)easyWindow.setWindowSizePercent(@FloatRange(from =0,to =1)floatwidthPercent,@FloatRange(from =0,to =1) floatheightPercent);// 设置悬浮窗的位置easyWindow.setWindowLocation(@Pxintx,@Px inty);easyWindow.setWindowLocation(@GravityFlagintgravity,@Px intx,@Px inty);// 设置窗口位置(偏移量按照屏幕百分比)easyWindow.setWindowLocationPercent(@FloatRange(from =0,to =1)@Px floathorizontalPercent,@FloatRange(from =0,to =1)@Px floatverticalPercent);easyWindow.setWindowLocationPercent(@GravityFlag intgravity,@FloatRange(from =0,to =1)@Px floathorizontalPercent,@FloatRange(from =0,to =1)@Px floatverticalPercent);// 设置悬浮窗外层是否可触摸easyWindow.setOutsideTouchable(booleantouchable);// 设置悬浮窗背景阴影强度easyWindow.setBackgroundDimAmount(@FloatRange(from =0.0,to =1.0) floatamount);// 添加窗口标记easyWindow.addWindowFlags(intflags);// 移除窗口标记easyWindow.removeWindowFlags(intflags);// 设置窗口标记easyWindow.setWindowFlags(intflags);// 是否存在某个窗口标记easyWindow.hasWindowFlags(intflags);// 设置悬浮窗的显示类型easyWindow.setWindowType(inttype);// 设置悬浮窗动画样式easyWindow.setWindowAnim(intid);// 设置悬浮窗软键盘模式easyWindow.setSoftInputMode(intsoftInputMode);// 设置悬浮窗 TokeneasyWindow.setWindowToken(@NullableIBindertoken);// 设置悬浮窗透明度easyWindow.setWindowAlpha(@FloatRange(from =0.0,to =1.0) floatalpha);// 设置容器和窗口小部件之间的垂直边距easyWindow.setVerticalMargin(floatverticalMargin);// 设置容器和窗口小部件之间的水平边距easyWindow.setHorizontalMargin(floathorizontalMargin);// 设置位图格式easyWindow.setBitmapFormat(intformat);// 设置状态栏的可见性easyWindow.setSystemUiVisibility(intsystemUiVisibility);// 设置垂直权重easyWindow.setVerticalWeight(floatverticalWeight);// 设置挖孔屏下的显示模式easyWindow.setLayoutInDisplayCutoutMode(intlayoutInDisplayCutoutMode);// 设置悬浮窗在哪个显示屏上显示easyWindow.setPreferredDisplayModeId(intpreferredDisplayModeId);// 设置悬浮窗标题easyWindow.setWindowTitle(@NullableCharSequencetitle);// 设置屏幕的亮度easyWindow.setScreenBrightness(@FloatRange(from = -1.0,to =1.0) floatscreenBrightness);// 设置键盘背光的亮度easyWindow.setButtonBrightness(@FloatRange(from = -1.0,to =1.0) floatbuttonBrightness);// 设置悬浮窗的刷新率easyWindow.setPreferredRefreshRate(floatpreferredRefreshRate);// 设置悬浮窗的颜色模式easyWindow.setColorMode(intcolorMode);// 设置悬浮窗背后的高斯模糊半径大小(Android 12 及以上才支持)easyWindow.setBlurBehindRadius(@IntRange(from =0) intblurBehindRadius);// 设置悬浮窗屏幕方向easyWindow.setScreenOrientation(intscreenOrientation);// 设置悬浮窗可见性easyWindow.setWindowViewVisibility(intvisibility);// 获取悬浮窗可见性easyWindow.getWindowViewVisibility();// 设置悬浮窗根布局(一般情况下推荐使用 {@link #setContentView} 方法来填充布局)easyWindow.setRootLayout(@NonNullViewGroupviewGroup);// 重新设置 WindowManager 参数集easyWindow.setWindowParams(@NonNullWindowManager.LayoutParamsparams);// 重新设置 WindowManager 对象easyWindow.setWindowManager(@NonNullWindowManagerwindowManager);// 获取当前窗口视图宽度easyWindow.getWindowViewWidth();// 获取当前窗口视图高度easyWindow.getWindowViewHeight();// 设置可见性状态给 VieweasyWindow.setVisibilityByView(@IdRes intviewId, intvisibility);// 设置背景 Drawable 给 VieweasyWindow.setBackgroundDrawableByView(@IdRes intviewId,@DrawableRes intdrawableId);easyWindow.setBackgroundDrawableByView(@IdRes intviewId,@NullableDrawabledrawable);// 设置文本给 TextVieweasyWindow.setTextByTextView(@IdRes intviewId,@StringRes intstringId);easyWindow.setTextByTextView(@IdRes intviewId,@NullableCharSequencetext);// 设置字体颜色给 TextVieweasyWindow.setTextColorByTextView(@IdRes intviewId,@ColorInt intcolorValue);// 设置字体大小给 TextVieweasyWindow.setTextSizeByTextView(@IdRes intviewId, floattextSize);easyWindow.setTextSizeByTextView(@IdRes intviewId, intunit, floattextSize);// 设置提示文本给 TextVieweasyWindow.setHintTextByTextView(@IdRes intviewId,@StringRes intstringId);easyWindow.setHintTextByTextView(@IdRes intviewId,@NullableCharSequencetext);// 设置提示文本颜色给 TextVieweasyWindow.setHintTextColorByTextView(@IdRes intviewId,@ColorInt intcolorValue);// 设置图片 Drawable 给 ImageVieweasyWindow.setImageDrawableByImageView(@IdRes intviewId,@DrawableRes intdrawableId);easyWindow.setImageDrawableByImageView(@IdRes intviewId,@NullableDrawabledrawable);// 设置点击监听(没有传入 View 的方法默认使用窗口根布局)easyWindow.setOnClickListener(@NullableOnWindowViewClickListener<?extendsView>listener);easyWindow.setOnClickListenerByView(@IdRes intid,@NullableOnWindowViewClickListener<?extendsView>listener);// 设置长按监听(没有传入 View 的方法默认使用窗口根布局)easyWindow.setOnLongClickListener(@NullableOnWindowViewLongClickListener<?extendsView>listener);easyWindow.setOnLongClickListenerByView(@IdRes intid,@NullableOnWindowViewLongClickListener<?extendsView>listener);// 设置触摸监听(没有传入 View 的方法默认使用窗口根布局)easyWindow.setOnTouchListener(@NullableOnWindowViewTouchListener<?extendsView>listener);easyWindow.setOnTouchListenerByView(@IdRes intid,@NullableOnWindowViewTouchListener<?extendsView>listener);// 设置按键监听(没有传入 View 的方法默认使用窗口根布局)easyWindow.setOnKeyListener(@NullableOnWindowViewKeyListener<?extendsView>listener);easyWindow.setOnKeyListenerByView(@IdRes intid,@NullableOnWindowViewKeyListener<?extendsView>listener);// 跳转 ActivityeasyWindow.startActivity(@NullableClass<?extendsActivity>clazz);easyWindow.startActivity(@NullableIntentintent);// 延迟执行任务easyWindow.sendTask(Runnablerunnable);// 延迟一段时间执行任务easyWindow.sendTask(@NonNullRunnablerunnable, longdelayMillis);// 移除指定的任务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.findWindowInstanceByClass(@NullableClass<?extendsEasyWindow<?>>clazz);// 寻找特定标记的悬浮窗EasyWindowManager.findWindowInstanceByTag(@NullableStringtag);// 获取所有的悬浮窗EasyWindowManager.getAllWindowInstance();

作者的其他开源项目

微信公众号:Android轮子哥

Android 技术 Q 群:10047167

如果您觉得我的开源库帮你节省了大量的开发时间,请扫描下方的二维码随意打赏,要是能打赏个 10.24 🐵就太👍了。您的支持将鼓励我继续创作:octocat:点击查看捐赠列表

License

Copyright 2019 Huang JinQunLicensed 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.0Unless required by applicable law or agreed to in writing, softwaredistributed 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 andlimitations under the License.

[8]ページ先頭

©2009-2025 Movatter.jp