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