content: package com.music.player.bhandari.m.customViews;
  
  import android.content.Context;
  import android.content.res.TypedArray;
  import android.util.AttributeSet;
  import android.widget.FrameLayout;
  
  import com.music.player.bhandari.m.R;
  
  /**
   * Copyright 2017 Amit Bhandari AB
   * <p>
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * <p>
   * http://www.apache.org/licenses/LICENSE-2.0
   * <p>
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  public class FixedAspectRatioFrameLayout extends FrameLayout {
      private int mAspectRatioWidth;
      private int mAspectRatioHeight;
  
      public FixedAspectRatioFrameLayout(Context context) {
          super(context);
      }
  
      public FixedAspectRatioFrameLayout(Context context, AttributeSet attrs) {
          super(context, attrs);
  
          init(context, attrs);
      }
  
      public FixedAspectRatioFrameLayout(Context context, AttributeSet attrs, int defStyle) {
          super(context, attrs, defStyle);
  
          init(context, attrs);
      }
  
      private void init(Context context, AttributeSet attrs) {
          TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FixedAspectRatioFrameLayout);
  
          mAspectRatioWidth = a.getInt(R.styleable.FixedAspectRatioFrameLayout_aspectRatioWidth, 1);
          mAspectRatioHeight = a.getInt(R.styleable.FixedAspectRatioFrameLayout_aspectRatioHeight, 1);
  
          a.recycle();
      }
      // **overrides**
  
      @Override
      protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
          int originalWidth = MeasureSpec.getSize(widthMeasureSpec);
  
          int originalHeight = MeasureSpec.getSize(heightMeasureSpec);
  
          int calculatedHeight = originalWidth * mAspectRatioHeight / mAspectRatioWidth;
  
          int finalWidth, finalHeight;
  
          if (calculatedHeight > originalHeight) {
              finalWidth = originalHeight * mAspectRatioWidth / mAspectRatioHeight;
              finalHeight = originalHeight;
          } else {
              finalWidth = originalWidth;
              finalHeight = calculatedHeight;
          }
  
          super.onMeasure(
                  MeasureSpec.makeMeasureSpec(finalWidth, MeasureSpec.EXACTLY),
                  MeasureSpec.makeMeasureSpec(finalHeight, MeasureSpec.EXACTLY));
      }
  }