微信小程序悬浮窗功能实现

利用flex布局,实现小程序悬浮窗可拖动,可点击
先上效果图看看在这里插入图片描述
来看看代码
index.wxml

<view>
    <view bindtouchmove="handleSetMoveViewPos">
        <view wx-if="{{!isIos}}" class="move-view" style=" top:{{top}}px;left:{{left}}px;" bindtap="getUserProfile" catchtouchmove="handleTouchMove">
            <image class="img" src="https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTJLUTSZpHgwZBt5NNsfPtOgd0jCdgBcOPOQBvic8jvsND5iadkA46jbdbx1StzmOJdeHEK0TsmmRzmw/132">
            </image>
        </view>
        <cover-view wx-if="{{isIos}}" class="move-view" style=" top:{{top}}px;left:{{left}}px;" bindtap="getUserProfile" catchtouchmove="handleTouchMove">
            <cover-image class="img" src="https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTJLUTSZpHgwZBt5NNsfPtOgd0jCdgBcOPOQBvic8jvsND5iadkA46jbdbx1StzmOJdeHEK0TsmmRzmw/132">
            </cover-image>
        </cover-view>
    </view>
    <view>
        哈哈哈😃哈哈哈
    </view>
</view>

index.wxss

page {
    background-color: skyblue;
}
.move-view {
    position: fixed ; /*相对定位*/
	height:120rpx;
	width:120rpx;
	border-radius:50%;
	color: white;
    /* background-color: skyblue; */
    box-shadow: inset 0 0 15px 5px #fff;
	text-align: center;
}
.img {
    width: 120rpx;
    height: 120rpx;
    border-radius: 50%;
    box-shadow: inset 0 0 15px 5px #fff;
}

index.js

Page({
/**
   * 页面的初始数据
   */
  data: {
    left: 0,
    top: 0,
    isIos: true // 小程序运行的环境是否是ios,默认为true
  },
    /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.getSystemInfo({
      success: (res) => {
        if (res.platform == "android") {
        // 判断小程序运行的环境
          this.setData({
            isIos: false
          })
        }
      }
    })
  },
  /**
  * 安卓机和ios兼容问题
  */
   handleSetMoveViewPos: function (e) {
     console.log('handleSetMoveViewPos,e',this.data.isIos);
    // 在ios下永远都不会走这个方案,以免引起无用的计算
    if (!this.data.isIos) {
      const RADIUS = 30 // 悬浮窗半径

      const touchPosX = e.touches[0].clientX
      const touchPosY = e.touches[0].clientY

      const moveViewCenterPosX = this.data.left + RADIUS
      const moveViewCenterPosY = this.data.top + RADIUS

      // 确保手指在悬浮窗上才可以移动
      if (Math.abs(moveViewCenterPosX - touchPosX) < RADIUS && Math.abs(moveViewCenterPosY - touchPosY) < RADIUS) {
        if (touchPosX > 0 && touchPosY > 0) {
          this.setData({
            left: touchPosX - RADIUS,
            top: touchPosY - RADIUS
          })
        } else {
          this.setData({
            left: 0, // 默认显示位置 left距离
            top: 0  // 默认显示位置 top距离
          })
        }
      }
    }
  },
  /**
  * 拖拽移动
  */
   handleTouchMove: function (e) {
     console.log('handleTouchMove---->',e);
    const RADIUS = 30 // 悬浮窗半径

    const touchPosX = e.touches[0].clientX
    const touchPosY = e.touches[0].clientY

    if (touchPosX > 0 && touchPosY > 0) {
      this.setData({
        left: touchPosX - RADIUS,
        top: touchPosY - RADIUS
      })
    } else {
      this.setData({
        left: 0, //默认显示位置 left距离
        top: 0  //默认显示位置 top距离
      })
    }
  },
  /**
  * 实现可点击效果
  */
  getUserProfile(e){
    wx.getUserProfile({
      desc: '用于完善会员资料',
      success: (res) => {
        console.log('用户信息',res);
      }
    })
  },
})
  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您!要实现仿微信视频通话悬浮窗功能,您可以按照以下步骤进行操作: 1. 创建悬浮窗权限:在 AndroidManifest.xml 文件中添加悬浮窗权限声明,如下所示: ```xml <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> ``` 2. 创建悬浮窗服务:创建一个继承自 Service 的类,用于管理悬浮窗的创建、更新和移除等操作。在该类中,您可以使用 WindowManager 来显示悬浮窗,并监听触摸事件来实现拖拽和缩放等操作。 3. 设计悬浮窗布局:创建一个 XML 布局文件,用于定义悬浮窗的界面。您可以自定义界面元素,如视频画面、控制按钮等。 4. 实现悬浮窗功能:在悬浮窗服务类中,根据需要添加相应的逻辑。例如,您可以使用 MediaProjection API 来捕获屏幕内容,并将其显示在悬浮窗中;您还可以使用 Camera API 来获取摄像头数据,并实现视频通话功能。 5. 添加权限申请逻辑:在需要使用悬浮窗功能的地方,添加权限申请逻辑,以确保用户在安装应用时授予悬浮窗权限。 请注意,实现仿微信视频通话悬浮窗功能需要一定的 Android 开发知识和经验。如果您对 Android 开发不太熟悉,建议您先学习相关的基础知识,然后再尝试实现功能。同时,还要注意遵守 Android 官方的开发规范和安全要求。希望对您有所帮助!如果您有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值