autojs悬浮代码例子悬浮动画悬浮组件计算源代码分享

本文介绍了一个使用Auto.js实现的悬浮窗控制脚本,能够自定义响应点击、移动和长按事件,并自动调整窗口位置以适应屏幕旋转。此外,还展示了如何加载Web视图并进行基本的网页浏览操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


/**
*完成时间: 2019年3月20日 下午11:24:54
*测试机型: PD1813D
 *Auto.js版本: 4.1.0 Alpha5
 *屏幕: 1080*2280
*备注: 暂无备注
**/
 //此代码由飞云脚本圈整理提供(www.feiyunjs.com)
var 悬浮控制 = function(window, windowid, ar) {
    this.Orientation = context.resources.configuration.orientation;
    this.Width = this.Orientation == 1 ? device.width : device.height;
    this.Height = this.Orientation == 2 ? device.width : device.height;
    this.isAutoIntScreen = true;
    this.Click = function() {};
    this.Move = function() {};
    this.LongClick = function() {};
    this.setClick = (fun) => {
        fun = fun || function() {};
        this.Click = fun;
    };
    this.setMove = (fun) => {
        fun = fun || function() {};
        this.Move = fun;
    };
    this.setLongClick = (fun, ji) => {
        fun = fun || function() {};
        this.LongClick = fun;
        if (parseInt(ji)) {
            this.Tjitime = parseInt(ji) / 50;
        };
    };
    setInterval(() => {
        if (context.resources.configuration.orientation != this.Orientation) {
            this.Orientation = context.resources.configuration.orientation;
            this.Width = this.Orientation == 1 ? device.width : device.height;
            this.Height = this.Orientation == 2 ? device.width : device.height;
            var xy = this.windowGXY(window.getX(), window.getY(), this.G(window));
            this.windowyidong([
                [window.getX(), window.getY()],
                [xy.x, xy.y]
            ]);
        };
    }, 100);
    this.TX = 0;
    this.TY = 0;
    this.Tx = 0;
    this.Ty = 0;
    this.Tyidong = false;
    this.Tkeep = false;
    this.Tjitime = 12;
    this.Ttime = 0;
    setInterval(() => {
        if (this.Tkeep) {
            this.Ttime++;
            if (!this.Tyidong && this.Ttime > this.Tjitime) {
                //非移动且按下时长超过1秒判断为长按
                this.Tkeep = false;
                this.Ttime = 0;
                this.LongClick();
            };
        };
    }, 50);
    if (windowid) {
        windowid.setOnTouchListener(new android.view.View.OnTouchListener((view, event) => {
            this.Move(view, event);
            switch (event.getAction()) {
                case event.ACTION_DOWN:
                    this.Tx = event.getRawX();
                    this.Ty = event.getRawY();
                    this.TX = window.getX();
                    this.TY = window.getY();
                    this.Tkeep = true; //按下,开启计时
                    break;
                case event.ACTION_MOVE:
                    var sx = event.getRawX() - this.Tx;
                    var sy = event.getRawY() - this.Ty;
                    if (!this.Tyidong && this.Tkeep && this.weiyi(sx, sy) >= 10) {
                        this.Tyidong = true;
                    };
                    if (this.Tyidong && this.Tkeep) {
                        window.setPosition(this.TX + sx, this.TY + sy);
                    };
                    break;
                case event.ACTION_UP:
                    if (!this.Tyidong && this.Tkeep && this.Ttime < 7) {
                        this.Click();
                    };
                    this.Tkeep = false;
                    this.Ttime = 0;
                    if (this.Tyidong) {
                        if (this.isAutoIntScreen) {
                            threads.start(new java.lang.Runnable(() => {
                                this.windowyidong(this.IntScreen());
                            }));
                        } else {
                            threads.start(new java.lang.Runnable(() => {
                                this.windowyidong(this.ViewIntScreen());
                            }));

                        };
                        this.Tyidong = false;
                    };
                    break;
            };
            return true;
        }));
    };
    this.G = (win, view) => {
        //返回悬浮窗的坐标范围。
        var K = 36, //悬浮窗的隐形边矩
            H = 66; //手机通知栏的高度
        var ary;
        if (!ar) {
            if (view) {
                ary = [
                    [-view.getX(), -view.getY()],
                    [this.Width - (view.getX() + view.getWidth()), this.Height - (view.getY() + view.getHeight()) - H - K]
                ];

            } else {
                ary = [
                    [0, 0],
                    [this.Width - win.getWidth() + K * 2, this.Height - win.getHeight() - H + K * 2]
                ];
            }
        } else {
            if (view) {
                ary = [
                    [-view.getX(), H - view.getY()],
                    [this.Width - (view.getX() + view.getWidth()), this.Height - (view.getY() + view.getHeight())]
                ];

            } else {
                ary = [
                    [0, H],
                    [this.Width - win.getWidth(), this.Height - win.getHeight()]
                ];
            }
        };
        return ary;
    };
    this.weiyi = function() { //平方和开方
        var num = 0;
        for (var i = 0; i < arguments.length; i++) {
            num += arguments[i] * arguments[i];
        };
        return Math.round(Math.sqrt(num) * 1000) / 1000
    };
    this.windowGXY = function(x, y, k) {
        //修正坐标的所在范围。如果坐标超出了范围,则修正回来。
        x = (k[0][0] < x && x < k[1][0]) ? x : (k[0][0] < x ? k[1][0] : k[0][0]);
        y = (k[0][1] < y && y < k[1][1]) ? y : (k[0][1] < y ? k[1][1] : k[0][1]);
        return {
            x: x,
            y: y
        };
    };
    this.windowyidong = (A, s, w) => {
        //移动悬浮窗的动画效果。
        w = w || window;
        s = s || 10;
        var sx = A[1][0] - A[0][0],
            sy = A[1][1] - A[0][1];
        var sd = this.weiyi(sx, sy) / s;
        var X = sx / sd,
            Y = sy / sd;
        var x = 0,
            y = 0;
        for (var i = 0; i < sd; i++) {
            x += X;
            y += Y;
            sleep(1);
            w.setPosition(A[0][0] + x, A[0][1] + y);
        };
        w.setPosition(A[1][0], A[1][1]);
    };
    this.OutScreen = () => {
        //算出最短的距离到达屏幕之外。
        var F = this.G(window);
        var x = window.getX(),
            y = window.getY();
        var sx = window.getX() + window.getWidth() / 2,
            sy = window.getY() + window.getHeight() / 2 + 66;
        var cx = Math.abs(sx < (this.Width - sx) ? sx : (this.Width - sx)) < Math.abs(sy < (this.Height - sy) ? sy : (this.Height - sy)) ? (sx < this.Width / 2 ? (F[0][0] - window.getWidth()) : (F[1][0] + window.getWidth())) : x,
            cy = Math.abs(sx < (this.Width - sx) ? sx : (this.Width - sx)) < Math.abs(sy < (this.Height - sy) ? sy : (this.Height - sy)) ? y : (sy < this.Height / 2 ? (F[0][1] - window.getHeight()) : (F[1][1] + window.getHeight()));
        return [
            [x, y],
            [cx, cy]
        ];
    };
    this.toScreenEdge = (d) => {
        //返回到屏幕边缘的坐标。d为浮点数0.1~1之间。
        d = d || 0;
        var F = this.G(window);
        var x = window.getX(),
            y = window.getY();
        var sw = window.getWidth() * d;
        var sx = window.getX() + window.getWidth() / 2,
            sy = window.getY() + window.getHeight() / 2 + 66;
        var cx = sx < (this.Width - sx) ? -sw : (this.Width + sw - window.getWidth() + 72);
        return [
            [x, y],
            [cx, y]
        ];
    };
    this.centerXY = (F) => {
        //返回距离中心位置的一个方形两个坐标。
        var w = window.getWidth();
        var h = window.getHeight();
        return [
            [F[0] + w / 2, F[1] + h / 2],
            [F[0] - w / 2, F[1] - h / 2]
        ];
    };
    this.IntScreen = () => {
        //当悬浮超出屏幕之外之后进入的坐标。
        var A = this.windowGXY(window.getX(), window.getY(), this.G(window));
        return [
            [window.getX(), window.getY()],
            [A.x, A.y]
        ];
    };
    this.ViewIntScreen = () => {
        //当悬浮超出屏幕之外之后进入的坐标。
        var A = this.windowGXY(window.getX(), window.getY(), this.G(window, windowid));
        return [
            [window.getX(), window.getY()],
            [A.x, A.y]
        ];
    };
    threads.start(new java.lang.Runnable(() => {
        this.windowyidong(this.IntScreen());
    }));
};


