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

}
相关推荐
无极程序员32 分钟前
PHP常量
android·ide·android studio
Rainbow_Technology1 小时前
劫持微信聊天记录并分析还原 —— 数据库结构讲解(四)
微信·微信聊天记录查看·微信聊天记录破解
萌面小侠Plus2 小时前
Android笔记(三十三):封装设备性能级别判断工具——低端机还是高端机
android·性能优化·kotlin·工具类·低端机
慢慢成长的码农2 小时前
Android Profiler 内存分析
android
大风起兮云飞扬丶2 小时前
Android——多线程、线程通信、handler机制
android
L72562 小时前
Android的Handler
android
清风徐来辽2 小时前
Android HandlerThread 基础
android
HerayChen3 小时前
HbuildderX运行到手机或模拟器的Android App基座识别不到设备 mac
android·macos·智能手机
顾北川_野3 小时前
Android 手机设备的OEM-unlock解锁 和 adb push文件
android·java
hairenjing11233 小时前
在 Android 手机上从SD 卡恢复数据的 6 个有效应用程序
android·人工智能·windows·macos·智能手机