Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

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

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:11.2'}

使用案例

  • 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,newEasyWindow.OnClickListener<TextView>() {@OverridepublicvoidonClick(EasyWindow<?>easyWindow,TextViewview) {// 点击这个 View 后消失easyWindow.cancel();// 跳转到某个Activity// easyWindow.startActivity(intent);            }        })        .show();
  • 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<TextView?> { easyWindow:EasyWindow<*>, view:TextView?->// 点击这个 View 后消失        easyWindow.cancel()// 跳转到某个Activity// easyWindow.startActivity(intent);    })}.show()
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<TextView?> { easyWindow:EasyWindow<*>, view:TextView?->// 点击这个 View 后消失            easyWindow.cancel()// 跳转到某个Activity// easyWindow.startActivity(intent);        })        .show()

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

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

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

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

publicfinalclassActivityWindowLifecycleimplementsApplication.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 介绍

  • 对象方法
// 设置悬浮窗宽度setWidth(intwidth)// 设置悬浮窗高度setHeight(intheight)// 设置悬浮窗显示的重心setGravity(intgravity)// 设置水平偏移量setXOffset(intpx)// 设置垂直偏移量setYOffset(intpx)// 设置悬浮窗外层是否可触摸setOutsideTouchable(booleantouchable)// 设置悬浮窗背景阴影强度setBackgroundDimAmount(floatamount)// 添加窗口标记addWindowFlags(intflags)// 移除窗口标记removeWindowFlags(intflags)// 设置窗口标记setWindowFlags(intflags)// 是否存在某个窗口标记hasWindowFlags(intflags)// 设置悬浮窗的显示类型setWindowType(inttype)// 几乎涵盖了所有 WindowManager 的参数// 更多 API 方法请查看 EasyWindow 类的源码......
  • 静态方法
// 取消所有正在显示的悬浮窗EasyWindow.cancelAllWindow()// 取消特定类名的悬浮窗EasyWindow.cancelWindowByClass(Class<?extendsEasyWindow<?>>clazz)// 取消特定标记的悬浮窗EasyWindow.cancelWindowByTag(Stringtag)// 显示所有已取消但未回收的悬浮窗EasyWindow.showAllWindow()// 显示特定类名已取消但未回收的悬浮窗EasyWindow.showWindowByClass(Class<?extendsEasyWindow<?>>clazz)// 显示特定标记已取消但未回收的悬浮窗EasyWindow.showWindowByTag(Stringtag)// 回收所有正在显示的悬浮窗EasyWindow.recycleAllWindow()// 回收特定类名的悬浮窗EasyWindow.recycleWindowByClass(Class<?extendsEasyWindow<?>>clazz)// 回收特定标记的悬浮窗EasyWindow.recycleWindowByTag(Stringtag)// 判断当前是否有悬浮窗正在显示EasyWindow.existAnyWindowShowing()// 判断当前是否有特定类名的悬浮窗正在显示EasyWindow.existWindowShowingByClass(Class<?extendsEasyWindow<?>>clazz)// 判断当前是否有特定标记的悬浮窗正在显示EasyWindow.existWindowShowingByTag(Stringtag)// 获取所有的悬浮窗EasyWindow.getAllWindowInstance()// 获取特定类名的悬浮窗EasyWindow.getWindowInstanceByClass(Class<?extendsEasyWindow<?>>clazz)// 获取特定标记的悬浮窗EasyWindow.getWindowInstanceByTag(Stringtag)

框架混淆规则

  • 在混淆规则文件proguard-rules.pro 中加入
-keep class com.hjq.window.** {*;}

作者的其他开源项目

微信公众号: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