鸿蒙-应用内悬浮窗

//悬浮窗工具类

import { window } from '@kit.ArkUI';
import { BusinessError } from '@kit.BasicServicesKit';
import { Logger } from '@mbbase/common-ui';
import * as FloatedWindowPage from './FloatedWindowPage'; // 导入命名路由页面

const TAG = '[FloatedWindowUtils]';

export interface FloatedWindowParams {
  width: number;
  height: number;
  x: number;
  y: number;
  backgroundColor?: string;
}

export class FloatedWindowUtils {
  public static showSubWindow(windowStage: window.WindowStage | undefined,
    subWindowParams: FloatedWindowParams) {
    if (!windowStage) {
      Logger.error(TAG, `windowStage is undefined.`);
      return;
    }

    windowStage.createSubWindow(FloatedWindowPage.subWindowName, (err, subWindow) => {
      try {
        subWindow.loadContentByName(FloatedWindowPage.entryName, (err: BusinessError) => {
          if (err.code) {
            Logger.error(TAG, `Failed to load the content. Cause: ${err.message}.`);
            return;
          }
          Logger.info('Succeeded in loading the content.');
          subWindow.setWindowBackgroundColor(subWindowParams.backgroundColor ?? '#00ffffff');
          subWindow.moveWindowTo(subWindowParams.x, subWindowParams.y);
          subWindow.resize(subWindowParams.width, subWindowParams.height)
          subWindow.setWindowTouchable(true);
          subWindow.showWindow();
          // subWindow.setWindowBackgroundColor(Color.Transparent.toString());
          subWindow.setWindowLayoutFullScreen(true);
        })
      } catch (err) {
        Logger.error('failed to create subWindow Cause:' + err);
      }
    })
  }

  public static async getFloatedWindow(): Promise<window.Window | undefined> {
    let windowStage = await AppStorage.get<window.WindowStage>('windowStage');
    if (!windowStage) {
      return undefined;
    }

    let subList = await windowStage.getSubWindow()


    for (let i = 0; i < subList.length; i++) {
      let aa: window.WindowProperties = subList[i].getWindowProperties()
      // if (aa.name == 'FloatedWindow') {
      return subList[i];
      // }
    }
    return undefined;
  }


  public static async destroySubWindow() {
    try {
      let subWindow: window.Window = await window.findWindow(FloatedWindowPage.subWindowName)
      if (!subWindow) {
        Logger.info('subWindow is undefined.');
        return;
      }
      subWindow.destroyWindow((err: BusinessError) => {
        if (err.code) {
          Logger.error(TAG, `Failed to destroy the window. Cause: ${err.message}.`);
          return;
        }
        AppStorage.set<window.Window>('subWindow', undefined);
        Logger.info('Succeeded in destroying the window.');
      });
    } catch (err) {
      Logger.error('Find subWindow failed. Cause:' + err);
    }
  }

  public static async moveSubWindow(x: number, y: number) {
    try {
      let subWindow: window.Window = window.findWindow(FloatedWindowPage.subWindowName)
      if (!subWindow) {
        Logger.info('subWindow is undefined.');
        return;
      }

      subWindow.moveWindowTo(x, y, (err: BusinessError) => {
        if (err.code) {
          Logger.error(TAG, `Failed to move the window. Cause: ${err.message}.`);
          return;
        }
        Logger.info('Succeeded in moving the window.', x, y);
      });
    } catch (err) {
      Logger.error('Find subWindow failed. Cause:' + err);
    }
  }

  public static async resizeSubWindow(width: number, height: number) {
    try {
      let subWindow: window.Window = window.findWindow(FloatedWindowPage.subWindowName)
      if (!subWindow) {
        Logger.info('subWindow is undefined.');
        return;
      }
      subWindow.resize(vp2px(width), vp2px(height), (err: BusinessError) => {
        if (err.code) {
          Logger.error(TAG, `Failed to change the window size. Cause: ${err.message}.`);
          return;
        }
        Logger.info('Succeeded in changing the window size.');
      })
    } catch (err) {
      Logger.error('Find subWindow failed. Cause:' + err);
    }
  }
}

 

//悬浮窗页面

import { display, window } from '@kit.ArkUI'
import { MBRouter } from '@mbbase/router'

export const entryName: string = 'FloatedWindowName';

export const subWindowName: string = 'FloatedWindow';

@Entry({ routeName: entryName })
@Component
export struct FloatedWindowPage {
  @State subWindow: window.Window = window.findWindow(subWindowName)
  @State @Watch('moveWindow') windowPosition: WindowPosition = {
    x: 40,
    y: 800
  }

  moveWindow() {
    this.subWindow.moveWindowTo(this.windowPosition.x, this.windowPosition.y);
  }

