|
|
°²×¿ÊÖ»ú½Å±¾¶àÖÖµ¹¼ÆÊ±ÏÔʾ·½Ê½
- /*
- µ¹¼ÆÊ±¹¤¾ßº¯Êý - ¿ÉÖ±½ÓÔÚÆäËû½Å±¾Öе÷ÓÃ
- Ìṩ¶àÖÖµ¹¼ÆÊ±ÏÔʾ·½Ê½
- //🍎½»Á÷ 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[0]);
- var targetMin = parseInt(parts[1]);
- var targetSec = parts.length > 2 ? parseInt(parts[2]) : 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("🎯 Ä¿±êʱ¼ä£º" + 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("💡 Ìáʾ£ºÈ¡Ïû×¢ÊÍÉÏÃæµÄʾÀý´úÂëÀ´Ê¹ÓöÔÓ¦¹¦ÄÜ");
- }
¸´ÖÆ´úÂë
|
|