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

一、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;
            }
        });
    }

}
相关推荐
xvch3 小时前
Kotlin 2.1.0 入门教程(八)
android·kotlin
limingade5 小时前
手机app如何跳过无障碍权限实现弹框自动点击-ADB连接专题
android·adb·智能手机·蓝牙电话·手机提取通话声音
limingade5 小时前
如何跨互联网adb连接到远程手机-蓝牙电话集中维护
android·arm开发·adb·智能手机·信息与通信·蓝牙电话
dal118网工任子仪6 小时前
79,【3】BUUCTF WEB [GXYCTF2019]BabysqliV3.0
android·前端
东京老树根6 小时前
Android - 通过Logcat Manager简单获取Android手机的Log
android·智能手机
天才奇男子6 小时前
数据库用户管理
android·数据库·adb
凯子坚持 c7 小时前
如何在gitee/github上面搭建obsidian的图床
gitee·github
计算机-秋大田13 小时前
基于微信的原创音乐小程序的设计与实现(LW+源码+讲解)
java·开发语言·后端·微信·小程序·课程设计
aerror14 小时前
Macos下交叉编译安卓的paq8px压缩算法
android·macos
zhangphil15 小时前
Android BitmapShader简洁实现马赛克,Kotlin(二)
android·kotlin