YYPOST群发软件 发表于 2025-4-22 08:18:27

floatUI自定义极度美化悬浮窗

/*
   欢迎使用AiWork安卓自动化IDE
   帮助文档: http://help.autoapp.net.cn
   官方地址: www.aiwork24.com
   qq群: 743723025
*/
// 定义一个名为悬浮窗的构造函数
function 悬浮窗() {
}

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

// 为悬浮窗构造函数的原型添加create方法,用于创建悬浮窗界面
悬浮窗.prototype.create = function () {
    // 创建一个floatUI实例,floatUI是一个用于创建浮窗界面的类
    var fui = new floatUI()
    // 设置默认屏幕高度为1920像素
    this.screenHeight = 1920 // 默认值
   
    // 尝试获取实际屏幕高度
    try {
      // 使用context对象获取屏幕显示信息,并从中获取屏幕高度像素值
      this.screenHeight = context.getResources().getDisplayMetrics().heightPixels;
    } catch(e) {
      // 如果获取屏幕高度失败,则打印错误信息并使用默认值
      printl("获取屏幕高度失败,使用默认值: " + e);
    }
   
    // 定义收起和展开时悬浮窗的高度
    var 收起高度 = 45;// 只显示展开按钮的高度
    var 展开高度 = 250; // 完全展开的高度
   
    // 加载XML布局,创建悬浮窗界面
    fui.loadXML(`
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:background="#00000000"
         android:alpha="0.8"
         android:background="@drawable/lin_border_radius"
         android:orientation="vertical">
         
      <!-- 展开按钮 -->
      <ImageButton
            android:id="button_open"
            android:layout_width="35dp"
            android:layout_height="35dp"
            android:src="资源/展开.png"
            android:scaleType="fitXY"
            android:background="#00000000"
            android:padding="5dp"/>
            
      <!-- 功能按钮区 -->
      <LinearLayout
            android:id="content_layout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:visibility="gone">
            
            <ImageButton
                android:id="button_start"
                android:layout_width="35dp"
                android:layout_height="35dp"
                android:src="资源/开始.png"
                android:scaleType="fitXY"
                android:padding="5dp"
                android:background="#00000000"/>
               
            <ImageButton
                android:id="button_stop"
                android:layout_width="35dp"
                android:layout_height="35dp"
                android:src="资源/停止.png"
                android:scaleType="fitXY"
                android:padding="5dp"
                android:background="#00000000"/>
               
            <ImageButton
                android:id="button_setting"
                android:layout_width="35dp"
                android:layout_height="35dp"
                android:src="资源/设置.png"
                android:scaleType="fitXY"
                android:padding="5dp"
                android:background="#00000000"/>
               
            <ImageButton
                android:id="button_updata"
                android:layout_width="35dp"
                android:layout_height="35dp"
                android:src="资源/下载.png"
                android:scaleType="fitXY"
                android:padding="5dp"
                android:background="#00000000"/>
               
            <ImageButton
                android:id="button_close"
                android:layout_width="35dp"
                android:layout_height="35dp"
                android:src="资源/关闭.png"
                android:scaleType="fitXY"
                android:padding="5dp"
                android:background="#00000000"/>
      </LinearLayout>
      </LinearLayout>
    `);
   
    // 将创建的floatUI实例赋值给当前悬浮窗实例的ui属性
    float.ui = fui;
   
    // 设置初始位置:屏幕底部只露出展开按钮
    this.setPos(0, this.screenHeight - 收起高度);
   
    // 获取XML布局中的各个UI元素
    this.btn_open = fui.findViewById("button_open");
    this.content_layout = fui.findViewById("content_layout");
    this.btn_start = fui.findViewById("button_start");
    this.btn_stop = fui.findViewById("button_stop");
    this.btn_setting = fui.findViewById("button_setting");
    this.btn_updata = fui.findViewById("button_updata");
    this.btn_close = fui.findViewById("button_close");
   
    // 初始化按钮事件
    this.initButtons();
};

// 初始化按钮事件的方法,为各个按钮设置点击事件监听器
悬浮窗.prototype.initButtons = function() {
    var self = this;
   
    // 设置展开/收起按钮的点击事件
    this.btn_open.setOnClickListener(function() {
      if (self.content_layout.getVisibility() === View.VISIBLE) {
            // 如果当前是展开状态,则收起
            self.setPos(0, self.screenHeight - 44);
            sleep(100); // 等待100毫秒
            self.content_layout.setVisibility(View.GONE);
      } else {
            // 如果当前是收起状态,则展开
            self.content_layout.setVisibility(View.VISIBLE);
            self.setPos(0, self.screenHeight - 250);
      }
    });
   
    // 设置开始按钮的点击事件
    this.btn_start.setOnClickListener(function() {
      printl("启动");
      全局_停止 = false;
    });
   
    // 设置停止按钮的点击事件
    this.btn_stop.setOnClickListener(function() {
      printl("停止");
      全局_停止 = true;
    });
   
    // 设置设置按钮的点击事件
    this.btn_setting.setOnClickListener(function() {
      printl("设置");
      // 加载并显示主界面
      var win = window.loadUI("主界面.ui");
      win.show();
    });
   
    // 设置更新按钮的点击事件
    this.btn_updata.setOnClickListener(function() {
      printl("更新");
    });
   
    // 设置关闭按钮的点击事件
    this.btn_close.setOnClickListener(function() {
      printl("关闭");
      self.close();
    });
};

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

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

// 启动悬浮窗,调用create方法
float.create();



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










页: [1]
查看完整版本: floatUI自定义极度美化悬浮窗