1 /* 2 * Copyright 2019 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package androidx.camera.integration.antelope 18 19 import android.content.Context 20 import android.util.AttributeSet 21 import android.view.SurfaceView 22 import android.view.View 23 24 /** A [SurfaceView] that can be adjusted to a specified aspect ratio. */ 25 class AutoFitSurfaceView 26 @JvmOverloads 27 constructor(context: Context, attrs: AttributeSet? = null, defStyle: Int = 0) : 28 SurfaceView(context, attrs, defStyle) { 29 30 private var ratioWidth = 0 31 private var ratioHeight = 0 32 33 /** 34 * Sets the aspect ratio for this view. The size of the view will be measured based on the ratio 35 * calculated from the parameters. Note that the actual sizes of parameters don't matter, that 36 * is, calling setAspectRatio(2, 3) and setAspectRatio(4, 6) have the same result. 37 * 38 * @param width Relative horizontal size 39 * @param height Relative vertical size 40 */ setAspectRationull41 fun setAspectRatio(width: Int, height: Int) { 42 if (width < 0 || height < 0) { 43 throw IllegalArgumentException("Size cannot be negative.") 44 } 45 ratioWidth = width 46 ratioHeight = height 47 requestLayout() 48 } 49 50 /** 51 * When the surface is given a new width/height, ensure that it maintains the correct aspect 52 * ratio. 53 */ onMeasurenull54 override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { 55 super.onMeasure(widthMeasureSpec, heightMeasureSpec) 56 val width = View.MeasureSpec.getSize(widthMeasureSpec) 57 val height = View.MeasureSpec.getSize(heightMeasureSpec) 58 if (ratioWidth == 0 || ratioHeight == 0) { 59 setMeasuredDimension(width, height) 60 } else { 61 if (width < height * ratioWidth / ratioHeight) { 62 setMeasuredDimension(width, width * ratioHeight / ratioWidth) 63 } else { 64 setMeasuredDimension(height * ratioWidth / ratioHeight, height) 65 } 66 } 67 } 68 } 69