ContainerLayout.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package io.rong.callkit;
  2. import android.content.Context;
  3. import android.support.annotation.NonNull;
  4. import android.util.AttributeSet;
  5. import android.view.SurfaceView;
  6. import android.view.ViewGroup;
  7. import android.view.WindowManager;
  8. import android.widget.RelativeLayout;
  9. import com.bailingcloud.bailingvideo.engine.binstack.util.FinLog;
  10. import com.bailingcloud.bailingvideo.engine.view.BlinkVideoView;
  11. /**
  12. * Created by Administrator on 2017/3/30.
  13. */
  14. public class ContainerLayout extends RelativeLayout {
  15. private Context context;
  16. private static boolean isNeedFillScrren = true;
  17. SurfaceView currentView;
  18. public ContainerLayout(Context context, AttributeSet attrs) {
  19. super(context, attrs);
  20. this.context = context;
  21. }
  22. public void addView(final SurfaceView videoView) {
  23. WindowManager wm = (WindowManager) context
  24. .getSystemService(Context.WINDOW_SERVICE);
  25. this.screenWidth = wm.getDefaultDisplay().getWidth();
  26. ;
  27. this.screenHeight = wm.getDefaultDisplay().getHeight();
  28. ;
  29. FinLog.d("---xx-- add view " + videoView.toString() + " Height: " + ((BlinkVideoView) videoView).rotatedFrameHeight + " Width: " + ((BlinkVideoView) videoView).rotatedFrameWidth);
  30. super.addView(videoView, getBigContainerParams((BlinkVideoView) videoView));
  31. currentView = videoView;
  32. ((BlinkVideoView) videoView).setOnSizeChangedListener(new BlinkVideoView.OnSizeChangedListener() {
  33. @Override
  34. public void onChanged(BlinkVideoView.Size size) {
  35. try {
  36. ContainerLayout.this.removeAllViews();
  37. FinLog.d("---xx-- change view " + videoView.toString() + " Height: " + ((BlinkVideoView) videoView).rotatedFrameHeight + " Width: " + ((BlinkVideoView) videoView).rotatedFrameWidth);
  38. ContainerLayout.this.addView(videoView, getBigContainerParams((BlinkVideoView) videoView));
  39. } catch (Exception e) {
  40. e.printStackTrace();
  41. }
  42. }
  43. });
  44. }
  45. @NonNull
  46. private LayoutParams getBigContainerParams(BlinkVideoView videoView) {
  47. LayoutParams layoutParams = null;
  48. if (!isNeedFillScrren) {
  49. if (screenHeight > screenWidth) { //V
  50. int layoutParamsHeight = (videoView.rotatedFrameHeight == 0 || videoView.rotatedFrameWidth == 0) ? ViewGroup.LayoutParams.WRAP_CONTENT : screenWidth * videoView.rotatedFrameHeight / videoView.rotatedFrameWidth;
  51. layoutParams = new LayoutParams(screenWidth, layoutParamsHeight);
  52. } else {
  53. int layoutParamsWidth = (videoView.rotatedFrameHeight == 0 || videoView.rotatedFrameHeight == 0) ? ViewGroup.LayoutParams.WRAP_CONTENT : (screenWidth * videoView.rotatedFrameWidth / videoView.rotatedFrameHeight > screenWidth ? screenWidth : screenHeight * videoView.rotatedFrameWidth / videoView.rotatedFrameHeight);
  54. layoutParams = new LayoutParams(layoutParamsWidth, screenHeight);
  55. }
  56. } else {
  57. layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
  58. }
  59. layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
  60. return layoutParams;
  61. }
  62. public void setIsNeedFillScrren(boolean isNeed) {
  63. isNeedFillScrren = isNeed;
  64. }
  65. @Override
  66. public void removeAllViews() {
  67. if (currentView != null)
  68. ((BlinkVideoView) currentView).setOnSizeChangedListener(null);
  69. super.removeAllViews();
  70. }
  71. private int screenWidth;
  72. private int screenHeight;
  73. }