  build() {
    Row() {
      Text('悬浮窗')
        .fontColor(Color.Red)
        .fontWeight(800)
        .onClick(() => {
          // MBRouter.push({ url: "ymm://test/debug" })
        })
    }.width('100%').height('100%')
    .gesture(
      PanGesture()
        .onActionStart(() => {
        })
        .onActionUpdate((event: GestureEvent) => {
          this.windowPosition.x += event.offsetX;
          this.windowPosition.y += event.offsetY;
          let top = 80;
          let bottom =
            display.getDefaultDisplaySync().height - this.subWindow.getWindowProperties().windowRect.height
              - top;
          if (this.windowPosition.y < top) {
            this.windowPosition.y = top;
          } else if (this.windowPosition.y > bottom) {
            this.windowPosition.y = bottom;
          }
          this.subWindow.moveWindowTo(this.windowPosition.x, this.windowPosition.y);
        })
        .onActionEnd((event: GestureEvent) => {
          let rect = this.subWindow.getWindowProperties().windowRect;

          if (this.windowPosition.x + rect.width / 2 >= display.getDefaultDisplaySync().width / 2) {
            this.windowPosition.x = display.getDefaultDisplaySync().width - rect.width;
          } else if (event.offsetX < display.getDefaultDisplaySync().width / 2) {
            this.windowPosition.x = 0;
          }
          let top = 80;
          let bottom =
            display.getDefaultDisplaySync().height - rect.height
              - top;
          if (this.windowPosition.y < top) {
            this.windowPosition.y = top;
          } else if (this.windowPosition.y > bottom) {
            this.windowPosition.y = bottom;
          }
          this.subWindow.moveWindowTo(this.windowPosition.x, this.windowPosition.y);
        })
    )
  }
}