var window = floaty.window(
    <vertical bg="#ffeeeeee" padding="5" h="{{Math.floor(device.height*0.7)}}px">
        <frame>
            <text id="text" w="*" gravity="center" maxLines="1" ellipsize="end"/>
            <progressbar id="progress" w="*" h="auto" indeterminate="true" layout_gravity="top" style="@style/Base.Widget.AppCompat.ProgressBar.Horizontal"/>
        </frame>
        <frame layout_weight="1">
            <webview id="webview" w="*" h="*"/>
            <list id="list" w="90dp" h="*" bg="#346489" layout_gravity="right">
                <text w="*" h="50" text="{{txt}}" textSize="16sp" bg="#dddddd" margin="5" gravity="center"/>
            </list>
        </frame>
        <horizontal w="*">
            <button id="left"  text="上页" layout_weight="1"/>
            <button id="center"  text="菜单" layout_weight="1"/>
            <button id="right" text="下页" layout_weight="1"/>
        </horizontal>
        <button id="move"  text="移动及最小化" w="*"/>
    </vertical>
);
var window_ = floaty.window(
    <button id="but_" w="150px" h="150px" text="▽" alpha="0.7"/>
);

var ad = new 悬浮控制(window, window.move);
ad.isAutoIntScreen = false;
var ad_ = new 悬浮控制(window_, window_.but_);
ad.setLongClick(exit);
ad_.setLongClick(exit);
var F = ad.OutScreen();
var F_ = ad_.OutScreen();

