floatUI自定义极度美化悬浮窗

78

主题

190

回帖

861

积分

管理员

积分
861
一个使用JavaScript编写的Android自动化脚本,用于在Android设备上创建并控制一个悬浮窗。这个脚本假设在类似于Auto.js这样的自动化IDE中运行,并且使用了一些特定的API来实现悬浮窗的功能。以下是对代码的详细解释:
  • 构造函数定义:首先定义了一个名为悬浮窗的构造函数。这并不是标准的JavaScript语法,正确的构造函数定义应该是function 悬浮窗() {}。脚本中使用了function 悬浮窗()(),这看起来像是某种自执行函数的形式,但实际上在标准JavaScript中应该是new 悬浮窗()来实例化对象。
  • 创建悬浮窗实例:使用new 悬浮窗()创建了一个悬浮窗类的实例,并将其赋值给变量float。
  • 全局变量:定义了一个全局变量全局_停止,用于控制脚本的停止状态。当这个变量为true时,意味着脚本需要停止执行。
  • 添加create方法:为悬浮窗构造函数的原型添加了一个create方法,该方法用于创建悬浮窗界面。在这个方法中,首先创建了一个floatUI实例,floatUI是用于创建浮窗界面的类。
  • 屏幕高度获取:尝试获取设备的实际屏幕高度。如果获取失败,则使用默认值1920像素。这里使用了context.getResources().getDisplayMetrics().heightPixels来获取屏幕高度。
  • 定义悬浮窗高度:定义了两个高度变量,收起高度和展开高度,分别表示悬浮窗在收起和展开状态下的高度。
  • 加载XML布局:使用fui.loadXML()方法加载一个XML布局字符串,该布局定义了悬浮窗的外观和结构。布局中包含了一个展开按钮和一个垂直排列的功能按钮区,初始状态下功能按钮区是不可见的。
  • 保存UI实例:将创建的floatUI实例赋值给当前悬浮窗实例的ui属性,以便后续的操作可以访问到这个布局。
  • 设置初始位置:使用this.setPos(0, this.screenHeight - 收起高度);将悬浮窗的位置设置在屏幕底部,只露出展开按钮。
  • 获取UI元素:通过fui.findViewById()方法获取XML布局中各个UI元素的引用,比如展开按钮、开始按钮等。
  • 初始化按钮事件:为各个按钮设置点击事件监听器。例如,当点击展开按钮时,会检查下方的功能按钮区是否可见,如果可见则收起,否则展开。开始按钮和停止按钮用于控制全局的停止变量,从而决定脚本的执行状态。设置按钮用于加载并显示主界面。更新按钮目前仅打印“更新”的信息,没有实际的功能实现。关闭按钮则调用this.close()方法关闭悬浮窗。
  • 关闭悬浮窗:定义了一个close方法,调用floatUI对象的close方法关闭悬浮窗。
  • 设置悬浮窗位置:定义了一个setPos方法,用于设置悬浮窗的位置。该方法调用floatUI对象的setPosition方法来实现。
  • 启动悬浮窗:最后,调用float.create()方法来启动悬浮窗,创建并显示界面。