export interface WindowPosition {
  x: number,
  y: number
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/64452.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【C++】小乐乐求和题目分析n变量类型讨论

博客主页&#xff1a; [小ᶻ☡꙳ᵃⁱᵍᶜ꙳] 本文专栏: C 文章目录 &#x1f4af;前言&#x1f4af;题目描述&#x1f4af;解题分析&#x1f4af;为什么 n 需要是 long long问题重点&#xff1a;中间计算水平上的数据类型不足的例子&#xff1a;正确解决&#xff1a;将 n 设…

计算机基础知识——数据结构与算法(一)(山东省大数据职称考试)

大数据分析应用-初级 第一部分 基础知识 一、大数据法律法规、政策文件、相关标准 二、计算机基础知识 三、信息化基础知识 四、密码学 五、大数据安全 六、数据库系统 七、数据仓库. 第二部分 专业知识 一、大数据技术与应用 二、大数据分析模型 三、数据科学 数据结构与算法…

WebView通过@JavascriptInterface 调用原生方法

1. 创建 WebView 和设置 WebView 设置 在 XML 布局中添加 WebView 在activity_main.xml里创建一个WebView控件 <?xml version"1.0" encoding"utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android"http://schem…

python:用 sklearn.metrics 评价 K-Means 聚类模型

sklearn 的 metrics 模块提供的聚类模型评价指标如下&#xff1a; ARI 评价法&#xff08;兰德系数&#xff09;: adjusted_rand_score AMI 评价法&#xff08;相互信息&#xff09;: adjusted_mutual_info_score V-measure 评分 : completeness_score FMI 评价法 : fowlkes_m…

基于AI对话生成剧情AVG游戏

游戏开发这个领域&#xff0c;一直有较高的学习门槛。作为一个非专业的游戏爱好者&#xff0c;如果想要开发游戏&#xff0c;往往受制于游戏引擎的专业程度&#xff0c;难以完成复杂的游戏项目。 AI IDE的诞生&#xff0c;提供了另外的一种思路&#xff0c;即通过AI 生成项目及…

mac电脑可以使用的模拟器

BlueStacks Air 推荐-》亲测可用 BlueStacks Air https://www.bluestacks.com 支持macOS/Windows&#xff0c;刚新增了对Apple Silicon系列M芯片的支持 GameLoop https://www.gameloop.com/ 支持 macOS/Windows Genymotion https://www.genymotion.com/ 支持Android/macO…

qt 类中的run线程

在Qt中&#xff0c;QThread类的run()方法是线程的执行入口&#xff0c;它是由QThread内部自动调用的&#xff0c;而不是用户直接调用。 详细解释&#xff1a; QThread类&#xff1a; QThread是Qt的线程类&#xff0c;提供了用于多线程操作的接口。我们可以创建QThread对象并将…

ElasticSearch 数据聚合与运算

1、数据聚合 聚合&#xff08;aggregations&#xff09;可以让我们极其方便的实现数据的统计、分析和运算。实现这些统计功能的比数据库的 SQL 要方便的多&#xff0c;而且查询速度非常快&#xff0c;可以实现近实时搜索效果。 注意&#xff1a; 参加聚合的字段必须是 keywor…

人工智能浪潮来袭:2024年技术革命与产业变革深度解析@附64页PDF文件下载

随着2024年的到来&#xff0c;人工智能&#xff08;AI&#xff09;技术正以前所未有的速度、广度和深度改变着我们的生产和生活方式。在这篇深度解析中&#xff0c;我们将带您一探AI技术的最新发展、产业应用的现状以及未来的安全治理趋势。 技术革命&#xff1a;AI技术的新范…

python 渗透测试开发工具之 子域名查询 python脚本逻辑 开发 高阶逻辑思维 CDN解析流程细分到信息收集的域名以及子域名分析

目录 知识点说明 域名收集是信息收集的一部分 而域名分为主域名以及子域名 所以 最后 域名说明 前提 解释域名域IP得作用 DNS解析流程【必控知识点】 解析总结 域名是什么 域名得分段解释 www.baidu.com【必控知识点】 从技术角度来看 总结: 信息收集子域名…

搭建分布式ZooKeeper集群

title: 搭建分布式ZooKeeper集群 date: 2024-11-26 23:39:00 categories: - 服务器 tags: - ZooKeeper - 大数据搭建分布式ZooKeeper集群 本次实验环境&#xff1a;Centos 7-2009、Hadoop-3.1.4、JDK 8、Zookeeper-3.6.3 前提 首先配置好Hadoop集群 为了配置方便&#xff0c…

Python高级游戏开发:打造一款塔防游戏

塔防(Tower Defense)游戏是一种经典的游戏类型,玩家通过建造防御塔来阻止敌人入侵。本文将介绍如何使用Python和Pygame库开发一款简单但高级的塔防游戏,涵盖地图设计、敌人路径规划、防御塔机制以及游戏逻辑实现。 一、游戏开发环境配置 1. 安装Pygame Pygame是Python中最…

unity 雷达

unity 雷达 首先去商店下载TouchScript插件 导入的时候勾选Enable TUIO 然后把预制体Cursors和TouchManager拖上 最后把TuioInput这个脚本挂上 脚本上的端口号尽量不改

F5中获取客户端ip地址(client ip)

当F5设备对其原始设置上的所有IP地址使用NAT时&#xff0c;连接到poo成员&#xff08;nodes、backend servers&#xff09;的出站连接将是NAT IP地址。 pool 成员&#xff08;nodes、backend servers&#xff09;将无法看到真实的客户端 ip地址&#xff0c;因为看到的是F5上的…

白话java设计模式

创建模式 单例模式&#xff08;Singleton Pattern&#xff09;&#xff1a; 就是一次创建多次使用&#xff0c;它的对象不会重复创建&#xff0c;可以全局来共享状态。 工厂模式&#xff08;Factory Method Pattern&#xff09;&#xff1a; 可以通过接口来进行实例化创建&a…

MATLAB引用矩阵元素的几种方法

引用矩阵元素可以通过索引&#xff0c;也可以通过逻辑值 索引 通过引用元素在矩阵中的位置来提取元素&#xff0c;例如&#xff1a; - 逻辑值 通过某种逻辑运算来使得要提取的值变为逻辑 1 1 1&#xff0c;用 A ( ) A() A()提取即可&#xff0c; A A A为原矩阵的名称。 例如&…

Python发送带key的kafka消息

在Python中发送带有键&#xff08;key&#xff09;的Kafka消息&#xff0c;通常会使用confluent-kafka或kafka-python这样的库。这里我将分别展示如何使用这两个库来实现这个功能。 ### 使用 confluent-kafka 首先&#xff0c;确保你已经安装了confluent-kafka库。如果没有安装…

机器学习预处理-表格数据的空值处理

机器学习预处理-表格数据的空值处理 机器学习预处理-表格数据的分析与可视化中详细介绍了表格数据的python可视化&#xff0c;可视化能够帮助我们了解数据的构成和分布&#xff0c;是我们进行机器学习的必备步骤。上文中也提及&#xff0c;原始的数据存在部分的缺失&#xff0…

了解 SpringMVC 请求流程

文章目录 1. Spring 基础 - SpringMVC 请求流程1.1 引入1.2 什么是 MVC1.3 什么是 Spring MVC1.4 请求流程核心架构的具体流程步骤补充 1.5 案例**Maven 包引入****业务代码的编写**DaoServiceControllerwebapp 下的 web.xmlspringmvc.xmlJSP 视图 2. Spring 进阶 - Dispatcher…

【mysql】如何解决主从架构从库延迟问题

目录 1. 说明2.优化主库的写入性能3. 优化网络性能4. 增强从库的硬件性能5. 调整从库的配置6. 主从架构优化7. 监控和调优8.使用 GTID 和 Group Replication 1. 说明 1.在 MySQL 数据库中&#xff0c;从库延迟&#xff08;replication lag&#xff09;是指主库和从库之间的数据…