diff --git a/app/src/main/kotlin/com/simplemobiletools/camera/views/AutoFitTextureView.kt b/app/src/main/kotlin/com/simplemobiletools/camera/views/AutoFitTextureView.kt new file mode 100644 index 00000000..ec20a370 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/camera/views/AutoFitTextureView.kt @@ -0,0 +1,39 @@ +package com.simplemobiletools.camera.views + +import android.content.Context +import android.util.AttributeSet +import android.view.TextureView +import android.view.View + +// taken from the official Camera2 sample at https://github.com/googlesamples/android-Camera2Basic +class AutoFitTextureView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyle: Int = 0) : TextureView(context, attrs, defStyle) { + + private var mRatioWidth = 0 + private var mRatioHeight = 0 + + fun setAspectRatio(width: Int, height: Int) { + if (width < 0 || height < 0) { + throw IllegalArgumentException("Size cannot be negative.") + } + + mRatioWidth = width + mRatioHeight = height + requestLayout() + } + + override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { + super.onMeasure(widthMeasureSpec, heightMeasureSpec) + val width = View.MeasureSpec.getSize(widthMeasureSpec) + val height = View.MeasureSpec.getSize(heightMeasureSpec) + + if (mRatioWidth == 0 || mRatioHeight == 0) { + setMeasuredDimension(width, height) + } else { + if (width < height * mRatioWidth / mRatioHeight) { + setMeasuredDimension(width, width * mRatioHeight / mRatioWidth) + } else { + setMeasuredDimension(height * mRatioWidth / mRatioHeight, height) + } + } + } +}