adding AutoFitTextuewView from the Camera2 sample
This commit is contained in:
parent
5fa8f06391
commit
d6ff4c5c64
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue