Compare commits
	
		
			1 Commits
		
	
	
		
			eyenov/pre
			...
			031aa9d43a
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
|  | 031aa9d43a | 
| @@ -133,8 +133,8 @@ android { | |||||||
|   } |   } | ||||||
|  |  | ||||||
|   compileOptions { |   compileOptions { | ||||||
|     sourceCompatibility JavaVersion.VERSION_1_8 |     sourceCompatibility JavaVersion.VERSION_17 | ||||||
|     targetCompatibility JavaVersion.VERSION_1_8 |     targetCompatibility JavaVersion.VERSION_17 | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   externalNativeBuild { |   externalNativeBuild { | ||||||
|   | |||||||
| @@ -1,4 +1,3 @@ | |||||||
| <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,6 +107,47 @@ 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