跳至主要內容

floating_window - 悬浮窗

约 1480 字大约 5 分钟道无涯

floating_window模块提供显示悬浮窗,检查是否有悬浮窗权限等功能。

由于悬浮窗模块涉及UI交互,显示悬浮窗需要在UI线程中运行,因此需要在文件开始指定:

  • "ui-thread nodejs";:仅悬浮窗,无UI页面Activity的情况
  • "ui nodejs";:既有悬浮窗,又有UI页面Activity
  • "ui-thread";:仅悬浮窗,无UI页面Activity,并且文件名本身用.node.js结尾
  • "ui";:既有悬浮窗,又有UI页面Activity,并且文件名本身用.node.js结尾

示例

"ui-thread nodejs";

const { createWindow } = require("floating_window");
const window = createWindow();
window.setViewFromXml(`
  <vertical bg="#ffffff">
      <text text="Node.js: ${process.version}" textColor="#aa0000" textSize="16" width="*"/>                
  </vertical>
`);
window.show();
$autojs.keepRunning();

目录

接口

类型别名

变量

函数

类型别名

KeepToEdgeType

Ƭ KeepToEdgeType: "bounce" | boolean

是否自动贴边。如果为true,在拖拽结束时,悬浮窗会自动贴边;如果为bounce,则会跳跃多次后贴边,类似Auto.js Pro自带悬浮窗的弹性效果。

变量

FLAG_DIM_BEHIND

Const FLAG_DIM_BEHIND: 2

Window flag: everything behind this window will be dimmed. Use #dimAmount to control the amount of dim.

参见


FLAG_FULLSCREEN

Const FLAG_FULLSCREEN: 1024

Window flag: hide all screen decorations (such as the status bar) while this window is displayed. This allows the window to use the entire display space for itself -- the status bar will be hidden when an app window with this flag set is on the top layer. A fullscreen window will ignore a value of #SOFT_INPUT_ADJUST_RESIZE for the window's #softInputMode field; the window will stay fullscreen and will not resize.

This flag can be controlled in your theme through the android.R.attr#windowFullscreen attribute; this attribute is automatically set for you in the standard fullscreen themes such as android.R.style#Theme_NoTitleBar_Fullscreen, android.R.style#Theme_Black_NoTitleBar_Fullscreen, android.R.style#Theme_Light_NoTitleBar_Fullscreen, android.R.style#Theme_Holo_NoActionBar_Fullscreen, android.R.style#Theme_Holo_Light_NoActionBar_Fullscreen, android.R.style#Theme_DeviceDefault_NoActionBar_Fullscreen, and android.R.style#Theme_DeviceDefault_Light_NoActionBar_Fullscreen.

Deprecated

Use WindowInsetsController#hide(int) with Type#statusBars() instead.

参见


FLAG_KEEP_SCREEN_ON

Const FLAG_KEEP_SCREEN_ON: 128

设置该标志让窗口显示时保持屏幕常亮。

Window flag: as long as this window is visible to the user, keep the device's screen turned on and bright.

参见


FLAG_LAYOUT_IN_SCREEN

Const FLAG_LAYOUT_IN_SCREEN: 256

设置该标志让窗口保持在屏幕内显示。

Window flag for attached windows: Place the window within the entire screen, ignoring any constraints from the parent window.

Note: on displays that have a DisplayCutout, the window may be placed such that it avoids the DisplayCutout area if necessary according to the #layoutInDisplayCutoutMode.


FLAG_LAYOUT_NO_LIMITS

Const FLAG_LAYOUT_NO_LIMITS: 512

设置该标志让窗口可超出屏幕范围。

Window flag: allow window to extend outside of the screen.

参见


FLAG_NOT_FOCUSABLE

Const FLAG_NOT_FOCUSABLE: 8

设置该标志将让该窗口无法获得焦点。

Window flag: this window won't ever get key input focus, so the user can not send key or other button events to it. Those will instead go to whatever focusable window is behind it. This flag will also enable #FLAG_NOT_TOUCH_MODAL whether or not that is explicitly set.