threads.start(function() {
    sleep(100);
    F_ = ad_.OutScreen();
    ad_.windowyidong(F_);
});


ad.setClick(function() {
    window.disableFocus();
    threads.start(function() {
        F = ad.OutScreen();
        ad.windowyidong(F);
        ad_.windowyidong([F_[1], ad_.centerXY(ad.centerXY(F[0])[0])[1]]);
        ad_.windowyidong(ad_.IntScreen());
        ad_.windowyidong(ad_.toScreenEdge(0));
    });
});

ad_.setClick(function() {
    window.disableFocus();
    threads.start(function() {
        F_ = ad_.OutScreen();
        ad_.windowyidong(F_);
        ad.windowyidong([F[1], ad.centerXY(ad_.centerXY(F_[0])[0])[1]]);
        ad.windowyidong(ad.IntScreen());
    });
});


ui.run(() => {
    window.list.setVisibility(8);
});
var listArray = [{
        txt: "Auto.js官方文档",
        url: "https://hyb1996.github.io/AutoJs-Docs"
    },
    {
        txt: "百度",
        url: "http://www.baidu.com"
    },
    {
        url: "http://www.runoob.com/jsref/jsref-tutorial.html",
        txt: "javascript"
    },
    {
        url: "http://www.runoob.com/jsref/jsref-obj-array.html",
        txt: "array"
    },
    {
        url: "http://www.runoob.com/jsref/jsref-obj-boolean.html",
        txt: "boolean"
    },
    {
        url: "http://www.runoob.com/jsref/jsref-obj-date.html",
        txt: "date"
    },
    {
        url: "http://www.runoob.com/jsref/jsref-obj-math.html",
        txt: "math"
    },
    {
        url: "http://www.runoob.com/jsref/jsref-obj-number.html",
        txt: "number"
    },
    {
        url: "http://www.runoob.com/jsref/jsref-obj-regexp.html",
        txt: "regexp"
    },
    {
        url: "http://www.runoob.com/jsref/jsref-obj-string.html",
        txt: "string"
    }

];

window.list.setDataSource(listArray);


var url = "https://hyb1996.github.io/AutoJs-Docs";
//var url = "file:///storage/emulated/0/网页/试.html";
ui.run(() => {
    window.webview.loadUrl(url);
});
setInterval(() => {
    ui.run(() => {
        var P = window.webview.getProgress();
        var T = window.webview.getTitle();
        if (P == 100) {
            window.progress.setVisibility(8);
        } else {
            window.progress.setVisibility(0);
        };
        window.text.setText(String(T));
    });
}, 100);

/*
    if (window.webview.canGoBack()) {
            ui.run(() => {
                window.webview.goBack();
            });

};
*/
window.list.on("item_click", function(item, i) {
    ui.run(() => {
        window.webview.loadUrl(String(item.url));
        window.list.setVisibility(8);
    });

});


window.text.click(function(v) {
    var T = String(window.webview.getUrl());
    threads.start(function() {
        switch (dialogs.select("操作", ["刷新当前页面", "复制当前网址"])) {
            case 0:
                ui.run(() => {
                    window.webview.reload();
                });
                break;
            case 1:
                setClip(T);
                toast("已复制\n" + T);
                break;
        };
    });
});

window.left.click(function(v) {
    ui.run(() => {
        window.webview.goBack();
    });
});
window.center.click(function(v) {
    if (window.list.visibility == 8) {
        ui.run(() => {
            window.list.setVisibility(0);
        });
    } else {
        ui.run(() => {
            window.list.setVisibility(8);
        });

    };
});
window.right.click(function(v) {
    ui.run(() => {
        window.webview.goForward();
    });
});

说明

本文提供的代码仅供参考。
可能有些地方在最新版本的Auto.js上面需要做修改,才能运行。

Auto.js简介

Auto.js是利用安卓系统的“辅助功能”实现类似于按键精灵一样,可以通过代码模拟一系列界面动作的辅助工作。
与“按键精灵”不同的是,它的模拟动作并不是简单的使用在界面定坐标点来实现,而是类似与win一般,找窗口句柄来实现的。

Auto.js使用JavaScript作为脚本语言,目前使用Rhino 1.7.7.2作为脚本引擎,支持ES5与部分ES6特性。

开发文档

Auto.js Pro开发文档优化版
文档尚在完善中,可能有文档描述和代码实际行为有出入的情况。
模板、样式、generator来自Node.js。

为什么要使用Auto.js Pro开发脚本,有什么特点?

吸引我使用Auto.js Pro的原因有很多。最主要的几个原因是:

  • Auto.js Pro能开发免ROOT的安卓脚本
  • Auto.js Pro基于节点操作,能开发全分辨率的脚本,自动适配各种安卓机型
  • Auto.js Pro丰富的UI组件,能自定义各种样式的安卓界面
  • Auto.js Pro使用的javascript的语法比较优雅,代码可读性强
  • Auto.js Pro的命令库非常的丰富,接口比较多
  • Auto.js Pro脚本文件体积比较小。1000行的代码,打包后的apk文件只有3-5M,还没有广告

相关教程

Auto.js Pro安卓全分辨率免ROOT引流脚本开发教程

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值