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

}
相关推荐
大白要努力!38 分钟前
Android opencv使用Core.hconcat 进行图像拼接
android·opencv
天空中的野鸟1 小时前
Android音频采集
android·音视频
小白也想学C3 小时前
Android 功耗分析(底层篇)
android·功耗
曙曙学编程3 小时前
初级数据结构——树
android·java·数据结构
会讲英语的码农4 小时前
Git项目管理
gitee·github
闲暇部落5 小时前
‌Kotlin中的?.和!!主要区别
android·开发语言·kotlin
诸神黄昏EX7 小时前
Android 分区相关介绍
android
大白要努力!8 小时前
android 使用SQLiteOpenHelper 如何优化数据库的性能
android·数据库·oracle
Estar.Lee8 小时前
时间操作[取当前北京时间]免费API接口教程
android·网络·后端·网络协议·tcp/ip
Winston Wood8 小时前
Perfetto学习大全
android·性能优化·perfetto