mirror of
https://github.com/SimpleMobileTools/Simple-Camera.git
synced 2025-06-27 09:02:59 +02:00
39 lines
1.3 KiB
Kotlin
39 lines
1.3 KiB
Kotlin
package com.simplemobiletools.camera.views
|
|
|
|
import android.content.Context
|
|
import android.util.AttributeSet
|
|
import android.view.TextureView
|
|
|
|
// 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 = MeasureSpec.getSize(widthMeasureSpec)
|
|
val height = 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)
|
|
}
|
|
}
|
|
}
|
|
}
|