Android仿微信视频聊天本地与远程切换功能

news/2024/5/17 18:02:54 标签: android, 微信, gitee

一、xml布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinatorLayout"
    android:layout_width="@dimen/dp_640"
    android:layout_height="@dimen/dp_400"
    android:background="@color/pageBgColor"
    android:orientation="vertical">

    <!--  视频预览 -->
    <csu.xiaoya.robotApp.ui.activity.homepage.familydct.bean.DraggableTextureView
        android:id="@+id/preview"
        android:layout_width="@dimen/dp_640"
        android:layout_height="@dimen/dp_400"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintLeft_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <!--  远程视频 -->
    <csu.xiaoya.robotApp.ui.activity.homepage.familydct.bean.DraggableTextureView
        android:id="@+id/remoteUserView"
        android:layout_width="150dp"
        android:layout_height="180dp"
        android:layout_marginTop="30dp"
        android:layout_marginRight="30dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintLeft_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/imHead"
        android:layout_width="@dimen/dp_120"
        android:layout_height="@dimen/dp_120"
        android:layout_gravity="center"
        android:layout_marginBottom="@dimen/dp_100"
        android:scaleType="centerCrop"
        android:src="@mipmap/doctor_head"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <ImageView
        android:id="@+id/changeVideoWindows"
        android:layout_width="@dimen/dp_30"
        android:layout_height="@dimen/dp_30"
        android:layout_marginLeft="@dimen/dp_30"
        android:layout_marginTop="@dimen/dp_30"
        android:background="@drawable/change_windows"
        android:src="@mipmap/video_windows_change"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

  二、切换代码

 /**
     * 通话大小
     * 窗口切换
     */
    private boolean isLocalVideoSmallState = true;

    private void switchWindowMode(VideoChatDialog videoChatDialog, boolean isLocalVideoSmall) {
        ConstraintLayout constraintLayout = videoChatDialog.findViewById(R.id.coordinatorLayout);
        TextureView localVideoTextureView = videoChatDialog.findViewById(R.id.preview);
        TextureView remoteVideoTextureView = videoChatDialog.findViewById(R.id.remoteUserView);
    
        ImageView changeVideoWindows = videoChatDialog.findViewById(R.id.changeVideoWindows);

        ConstraintSet constraintSet = new ConstraintSet();
        constraintSet.clone(constraintLayout);
        if (isLocalVideoSmall) {

            constraintLayout.removeView(localVideoTextureView);
            constraintLayout.removeView(remoteVideoTextureView);
    
            constraintLayout.removeView(changeVideoWindows);

            constraintLayout.addView(remoteVideoTextureView);
            constraintLayout.addView(localVideoTextureView);
  
            constraintLayout.addView(changeVideoWindows);

            // 远程端全屏模式
            remoteVideoTextureView.setEnabled(false);
            constraintSet.clear(remoteVideoTextureView.getId());
            constraintSet.connect(remoteVideoTextureView.getId(), ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP, 0);
            constraintSet.connect(remoteVideoTextureView.getId(), ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END, 0);
            constraintSet.connect(remoteVideoTextureView.getId(), ConstraintSet.RIGHT, ConstraintSet.PARENT_ID, ConstraintSet.RIGHT, 0);

            constraintSet.constrainWidth(remoteVideoTextureView.getId(), 1280);
            constraintSet.constrainHeight(remoteVideoTextureView.getId(), 800);

            // 本地小窗口
            localVideoTextureView.setEnabled(true);
            constraintSet.clear(localVideoTextureView.getId());

            constraintSet.connect(localVideoTextureView.getId(), ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP, 30);
            constraintSet.connect(localVideoTextureView.getId(), ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END, 0);
            constraintSet.connect(localVideoTextureView.getId(), ConstraintSet.RIGHT, ConstraintSet.PARENT_ID, ConstraintSet.RIGHT, 30);

            constraintSet.constrainWidth(localVideoTextureView.getId(), 300); // 设置小窗口的宽度
            constraintSet.constrainHeight(localVideoTextureView.getId(), 200);

            isLocalVideoSmallState = false;

        } else {

            constraintLayout.removeView(remoteVideoTextureView);
            constraintLayout.removeView(localVideoTextureView);
      
            constraintLayout.removeView(changeVideoWindows);

            constraintLayout.addView(localVideoTextureView);
            constraintLayout.addView(remoteVideoTextureView);
          
            constraintLayout.addView(changeVideoWindows);

            // 本地 全屏模式
            localVideoTextureView.setEnabled(false);
            constraintSet.clear(localVideoTextureView.getId());

            constraintSet.connect(localVideoTextureView.getId(), ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP, 0);
            constraintSet.connect(localVideoTextureView.getId(), ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END, 0);
            constraintSet.connect(localVideoTextureView.getId(), ConstraintSet.RIGHT, ConstraintSet.PARENT_ID, ConstraintSet.RIGHT, 0);

            constraintSet.constrainWidth(localVideoTextureView.getId(), 1280);
            constraintSet.constrainHeight(localVideoTextureView.getId(), 800);

            // 远程 小窗口
            remoteVideoTextureView.setEnabled(true);
            constraintSet.clear(remoteVideoTextureView.getId());

            constraintSet.connect(remoteVideoTextureView.getId(), ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP, 30);
            constraintSet.connect(remoteVideoTextureView.getId(), ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END, 0);
            constraintSet.connect(remoteVideoTextureView.getId(), ConstraintSet.RIGHT, ConstraintSet.PARENT_ID, ConstraintSet.RIGHT, 30);

            constraintSet.constrainWidth(remoteVideoTextureView.getId(), 300); // 设置小窗口的宽度
            constraintSet.constrainHeight(remoteVideoTextureView.getId(), 200);

            isLocalVideoSmallState = true;
        }

        constraintSet.applyTo(constraintLayout);

    }

