adding AutoFitTextuewView from the Camera2 sample

This commit is contained in:
tibbi 2018-05-27 21:33:28 +02:00
parent 5fa8f06391
commit d6ff4c5c64
1 changed files with 39 additions and 0 deletions

View File

@ -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)
}
}
}
}