YYPOST群发软件 发表于 2026-3-25 10:10:40

安卓手机脚本多种倒计时显示方式

安卓手机脚本多种倒计时显示方式




/*
   倒计时工具函数 - 可直接在其他脚本中调用
   提供多种倒计时显示方式
   //🍎交流 QQ 群 711841924 群一,苹果内测群,528816639
*/

/**
* 格式化时间函数(内部使用)
* @param {number} secs - 秒数
* @returns {string} 格式化后的时间字符串
*/
function formatTime(secs) {
    var h = Math.floor(secs / 3600);
    var m = Math.floor((secs % 3600) / 60);
    var s = secs % 60;
   
    var str = "";
    if (h > 0) str += h + "时";
    if (m > 0 || h > 0) str += m + "分";
    str += s + "秒";
    return str;
}

/**
* 在固定位置显示倒计时(悬浮窗方式)
* @param {number} seconds - 倒计时秒数
* @param {object} options - 配置选项
*/
function showCountdownFloat(seconds, options) {
    options = options || {};
    var config = {
      x: options.x || (screen.getScreenWidth() - 400) / 2,// X 坐标
      y: options.y || (screen.getScreenHeight() - 150),   // Y 坐标
      width: options.width || 400,                        // 宽度
      height: options.height || 60,                         // 高度
      textColor: options.textColor || '#00ff00',            // 文字颜色
      bgColor: options.bgColor || '#000000',                // 背景颜色
      textSize: options.textSize || 24                      // 字号
    };
   
    // 创建悬浮窗
    var countdownUI = new floatUI();
    countdownUI.loadXML(
      '<LinearLayout orientation="vertical" w="' + config.width + '" h="' + config.height + '" gravity="center">' +
      '<TextView id="countdownText" textColor="' + config.textColor + '" background="' + config.bgColor + '" layout_width="wrap_content" layout_height="wrap_content" textSize="' + config.textSize + 'sp" />' +
      '</LinearLayout>'
    );
   
    var countdownText = countdownUI.findViewById('countdownText');
   
    if (countdownText) {
      // 设置位置
      setTimeout(function() {
            countdownUI.setPosition(config.x, config.y);
      }, 100);
      
      // 开始倒计时
      var remaining = seconds;
      countdownText.setText(formatTime(remaining));
      
      var timer = setInterval(function() {
            remaining--;
            
            if (remaining > 0) {
                countdownText.setText(formatTime(remaining));
               
                // 最后 10 秒变红
                if (remaining <= 10) {
                  countdownText.setTextColor(android.graphics.Color.RED);
                }
            } else {
                clearInterval(timer);
                countdownText.setText("时间到!");
                countdownText.setTextColor(android.graphics.Color.YELLOW);
                toast.show("倒计时结束!");
            }
      }, 1000);
      
      return {
            stop: function() {
                clearInterval(timer);
            },
            update: function(newSeconds) {
                remaining = newSeconds;
            }
      };
    }
   
    return null;
}

/**
* 使用 Toast 显示倒计时(简单方式)
* @param {number} seconds - 倒计时秒数
*/
function showCountdownToast(seconds) {
    var remaining = seconds;
   
    function formatTime(secs) {
      var h = Math.floor(secs / 3600);
      var m = Math.floor((secs % 3600) / 60);
      var s = secs % 60;
      return h + "时" + m + "分" + s + "秒";
    }
   
    var interval = setInterval(function() {
      remaining--;
      
      if (remaining > 0) {
            toast.show("⏱️ 剩余:" + formatTime(remaining));
      } else {
            clearInterval(interval);
            toast.show("✅ 时间到!");
      }
    }, 1000);
}

/**
* 在日志中显示倒计时
* @param {number} seconds - 倒计时秒数
* @param {string} prefix - 日志前缀
*/
function showCountdownLog(seconds, prefix) {
    prefix = prefix || "⏱️";
    var remaining = seconds;
   
    function formatTime(secs) {
      var h = Math.floor(secs / 3600);
      var m = Math.floor((secs % 3600) / 60);
      var s = secs % 60;
      
      var str = "";
      if (h > 0) str += h + "时";
      if (m > 0 || h > 0) str += m + "分";
      str += s + "秒";
      return str;
    }
   
    printl(prefix + " 倒计时开始:" + formatTime(remaining));
   
    var timer = setInterval(function() {
      remaining--;
      
      if (remaining > 0) {
            if (remaining % 5 === 0) { // 每 5 秒打印一次
                printl(prefix + " 剩余:" + formatTime(remaining));
            }
      } else {
            clearInterval(timer);
            printl(prefix + " ⏰ 时间到!");
      }
    }, 1000);
}

/**
* 倒计时到指定时间点
* @param {string} targetTime - 目标时间,格式 'HH:mm:ss' 或 'HH:mm'
*/
function countdownTo(targetTime) {
    var now = new Date();
    var parts = targetTime.split(':');
    var targetHour = parseInt(parts);
    var targetMin = parseInt(parts);
    var targetSec = parts.length > 2 ? parseInt(parts) : 0;
   
    var target = new Date();
    target.setHours(targetHour, targetMin, targetSec, 0);
   
    // 如果目标时间已过,设为明天
    if (target <= now) {
      target.setDate(target.getDate() + 1);
    }
   
    var diffMs = target - now;
    var diffSeconds = Math.floor(diffMs / 1000);
   
    printl("&#127919; 目标时间:" + targetTime);
    printl("⏱️ 剩余时间:" + diffSeconds + "秒");
   
    // 显示倒计时悬浮窗
    return showCountdownFloat(diffSeconds, {
      y: screen.getScreenHeight() - 200
    });
}

// 导出函数供外部使用
this.showCountdownFloat = showCountdownFloat;
this.showCountdownToast = showCountdownToast;
this.showCountdownLog = showCountdownLog;
this.countdownTo = countdownTo;

// 使用示例
if (typeof main !== 'function') {
    printl("\n========== 倒计时工具函数使用示例 ==========\n");
   
    // 示例 1: 简单的 30 秒倒计时(悬浮窗)
    printl("示例 1: 30 秒倒计时悬浮窗");
    showCountdownFloat(30, { y: screen.getScreenHeight() - 150 });
   
    // 示例 2: 使用 Toast 显示倒计时
    printl("示例 2: Toast 倒计时");
    showCountdownToast(10);
   
    // 示例 3: 在日志中显示倒计时
    printl("示例 3: 日志倒计时");
    showCountdownLog(20, "⏱️");
   
    // 示例 4: 倒计时到指定时间
    printl("示例 4: 倒计时到 18:30:00");
    countdownTo("18:30:00");
   
    printl("\n============================================\n");
    printl("&#128161; 提示:取消注释上面的示例代码来使用对应功能");
}




页: [1]
查看完整版本: 安卓手机脚本多种倒计时显示方式