### Create a Basic Floating Window
Source: https://github.com/kongxiaojun/easyfloat/blob/master/README.md
A minimal example demonstrating how to create and display a floating window using EasyFloat with a single line of code, setting a layout resource.
```Kotlin
EasyFloat.with(this).setLayout(R.layout.float_test).show()
```
--------------------------------
### Configure and Display a Floating Window with Advanced Options (Kotlin)
Source: https://github.com/kongxiaojun/easyfloat/blob/master/README.md
A detailed example showcasing various configuration options for an EasyFloat window, including layout, show pattern, side pattern, tag, drag behavior, location, gravity, animation, activity filtering, and comprehensive state callbacks using Kotlin DSL.
```Kotlin
EasyFloat.with(this)
// 设置浮窗xml布局文件/自定义View,并可设置详细信息
.setLayout(R.layout.float_app) { }
// 设置浮窗显示类型,默认只在当前Activity显示,可选一直显示、仅前台显示
.setShowPattern(ShowPattern.ALL_TIME)
// 设置吸附方式,共15种模式,详情参考SidePattern
.setSidePattern(SidePattern.RESULT_HORIZONTAL)
// 设置浮窗的标签,用于区分多个浮窗
.setTag("testFloat")
// 设置浮窗是否可拖拽
.setDragEnable(true)
// 设置拖拽的viewid,设置后触摸此View才支持拖拽,触摸其他地方则不能拖拽
.setDragViewId(R.id.ivFull)
// 浮窗是否包含EditText,默认不包含
.hasEditText(false)
// 设置浮窗固定坐标,ps:设置固定坐标,Gravity属性和offset属性将无效
.setLocation(100, 200)
// 设置浮窗的对齐方式和坐标偏移量
.setGravity(Gravity.END or Gravity.CENTER_VERTICAL, 0, 200)
// 设置当布局大小变化后,整体view的位置对齐方式
.setLayoutChangedGravity(Gravity.END)
// 设置拖拽边界值
.setBorder(100, 100,800,800)
// 设置宽高是否充满父布局,直接在xml设置match_parent属性无效
.setMatchParent(widthMatch = false, heightMatch = false)
// 设置浮窗的出入动画,可自定义,实现相应接口即可(策略模式),无需动画直接设置为null
.setAnimator(DefaultAnimator())
// 设置系统浮窗的不需要显示的页面
.setFilter(MainActivity::class.java, SecondActivity::class.java)
// 设置系统浮窗的有效显示高度(不包含虚拟导航栏的高度),基本用不到,除非有虚拟导航栏适配问题
.setDisplayHeight { context -> DisplayUtils.rejectedNavHeight(context) }
// 浮窗的一些状态回调,如:创建结果、显示、隐藏、销毁、touchEvent、拖拽过程、拖拽结束。
// ps:通过Kotlin DSL实现的回调,可以按需复写方法,用到哪个写哪个
.registerCallback {
createResult { isCreated, msg, view -> }
show { }
hide { }
dismiss { }
touchEvent { view, motionEvent -> }
drag { view, motionEvent -> }
dragEnd { }
}
// 创建浮窗(这是关键哦😂)
.show()
```
--------------------------------
### XML Layout for Draggable FloatingView
Source: https://github.com/kongxiaojun/easyfloat/blob/master/readme/README_1.3.4.md
Example XML layout demonstrating how to embed a `FloatingView` widget directly in an Android layout file. This allows for a draggable float window within an Activity's layout, containing custom content like an ImageView.
```xml
```
--------------------------------
### Create and Show a Basic EasyFloat Window
Source: https://github.com/kongxiaojun/easyfloat/blob/master/readme/README_1.3.4.md
This concise snippet illustrates the simplest way to create and display a floating window using EasyFloat. It sets a layout from resources and immediately shows the float window.
```Kotlin
EasyFloat.with(this).setLayout(R.layout.float_test).show()
```
--------------------------------
### Comprehensive EasyFloat Window Configuration (Kotlin)
Source: https://github.com/kongxiaojun/easyfloat/blob/master/readme/README_1.3.4.md
This extensive Kotlin snippet showcases the full range of configuration options available when creating an EasyFloat window. It covers setting layout, display patterns, side patterns, tags, dragability, location, gravity, match parent, animations, page filtering, display height, and registering callbacks using Kotlin DSL.
```Kotlin
EasyFloat.with(this)
// 设置浮窗xml布局文件,并可设置详细信息
.setLayout(R.layout.float_app, OnInvokeView { })
// 设置浮窗显示类型,默认只在当前Activity显示,可选一直显示、仅前台显示、仅后台显示
.setShowPattern(ShowPattern.ALL_TIME)
// 设置吸附方式,共15种模式,详情参考SidePattern
.setSidePattern(SidePattern.RESULT_HORIZONTAL)
// 设置浮窗的标签,用于区分多个浮窗
.setTag("testFloat")
// 设置浮窗是否可拖拽,默认可拖拽
.setDragEnable(true)
// 系统浮窗是否包含EditText,仅针对系统浮窗,默认不包含
.hasEditText(false)
// 设置浮窗固定坐标,ps:设置固定坐标,Gravity属性和offset属性将无效
.setLocation(100, 200)
// 设置浮窗的对齐方式和坐标偏移量
.setGravity(Gravity.END or Gravity.CENTER_VERTICAL, 0, 200)
// 设置宽高是否充满父布局,直接在xml设置match_parent属性无效
.setMatchParent(widthMatch = false, heightMatch = false)
// 设置Activity浮窗的出入动画,可自定义,实现相应接口即可(策略模式),无需动画直接设置为null
.setAnimator(DefaultAnimator())
// 设置系统浮窗的出入动画,使用同上
.setAppFloatAnimator(AppFloatDefaultAnimator())
// 设置系统浮窗的不需要显示的页面
.setFilter(MainActivity::class.java, SecondActivity::class.java)
// 设置系统浮窗的有效显示高度(不包含虚拟导航栏的高度),基本用不到,除非有虚拟导航栏适配问题
.setDisplayHeight(OnDisplayHeight { context -> DisplayUtils.rejectedNavHeight(context) })
// 浮窗的一些状态回调,如:创建结果、显示、隐藏、销毁、touchEvent、拖拽过程、拖拽结束。
// ps:通过Kotlin DSL实现的回调,可以按需复写方法,用到哪个写哪个
.registerCallback {
createResult { isCreated, msg, view -> }
show { }
hide { }
dismiss { }
touchEvent { view, motionEvent -> }
drag { view, motionEvent -> }
dragEnd { }
}
// 创建浮窗(这是关键哦😂)
.show()
```
--------------------------------
### Add JitPack Repository to Root Gradle
Source: https://github.com/kongxiaojun/easyfloat/blob/master/readme/README_1.3.4.md
This snippet shows how to add the JitPack Maven repository to your project's root `build.gradle` file. This is a prerequisite for fetching the EasyFloat library from JitPack.
```Groovy
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
```
--------------------------------
### EasyFloat Core API Reference
Source: https://github.com/kongxiaojun/easyfloat/blob/master/README.md
Reference for key methods available in the EasyFloat framework for managing floating windows, including visibility control, drag behavior, view access, position updates, and filtering for system float windows.
```APIDOC
dismiss(tag: String? = null, force: Boolean = false)
tag: Optional tag to identify the float window. Defaults to null.
force: Boolean indicating whether to force dismiss, bypassing exit animations. Defaults to false.
hide(tag: String? = null)
tag: Optional tag to identify the float window. Defaults to null.
show(tag: String? = null)
tag: Optional tag to identify the float window. Defaults to null.
dragEnable(dragEnable: Boolean, tag: String? = null)
dragEnable: Boolean to enable or disable dragging.
tag: Optional tag to identify the float window. Defaults to null.
isShow(tag: String? = null): Boolean
tag: Optional tag to identify the float window. Defaults to null.
Returns: True if the float window is currently shown, false otherwise.
getFloatView(tag: String? = null): View?
tag: Optional tag to identify the float window. Defaults to null.
Returns: The View object of the float window, or null if not found.
updateFloat(tag: String? = null, x: Int = -1, y: Int = -1, width: Int = -1, height: Int = -1)
tag: Optional tag to identify the float window. Defaults to null.
x: New X coordinate. Defaults to -1 (no change).
y: New Y coordinate. Defaults to -1 (no change).
width: New width. Defaults to -1 (no change).
height: New height. Defaults to -1 (no change).
Note: If no values are specified, an adsorption animation is performed.
// ******************* System Float Window Specific *******************
filterActivity(activity: Activity, tag: String? = null)
activity: The Activity to add to the filter list.
tag: Optional tag to identify the float window. Defaults to null.
filterActivities(tag: String? = null, vararg clazz: Class<*>)
tag: Optional tag to identify the float window. Defaults to null.
clazz: Variable number of Class objects representing Activities to add to the filter list.
removeFilter(activity: Activity, tag: String? = null)
activity: The Activity to remove from the filter list.
tag: Optional tag to identify the float window. Defaults to null.
removeFilters(tag: String? = null, vararg clazz: Class<*>)
tag: Optional tag to identify the float window. Defaults to null.
clazz: Variable number of Class objects representing Activities to remove from the filter list.
clearFilters(tag: String? = null)
tag: Optional tag to identify the float window. Defaults to null.
```
--------------------------------
### Integrate EasyFloat Library with Gradle
Source: https://github.com/kongxiaojun/easyfloat/blob/master/README.md
Instructions for adding the EasyFloat library to an Android project's `build.gradle` files. This involves adding the JitPack repository to the root `build.gradle` and the library dependency to the app module's `build.gradle`.
```Gradle
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
```
```Gradle
dependencies {
implementation 'com.github.kongxiaojun:EasyFloat:3.0.1'
}
```
--------------------------------
### Declare System Alert Window Permission
Source: https://github.com/kongxiaojun/easyfloat/blob/master/readme/README_1.3.4.md
This XML snippet demonstrates the necessary permission declaration in `AndroidManifest.xml` for EasyFloat to create system-level floating windows. This permission is crucial for `ShowPattern.ALL_TIME`, `ShowPattern.FOREGROUND`, and `ShowPattern.BACKROUND`.
```XML
```
--------------------------------
### Initialize EasyFloat for Global Features
Source: https://github.com/kongxiaojun/easyfloat/blob/master/readme/README_1.3.4.md
This snippet shows the optional global initialization for EasyFloat. It's required when using specific float window display patterns (foreground/background only) or when filtering pages, as it enables page lifecycle detection.
```Kotlin
EasyFloat.init(this, isDebug)
```
--------------------------------
### Register EasyFloat Callbacks (Java Interface)
Source: https://github.com/kongxiaojun/easyfloat/blob/master/readme/README_1.3.4.md
This Java snippet provides an alternative method for registering float window state callbacks, particularly useful when working in Java projects where Kotlin DSL might be less convenient. It implements the `OnFloatCallbacks` interface to handle various float window events like creation, show, hide, dismiss, touch, and drag.
```Java
.registerCallbacks(new OnFloatCallbacks() {
@Override
public void createdResult(boolean isCreated, @Nullable String msg, @Nullable View view) { }
@Override
public void show(@NotNull View view) { }
@Override
public void hide(@NotNull View view) { }
@Override
public void dismiss() { }
@Override
public void touchEvent(@NotNull View view, @NotNull MotionEvent event) { }
@Override
public void drag(@NotNull View view, @NotNull MotionEvent event) { }
@Override
public void void dragEnd(@NotNull View view) { }
})
```
--------------------------------
### System-wide Float Window API Reference
Source: https://github.com/kongxiaojun/easyfloat/blob/master/readme/README_1.3.4.md
API methods for managing system-level floating windows. These methods provide control over visibility, drag functionality, view retrieval, and filtering activities where the system float window should not appear.
```APIDOC
dismissAppFloat(tag: String? = null)
hideAppFloat(tag: String? = null)
showAppFloat(tag: String? = null)
appFloatDragEnable(dragEnable: Boolean, tag: String? = null)
appFloatIsShow(tag: String? = null)
getAppFloatView(tag: String? = null)
filterActivity(activity: Activity, tag: String? = null)
filterActivities(tag: String? = null, vararg clazz: Class<*>)
removeFilter(activity: Activity, tag: String? = null)
removeFilters(tag: String? = null, vararg clazz: Class<*>)
clearFilters(tag: String? = null)
```
--------------------------------
### Implement Drag-to-Close and Swipe-to-Create Floating Windows in Kotlin
Source: https://github.com/kongxiaojun/easyfloat/blob/master/README.md
This snippet demonstrates how to register drag-to-close functionality using `DragUtils.registerDragClose` within a drag callback and how to register swipe-to-create functionality using `DragUtils.registerSwipeAdd` in the Activity's `dispatchTouchEvent` method. It utilizes `OnTouchRangeListener` for handling touch events within a specified range, allowing for actions like dismissing a float window or creating a new one.
```kotlin
// 在拖拽回调中,注册拖拽关闭
drag { view, motionEvent ->
DragUtils.registerDragClose(motionEvent, object : OnTouchRangeListener {
override fun touchInRange(inRange: Boolean, view: BaseSwitchView) {
// 震动、视图调整等...
}
override fun touchUpInRange() {
// 关闭浮窗等...
EasyFloat.dismiss(tag, true)
}
})
}
// 在Activity的dispatchTouchEvent中,注册侧滑创建
DragUtils.registerSwipeAdd(ev, object : OnTouchRangeListener {
override fun touchInRange(inRange: Boolean, view: BaseSwitchView) {
// 震动、视图调整等...
}
override fun touchUpInRange() {
// 浮窗创建等,详情参考:SwipeTestActivity
showFloat()
}
})
```
--------------------------------
### Add EasyFloat Dependency to App Module Gradle
Source: https://github.com/kongxiaojun/easyfloat/blob/master/readme/README_1.3.4.md
This snippet demonstrates how to include the EasyFloat library as a dependency in your application module's `build.gradle` file. Replace '1.3.4' with the desired version.
```Groovy
dependencies {
implementation 'com.github.princekin-f:EasyFloat:1.3.4'
}
```
--------------------------------
### Register Floating Window Callbacks (Java)
Source: https://github.com/kongxiaojun/easyfloat/blob/master/README.md
An alternative way to register callbacks for floating window events in Java, using the `OnFloatCallbacks` interface, as Kotlin DSL is less convenient in Java.
```Java
.registerCallbacks(new OnFloatCallbacks() {
// 各种回调...
...
})
```
--------------------------------
### Declare SYSTEM_ALERT_WINDOW Permission in AndroidManifest
Source: https://github.com/kongxiaojun/easyfloat/blob/master/README.md
Required permission declaration in `AndroidManifest.xml` for using system-level floating windows (e.g., `ShowPattern.ALL_TIME`, `ShowPattern.FOREGROUND`, `ShowPattern.BACKROUND`). This permission is not needed for single-page float windows.
```XML
```
--------------------------------
### Check and Request Floating Window Permissions
Source: https://github.com/kongxiaojun/easyfloat/blob/master/readme/README_1.3.4.md
This snippet demonstrates how to programmatically check and request the necessary floating window permissions using `PermissionUtils`. The results of permission checks and requests can be obtained via the `OnFloatCallbacks` interface's `createdResult` method.
```Kotlin
// 权限检测
PermissionUtils.checkPermission(this)
// 权限申请,参数2为权限回调接口
PermissionUtils.requestPermission(this,OnPermissionResult)
```
--------------------------------
### Activity-bound Float Window API Reference
Source: https://github.com/kongxiaojun/easyfloat/blob/master/readme/README_1.3.4.md
API methods for managing floating windows associated with a specific Android Activity. These methods allow controlling visibility, drag behavior, and retrieving the float view for an Activity-level overlay.
```APIDOC
dismiss(activity: Activity? = null, floatTag: String? = null)
hide(activity: Activity? = null, floatTag: String? = null)
show(activity: Activity? = null, floatTag: String? = null)
setDragEnable(activity: Activity? = null, dragEnable: Boolean, floatTag: String? = null )
isShow(activity: Activity? = null, floatTag: String? = null)
getFloatView(activity: Activity? = null, tag: String? = null)
```
--------------------------------
### Manage Soft Keyboard for EditText in System Float Window
Source: https://github.com/kongxiaojun/easyfloat/blob/master/readme/README_1.3.4.md
Code snippets demonstrating how to programmatically open and close the soft keyboard for an EditText within a system float window. It requires setting `.hasEditText(true)` on the float window configuration.
```kotlin
InputMethodUtils.openInputMethod(editText, tag)
```
```kotlin
InputMethodUtils.closedInputMethod(tag)
```
--------------------------------
### Check and Request Floating Window Permissions
Source: https://github.com/kongxiaojun/easyfloat/blob/master/README.md
Utility functions provided by EasyFloat for checking and requesting floating window permissions. Permission results can be obtained via the `OnFloatCallbacks` interface's `createdResult` method.
```Kotlin
// 权限检测
PermissionUtils.checkPermission(this)
// 权限申请,参数2为权限回调接口
PermissionUtils.requestPermission(this,OnPermissionResult)
```
--------------------------------
### ProGuard Rule for EasyFloat Library
Source: https://github.com/kongxiaojun/easyfloat/blob/master/readme/README_1.3.4.md
ProGuard rule to prevent obfuscation and ensure proper functioning of the EasyFloat library classes during release builds. It keeps all classes and members within the `com.lzf.easyfloat` package.
```proguard
-keep class com.lzf.easyfloat.** {*;}
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.