三、自定义可拖拽TextureView 



/**
 * 自定义可拖动
 * TextureView
 */

public class DraggableTextureView extends TextureView {

    private float lastX;
    private float lastY;
    private boolean isDragging;

    public DraggableTextureView(Context context) {
        super(context);
        init();
    }

    public DraggableTextureView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }


    public DraggableTextureView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }


    private void init() {
        setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        lastX = event.getRawX();
                        lastY = event.getRawY();
                        isDragging = true;
                        break;
                    case MotionEvent.ACTION_MOVE:
                        if (isDragging) {
                            float dx = event.getRawX() - lastX;
                            float dy = event.getRawY() - lastY;
                            int newLeft = (int) (v.getLeft() + dx);
                            int newTop = (int) (v.getTop() + dy);
                            int newRight = (int) (v.getRight() + dx);
                            int newBottom = (int) (v.getBottom() + dy);
                            v.layout(newLeft, newTop, newRight, newBottom);
                            lastX = event.getRawX();
                            lastY = event.getRawY();
                        }
                        break;
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_CANCEL:
                        isDragging = false;
                        break;
                }
                return true;
            }
        });
    }

}


http://www.niftyadmin.cn/n/5444120.html

相关文章

实体框架EF(Entity Framework)简介

实体框架EF&#xff08;Entity Framework&#xff09;简介 文章目录 实体框架EF&#xff08;Entity Framework&#xff09;简介一、概述二、O/R Mapping是什么采用O/R Mapping带来哪些好处 三、Entity Framework架构3.1 下图展示了Entity Framework的整体架构3.2 Entity Framew…

中国贸易金融跨行交易区块链平台CTFU、区块链福费廷交易平台BCFT、中国人民银行贸易金融区块链平台CTFP、银行函证区块链服务平台BPBC

中国人民银行贸易金融区块链平台CTFP介绍 贸易金融的发展概况及存在的问题 1.1 贸易金融的概况 贸易金融是指商业银行在贸易双方债权债务关系的基础上&#xff0c;为国内或跨国的商品和服务贸易提供的贯穿贸易活动整个价值链、全程全面性的综合金融服务。伴随全球化的进程&a…

【数字图像处理系列】读取图像

【数字图像处理系列】读取图像 使用函数 imread 可以将图像读人 MATLAB 环境&#xff0c;imread 的语法为 imread(filename)其中&#xff0c;filename是一个含有图像文件全名的字符串(包括任何可用的扩展名)。例如&#xff0c;命令行 >>f imread(pout.tif)将tif图像po…

探索国内ip切换App:打破网络限制

在国内网络环境中&#xff0c;有时我们会遇到一些限制或者屏蔽&#xff0c;使得我们无法自由访问一些网站或服务。而国内IP切换App的出现&#xff0c;为解决这些问题提供了非常便捷的方式。这些App可以帮助用户切换IP地址&#xff0c;让用户可以轻松地访问被限制或屏蔽的网站&a…

深度学习算法工程师面试常见问题及解答

基础知识和理论&#xff1a; 解释什么是深度学习以及它与机器学习的关系。描述神经网络的基本结构和工作原理。什么是反向传播算法&#xff0c;它是如何工作的&#xff1f;激活函数的作用是什么&#xff1f;常见的激活函数有哪些&#xff1f;什么是损失函数&#xff1f;常见的损…

Unity 粒子在UI中使用时需要注意的地方

最近项目中要在UI中挂载粒子特效,美术给过来的粒子直接放到UI中会有一些问题,查询一些资料后,总结了一下 一: 粒子的大小发生变化,与在预制件编辑中设计的大小不同 在预制件编辑模式下,大小正常 实际使用的时候特别大或者特别小 经过检查,发现预制件编辑模式下,默认画布的Rend…

OpenCV 介绍使用

返回&#xff1a;OpenCV系列文章目录&#xff08;持续更新中......&#xff09; 上一篇:OpenCV4.9.0开源计算机视觉库使用简要说明 下一篇: OpenCV&#xff08;开源计算机视觉库&#xff1a;http://opencv.org&#xff09;是一个开源库&#xff0c;包含数百种计算机视觉算法。…

大厂面试--[VueReact] 对虚拟DOM的理解?虚拟DOM主要做了什么?虚拟DOM本身是什么?

什么是虚拟DOM ​ 从本质上讲&#xff0c;Virtual Dom是一个Javascript对象&#xff0c;通过对象的方式来表示DOM结构。将页面的状态抽象为JS对象的形式&#xff0c;配合不同的渲染工具&#xff0c;将使跨平台渲染成为可能。通过事物处理机制&#xff0c;将多次DOM修改的结果一…