前言

前一段时间做了一个即时通讯的项目,在项目中遇到很多坑,有时间一一做个总结,项目消息发送基于XMPP+Tigase,语言视频通话基于PJSIP+FreeSWITCH,项目UI仿微信。做到视频通话时,遇到本地视图与远程视图切换,网上搜了一篇相关的博客,根据大神思路写了这个Demo,其中用的是第三直播源可能有点不稳定,切换过程可能存在黑屏和无响应的情况,但是用的Pjsip中切换还是很流程的;

布局

  1. <?xml version=”1.0″ encoding=”utf-8″?>
  2. <FrameLayout
  3. xmlns:android=“http://schemas.android.com/apk/res/android”
  4. xmlns:tools=“http://schemas.android.com/tools”
  5. android:layout_width=“match_parent”
  6. android:layout_height=“match_parent”
  7. tools:context=“com.demo.surfaceviewdemo.MainActivity”>
  8. <RelativeLayout
  9. android:layout_width=“match_parent”
  10. android:layout_height=“match_parent”>
  11. <RelativeLayout
  12. android:id=“@+id/rl_remote”
  13. android:layout_width=“match_parent”
  14. android:layout_height=“wrap_content”>
  15. <SurfaceView
  16. android:id=“@+id/surfaceview_remote”
  17. android:layout_width=“match_parent”
  18. android:layout_height=“match_parent”/>
  19. </RelativeLayout>
  20. <RelativeLayout
  21. android:id=“@+id/rl_local”
  22. android:layout_width=“wrap_content”
  23. android:layout_height=“wrap_content”
  24. android:layout_alignParentRight=“true”>
  25. <SurfaceView
  26. android:id=“@+id/surfaceview_local”
  27. android:layout_width=“wrap_content”
  28. android:layout_height=“wrap_content”/>
  29. </RelativeLayout>
  30. </RelativeLayout>
  31. <!–通话时显示的–>
  32. <LinearLayout
  33. android:id=“@+id/ll_call_container”
  34. android:layout_width=“match_parent”
  35. android:layout_height=“wrap_content”
  36. android:layout_gravity=“bottom”
  37. android:layout_marginBottom=“25dp”
  38. android:gravity=“center_horizontal”
  39. android:orientation=“horizontal”>
  40. <TextView
  41. android:id=“@+id/tv_call_quiet”
  42. android:layout_width=“wrap_content”
  43. android:layout_height=“wrap_content”
  44. android:layout_weight=“1”
  45. android:drawablePadding=“10dp”
  46. android:drawableTop=“@mipmap/chat_video_change_voice_img”
  47. android:gravity=“center_horizontal”
  48. android:text=“切到语音聊天”
  49. android:textColor=“#ffffff”
  50. android:textSize=“12sp”/>
  51. <TextView
  52. android:id=“@+id/tv_handup_call”
  53. android:layout_width=“wrap_content”
  54. android:layout_height=“wrap_content”
  55. android:layout_weight=“1”
  56. android:drawablePadding=“10dp”
  57. android:drawableTop=“@mipmap/chat_video_guaduan_img_normal”
  58. android:gravity=“center_horizontal”
  59. android:text=“挂断”
  60. android:textColor=“#ffffff”
  61. android:textSize=“12sp”/>
  62. <TextView
  63. android:id=“@+id/tv_change_camera”
  64. android:layout_width=“wrap_content”
  65. android:layout_height=“wrap_content”
  66. android:layout_weight=“1”
  67. android:drawablePadding=“10dp”
  68. android:drawableTop=“@mipmap/chat_video_change_camera_img”
  69. android:gravity=“center_horizontal”
  70. android:text=“转换摄像头”
  71. android:textColor=“#ffffff”
  72. android:textSize=“12sp”/>
  73. </LinearLayout>
  74. </FrameLayout>

代码

为了实现跟微信一样的效果,普通屏幕全屏显示,为了不让视频内容挤到刘海屏中,添加一下代码:

  1. //如果判断有刘海屏不让填充到状态栏
  2. if (DisplayUtil.hasNotchScreen(this)) {
  3. getWindow().addFlags(
  4. WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
  5. | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
  6. } else {
  7. getWindow().addFlags(
  8. WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
  9. | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
  10. | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
  11. }

大小视图切换代码:

  1. /**
  2. * 大小视图切换 (小视图在前面、大视图在后面)
  3. *
  4. * @param sourcView 之前相对布局大小
  5. * @param beforeview 之前surfaceview
  6. * @param detView 之后相对布局大小
  7. * @param afterview 之后surfaceview
  8. */
  9. private void zoomOpera(View sourcView, SurfaceView beforeview,
  10. View detView, SurfaceView afterview) {
  11. RelativeLayout paretview = (RelativeLayout) sourcView.getParent();
  12. paretview.removeView(detView);
  13. paretview.removeView(sourcView);
  14. //设置远程大视图RelativeLayout 的属性
  15. RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
  16. RelativeLayout.LayoutParams.MATCH_PARENT);
  17. params1.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
  18. beforeview.setZOrderMediaOverlay(true);
  19. beforeview.getHolder().setFormat(PixelFormat.TRANSPARENT);
  20. sourcView.setLayoutParams(params1);
  21. //设置本地小视图RelativeLayout 的属性
  22. params1 = new RelativeLayout.LayoutParams(defaultLocalwidth, defaultLocalHeight);
  23. params1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
  24. params1.setMargins(0, defaultLocalMargin, defaultLocalMargin, 0);
  25. //在调用setZOrderOnTop(true)之后调用了setZOrderMediaOverlay(true) 遮挡问题
  26. afterview.setZOrderOnTop(true);
  27. afterview.setZOrderMediaOverlay(true);
  28. afterview.getHolder().setFormat(PixelFormat.TRANSPARENT);
  29. detView.setLayoutParams(params1);
  30. paretview.addView(sourcView);
  31. paretview.addView(detView);
  32. }

%title插图%num

效果图一

%title插图%num

效果图二