Setting this flag also implies that the window will not need to interact with a soft input method, so it will be Z-ordered and positioned independently of any active input method (typically this means it gets Z-ordered on top of the input method, so it can use the full screen for its content and cover the input method if needed. You can use #FLAG_ALT_FOCUSABLE_IM to modify this behavior.

参见


FLAG_NOT_TOUCHABLE

Const FLAG_NOT_TOUCHABLE: 16

设置该标志将让该窗口无法获得触摸事件,但在Android 12及以上因系统安全限制无效。

Window flag: this window can never receive touch events.

The intention of this flag is to leave the touch to be handled by some window below this window (in Z order).

Starting from Android Build.VERSION_CODES#S, for security reasons, touch events that pass through windows containing this flag (ie. are within the bounds of the window) will only be delivered to the touch-consuming window if one (or more) of the items below are true:

  • Same UID: This window belongs to the same UID that owns the touch-consuming window.
  • Trusted windows: This window is trusted. Trusted windows include (but are not limited to) accessibility windows (#TYPE_ACCESSIBILITY_OVERLAY), the IME (#TYPE_INPUT_METHOD) and assistant windows (TYPE_VOICE_INTERACTION). Windows of type #TYPE_APPLICATION_OVERLAY are not trusted, see below.
  • Invisible windows: This window is View#GONE or View#INVISIBLE.
  • Fully transparent windows: This window has LayoutParams#alpha equal to 0.
  • One SAW window with enough transparency: This window is of type #TYPE_APPLICATION_OVERLAY, has LayoutParams#alpha below or equal to the maximum obscuring opacity (see below) and it's the only window of type #TYPE_APPLICATION_OVERLAY from this UID in the touch path.
  • Multiple SAW windows with enough transparency: The multiple overlapping #TYPE_APPLICATION_OVERLAY windows in the touch path from this UID have a combined obscuring opacity below or equal to the maximum obscuring opacity. See section Combined obscuring opacity below on how to compute this value. If none of these cases hold, the touch will not be delivered and a message will be logged to logcat.

Maximum obscuring opacity

This value is 0.8. Apps that want to gather this value from the system rather than hard-coding it might want to use android.hardware.input.InputManager#getMaximumObscuringOpacityForTouch().

Combined obscuring opacity

The combined obscuring opacity of a set of windows is obtained by combining the opacity values of all windows in the set using the associative and commutative operation defined as:

opacity({A,B}) = 1 - (1 - opacity(A))*(1 - opacity(B))

where {@code opacity(X)} is the LayoutParams#alpha of window X. So, for a set of windows {@code {W1, .., Wn}}, the combined obscuring opacity will be:

opacity({W1, .., Wn}) = 1 - (1 - opacity(W1)) * ... * (1 - opacity(Wn))

参见


FLAG_SECURE

Const FLAG_SECURE: 8192

设置该标志让窗口内容无法被截图、录屏。

Window flag: treat the content of the window as secure, preventing it from appearing in screenshots or from being viewed on non-secure displays.

See android.view.Display#FLAG_SECURE for more details about secure surfaces and secure displays.

参见


FLAG_WATCH_OUTSIDE_TOUCH

Const FLAG_WATCH_OUTSIDE_TOUCH: 262144

设置该标志后,窗口将可在有窗口外部的触摸事件时收到ACTION_OUTSIDE事件。

Window flag: if you have set #FLAG_NOT_TOUCH_MODAL, you can set this flag to receive a single special MotionEvent with the action MotionEvent#ACTION_OUTSIDE MotionEvent.ACTION_OUTSIDE for touches that occur outside of your window. Note that you will not receive the full down/move/up gesture, only the location of the first down as an ACTION_OUTSIDE.

参见

函数

canDrawOverlays

canDrawOverlays(): boolean

判断是否有悬浮窗权限。

返回值

boolean

如果本应用有悬浮窗权限,则返回 true,否则返回 false


createWindow

createWindow(options?): FloatingWindow

创建一个新悬浮窗,可指定悬浮窗的初始大小、位置等参数。

参数

名称类型描述
options?CreateWindowOptions创建悬浮窗选项,参见CreateWindowOptions

返回值

FloatingWindow


manageDrawOverlays

manageDrawOverlays(): void

跳转到管理悬浮窗权限的页面。

返回值

void