我们在开发Autojs脚本时,需要使用到悬浮窗功能来控制脚本。那么到底要如何来做呢?今天给大家分享一些,先来看看效果:

调整移动和贴边。
防止滑出屏幕
附上完整代码
var img_url = "file://res/2.jpg"
suspendedWindow();
function suspendedWindow() {
window = floaty.rawWindow(
<horizontal gravity="center_vertical">
<img id="floaty_icon" src="{{img_url}}" w="40" h="40" alpha="0.8" circle="true" borderWidth="1dp" borderColor="black" />
<horizontal id="h_drawer">
<vertical>
<button id="ui_start" textColor="#FFFFFF" text="开始" bg="#4F4F4F" padding="0" h="40" w="50" />
<text text="" h="1" />
<button id="ui_close" textColor="#FFFFFF" text="结束" bg="#4F4F4F" padding="0" h="40" w="50" />
</vertical>
</horizontal>
</horizontal>
);
window.setPosition(50, device.height / 3);
window.exitOnClose();
setInterval(() => { }, 1000);
window.h_drawer.visibility = 8;
var execution = null;
var x = 0,
y = 0;
var windowX, windowY;
var downTime;
console.log("w=" + device.width);
console.log("h=" + device.height);
window.floaty_icon.setOnTouchListener(function (view, event) {
switch (event.getAction()) {
case event.ACTION_DOWN:
x = event.getRawX();
y = event.getRawY();
windowX = window.getX();
windowY = window.getY();
downTime = new Date().getTime();
return true;
case event.ACTION_MOVE:
let movexx = windowX + (event.getRawX() - x);
let moveyy = windowY + (event.getRawY() - y);
if (movexx < 0 || movexx > device.width) {
movexx = 0;
}
if (moveyy < 0 || moveyy > device.height) {
moveyy = 0;
}
window.setPosition(movexx, moveyy);
console.log("event y=" + event.getRawY());
console.log("event x=" + event.getRawX());
return true;
case event.ACTION_UP:
if (Math.abs(event.getRawY() - y) < 5 && Math.abs(event.getRawX() - x) < 5) {
drawerStatus();
}
return true;
}
return true;
});
function drawerStatus() {
if (window.h_drawer.visibility == 8) {
window.h_drawer.visibility = 0;
} else {
window.h_drawer.visibility = 8;
}
}
window.ui_close.setOnTouchListener(function (view, event) {
if (event.getAction() == event.ACTION_UP) {
toastLog("关闭脚本...");
window.close();
exit();
}
return true;
});
window.ui_start.setOnTouchListener(function (view, event) {
if (event.getAction() == event.ACTION_UP) {
window.setPosition(50, device.height / 3);
window.disableFocus();
if (window.ui_start.text() == "开始") {
window.ui_start.text("暂停");
console.log("开始运行悬浮窗");
var main = threads.start(function () {
device.keepScreenOn()
})
setTimeout(function () {
if (window.ui_start.text() == "暂停") {
drawerStatus()
}
}, 3000)
var monitoringStatus = setInterval(function () {
if (window.ui_start.text() == "开始") {
main.interrupt()
toastLog("暂停了")
clearInterval(monitoringStatus)
}
}, 100)
} else {
window.ui_start.text("开始");
toastLog("开始暂停...");
threads.shutDownAll();
}
}
return true;
});
events.observeKey();
events.on("key", function (code, event) {
engines.myEngine().forceStop();
threads.shutDownAll();
});
}