Compare commits
1 Commits
031aa9d43a
...
eyenov/pre
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c994506abc |
@@ -133,8 +133,8 @@ android {
|
|||||||
}
|
}
|
||||||
|
|
||||||
compileOptions {
|
compileOptions {
|
||||||
sourceCompatibility JavaVersion.VERSION_17
|
sourceCompatibility JavaVersion.VERSION_1_8
|
||||||
targetCompatibility JavaVersion.VERSION_17
|
targetCompatibility JavaVersion.VERSION_1_8
|
||||||
}
|
}
|
||||||
|
|
||||||
externalNativeBuild {
|
externalNativeBuild {
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
package="com.mrousavy.camera">
|
||||||
|
|
||||||
</manifest>
|
</manifest>
|
||||||
|
|||||||
@@ -107,47 +107,6 @@ class PreviewView(context: Context, callback: SurfaceHolder.Callback) :
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getSize(contentSize: Size, containerSize: Size, resizeMode: ResizeMode): Size {
|
|
||||||
var contentSize = contentSize
|
|
||||||
// Swap dimensions if orientation is landscape
|
|
||||||
if (orientation.isLandscape()) {
|
|
||||||
contentSize = Size(contentSize.height, contentSize.width)
|
|
||||||
}
|
|
||||||
val contentAspectRatio = contentSize.width.toDouble() / contentSize.height
|
|
||||||
val containerAspectRatio = containerSize.width.toDouble() / containerSize.height
|
|
||||||
if (!(contentAspectRatio > 0 && containerAspectRatio > 0)) {
|
|
||||||
// One of the aspect ratios is 0 or NaN, maybe the view hasn't been laid out yet.
|
|
||||||
return contentSize
|
|
||||||
}
|
|
||||||
|
|
||||||
val widthOverHeight = when (resizeMode) {
|
|
||||||
ResizeMode.COVER -> contentAspectRatio > containerAspectRatio
|
|
||||||
ResizeMode.CONTAIN -> contentAspectRatio < containerAspectRatio
|
|
||||||
}
|
|
||||||
|
|
||||||
return if (widthOverHeight) {
|
|
||||||
// Scale by width to cover height
|
|
||||||
val scaledWidth = containerSize.height * contentAspectRatio
|
|
||||||
Size(scaledWidth.roundToInt(), containerSize.height)
|
|
||||||
} else {
|
|
||||||
// Scale by height to cover width
|
|
||||||
val scaledHeight = containerSize.width / contentAspectRatio
|
|
||||||
Size(containerSize.width, scaledHeight.roundToInt())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@SuppressLint("DrawAllocation")
|
|
||||||
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
|
|
||||||
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
|
|
||||||
|
|
||||||
val measuredViewSize = Size(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.getSize(heightMeasureSpec))
|
|
||||||
val surfaceSize = size.rotatedBy(inputOrientation)
|
|
||||||
val fittedSize = getSize(surfaceSize, measuredViewSize, resizeMode)
|
|
||||||
|
|
||||||
Log.i(TAG, "PreviewView is $measuredViewSize rendering $surfaceSize orientation ($orientation). Resizing to: $fittedSize ($resizeMode)")
|
|
||||||
setMeasuredDimension(fittedSize.width, fittedSize.height)
|
|
||||||
}
|
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private const val TAG = "PreviewView"
|
private const val TAG = "PreviewView"
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user