content: package com.example.zth.myapplication.player;
  
  import android.content.Context;
  import android.util.AttributeSet;
  import android.view.SurfaceView;
  
  public class PlayerView extends SurfaceView {
      private double aspectRatio;
  
  
      public PlayerView(Context context) {
          super(context);
      }
  
      public PlayerView(Context context, AttributeSet attrs) {
          super(context, attrs);
      }
  
      public PlayerView(Context context, AttributeSet attrs, int defStyleAttr) {
          super(context, attrs, defStyleAttr);
      }
  
      public void setAspect(double aspect) {
          if (aspect > 0) {
              this.aspectRatio = aspect;
              requestLayout();
          }
      }
  
      @Override
      protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
          if (aspectRatio > 0) {
              int initialWidth = MeasureSpec.getSize(widthMeasureSpec);
              int initialHeight = MeasureSpec.getSize(heightMeasureSpec);
  
              final int horizPadding = getPaddingLeft() + getPaddingRight();
              final int vertPadding = getPaddingTop() + getPaddingBottom();
              initialWidth -= horizPadding;
              initialHeight -= vertPadding;
  
              final double viewAspectRatio = (double) initialWidth / initialHeight;
              final double aspectDiff = aspectRatio / viewAspectRatio - 1;
  
              if (Math.abs(aspectDiff) > 0.01) {
                  if (aspectDiff > 0) {
                      // adjust heght from width
                      initialHeight = (int) (initialWidth / aspectRatio);
                  } else {
                      // adjust width from height
                      initialWidth = (int) (initialHeight * aspectRatio);
                  }
                  initialWidth += horizPadding;
                  initialHeight += vertPadding;
                  widthMeasureSpec = MeasureSpec.makeMeasureSpec(initialWidth, MeasureSpec.EXACTLY);
                  heightMeasureSpec = MeasureSpec.makeMeasureSpec(initialHeight, MeasureSpec.EXACTLY);
              }
          }
  
          super.onMeasure(widthMeasureSpec, heightMeasureSpec);
      }
  }
  