这段代码主要是为了在Android设备上创建一个带有展开/收起功能的悬浮窗,并为其各个按钮添加事件监听器,以便用户可以通过点击悬浮窗上的按钮来实现相应的操作。
  1. /*
  2.    欢迎使用AiWork安卓自动化IDE
  3.    帮助文档: http://help.autoapp.net.cn
  4.    官方地址: www.aiwork24.com
  5.    qq群: 743723025
  6. */
  7. // 定义一个名为悬浮窗的构造函数
  8. function 悬浮窗() {
  9. }

  10. // 创建一个悬浮窗实例
  11. var float = new 悬浮窗()
  12. // 定义一个全局变量用于控制停止状态
  13. var 全局_停止 = false

  14. // 为悬浮窗构造函数的原型添加create方法,用于创建悬浮窗界面
  15. 悬浮窗.prototype.create = function () {
  16.     // 创建一个floatUI实例,floatUI是一个用于创建浮窗界面的类
  17.     var fui = new floatUI()
  18.     // 设置默认屏幕高度为1920像素
  19.     this.screenHeight = 1920 // 默认值
  20.    
  21.     // 尝试获取实际屏幕高度
  22.     try {
  23.         // 使用context对象获取屏幕显示信息,并从中获取屏幕高度像素值
  24.         this.screenHeight = context.getResources().getDisplayMetrics().heightPixels;
  25.     } catch(e) {
  26.         // 如果获取屏幕高度失败,则打印错误信息并使用默认值
  27.         printl("获取屏幕高度失败,使用默认值: " + e);
  28.     }
  29.    
  30.     // 定义收起和展开时悬浮窗的高度
  31.     var 收起高度 = 45;  // 只显示展开按钮的高度
  32.     var 展开高度 = 250; // 完全展开的高度
  33.    
  34.     // 加载XML布局,创建悬浮窗界面
  35.     fui.loadXML(`
  36.       <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  37.          android:layout_width="wrap_content"
  38.          android:layout_height="wrap_content"
  39.          android:background="#00000000"
  40.          android:alpha="0.8"
  41.          android:background="@drawable/lin_border_radius"
  42.          android:orientation="vertical">
  43.          
  44.         <!-- 展开按钮 -->
  45.         <ImageButton
  46.             android:id="button_open"
  47.             android:layout_width="35dp"
  48.             android:layout_height="35dp"
  49.             android:src="资源/展开.png"
  50.             android:scaleType="fitXY"
  51.             android:background="#00000000"
  52.             android:padding="5dp"/>
  53.             
  54.         <!-- 功能按钮区 -->
  55.         <LinearLayout
  56.             android:id="content_layout"
  57.             android:layout_width="wrap_content"
  58.             android:layout_height="wrap_content"
  59.             android:orientation="vertical"
  60.             android:visibility="gone">
  61.             
  62.             <ImageButton
  63.                 android:id="button_start"
  64.                 android:layout_width="35dp"
  65.                 android:layout_height="35dp"
  66.                 android:src="资源/开始.png"
  67.                 android:scaleType="fitXY"
  68.                 android:padding="5dp"
  69.                 android:background="#00000000"/>
  70.                
  71.             <ImageButton
  72.                 android:id="button_stop"
  73.                 android:layout_width="35dp"
  74.                 android:layout_height="35dp"
  75.                 android:src="资源/停止.png"
  76.                 android:scaleType="fitXY"
  77.                 android:padding="5dp"
  78.                 android:background="#00000000"/>
  79.                
  80.             <ImageButton
  81.                 android:id="button_setting"
  82.                 android:layout_width="35dp"
  83.                 android:layout_height="35dp"
  84.                 android:src="资源/设置.png"
  85.                 android:scaleType="fitXY"
  86.                 android:padding="5dp"
  87.                 android:background="#00000000"/>
  88.                
  89.             <ImageButton
  90.                 android:id="button_updata"
  91.                 android:layout_width="35dp"
  92.                 android:layout_height="35dp"
  93.                 android:src="资源/下载.png"
  94.                 android:scaleType="fitXY"
  95.                 android:padding="5dp"
  96.                 android:background="#00000000"/>
  97.                
  98.             <ImageButton
  99.                 android:id="button_close"
  100.                 android:layout_width="35dp"
  101.                 android:layout_height="35dp"
  102.                 android:src="资源/关闭.png"
  103.                 android:scaleType="fitXY"
  104.                 android:padding="5dp"
  105.                 android:background="#00000000"/>
  106.         </LinearLayout>
  107.       </LinearLayout>
  108.     `);
  109.    
  110.     // 将创建的floatUI实例赋值给当前悬浮窗实例的ui属性
  111.     float.ui = fui;
  112.    
  113.     // 设置初始位置:屏幕底部只露出展开按钮
  114.     this.setPos(0, this.screenHeight - 收起高度);
  115.    
  116.     // 获取XML布局中的各个UI元素
  117.     this.btn_open = fui.findViewById("button_open");
  118.     this.content_layout = fui.findViewById("content_layout");
  119.     this.btn_start = fui.findViewById("button_start");
  120.     this.btn_stop = fui.findViewById("button_stop");
  121.     this.btn_setting = fui.findViewById("button_setting");
  122.     this.btn_updata = fui.findViewById("button_updata");
  123.     this.btn_close = fui.findViewById("button_close");
  124.    
  125.     // 初始化按钮事件
  126.     this.initButtons();
  127. };

  128. // 初始化按钮事件的方法,为各个按钮设置点击事件监听器
  129. 悬浮窗.prototype.initButtons = function() {
  130.     var self = this;
  131.    
  132.     // 设置展开/收起按钮的点击事件
  133.     this.btn_open.setOnClickListener(function() {
  134.         if (self.content_layout.getVisibility() === View.VISIBLE) {
  135.             // 如果当前是展开状态,则收起
  136.             self.setPos(0, self.screenHeight - 44);
  137.             sleep(100); // 等待100毫秒
  138.             self.content_layout.setVisibility(View.GONE);
  139.         } else {
  140.             // 如果当前是收起状态,则展开
  141.             self.content_layout.setVisibility(View.VISIBLE);
  142.             self.setPos(0, self.screenHeight - 250);
  143.         }
  144.     });
  145.    
  146.     // 设置开始按钮的点击事件
  147.     this.btn_start.setOnClickListener(function() {
  148.         printl("启动");
  149.         全局_停止 = false;
  150.     });
  151.    
  152.     // 设置停止按钮的点击事件
  153.     this.btn_stop.setOnClickListener(function() {
  154.         printl("停止");
  155.         全局_停止 = true;
  156.     });
  157.    
  158.     // 设置设置按钮的点击事件
  159.     this.btn_setting.setOnClickListener(function() {
  160.         printl("设置");
  161.         // 加载并显示主界面
  162.         var win = window.loadUI("主界面.ui");
  163.         win.show();
  164.     });
  165.    
  166.     // 设置更新按钮的点击事件
  167.     this.btn_updata.setOnClickListener(function() {
  168.         printl("更新");
  169.     });
  170.    
  171.     // 设置关闭按钮的点击事件
  172.     this.btn_close.setOnClickListener(function() {
  173.         printl("关闭");
  174.         self.close();
  175.     });
  176. };

  177. // 定义关闭悬浮窗的方法
  178. 悬浮窗.prototype.close = function() {
  179.     // 调用floatUI对象的close方法关闭悬浮窗
  180.     this.ui.close();
  181. };

  182. // 定义设置悬浮窗位置的方法
  183. 悬浮窗.prototype.setPos = function(x, y) {
  184.     // 调用floatUI对象的setPosition方法设置悬浮窗的位置
  185.     this.ui.setPosition(x, y);
  186. };

  187. // 启动悬浮窗,调用create方法
  188. float.create();
复制代码
3.png 4.png 5.png


上一篇: AIWROK软件agent代理模式ADB方法集合

下一篇: 没有了

举报 回复