@@ -10,25 +10,21 @@ import android.view.SurfaceView
10
10
import android.widget.FrameLayout
11
11
import com.facebook.react.bridge.UiThreadUtil
12
12
import com.mrousavy.camera.extensions.getMaximumPreviewSize
13
+ import com.mrousavy.camera.extensions.resize
13
14
import com.mrousavy.camera.types.ResizeMode
14
15
import kotlin.math.roundToInt
16
+ import kotlinx.coroutines.Dispatchers
17
+ import kotlinx.coroutines.withContext
15
18
16
19
@SuppressLint(" ViewConstructor" )
17
20
class PreviewView (context : Context , callback : SurfaceHolder .Callback ) : SurfaceView(context) {
18
21
var size: Size = getMaximumPreviewSize()
19
- set(value) {
20
- field = value
21
- UiThreadUtil .runOnUiThread {
22
- Log .i(TAG , " Setting PreviewView Surface Size to $width x $height ..." )
23
- holder.setFixedSize(value.height, value.width)
24
- requestLayout()
25
- invalidate()
26
- }
27
- }
22
+ private set
28
23
var resizeMode: ResizeMode = ResizeMode .COVER
29
24
set(value) {
30
25
field = value
31
26
UiThreadUtil .runOnUiThread {
27
+ Log .i(TAG , " Setting PreviewView ResizeMode to $value ..." )
32
28
requestLayout()
33
29
invalidate()
34
30
}
@@ -41,11 +37,23 @@ class PreviewView(context: Context, callback: SurfaceHolder.Callback) : SurfaceV
41
37
FrameLayout .LayoutParams .MATCH_PARENT ,
42
38
Gravity .CENTER
43
39
)
40
+ holder.setKeepScreenOn(true )
44
41
holder.addCallback(callback)
45
42
}
46
43
44
+ suspend fun setSurfaceSize (width : Int , height : Int ) {
45
+ withContext(Dispatchers .Main ) {
46
+ size = Size (width, height)
47
+ Log .i(TAG , " Setting PreviewView Surface Size to $size ..." )
48
+ requestLayout()
49
+ invalidate()
50
+ holder.resize(width, height)
51
+ }
52
+ }
53
+
47
54
private fun getSize (contentSize : Size , containerSize : Size , resizeMode : ResizeMode ): Size {
48
- val contentAspectRatio = contentSize.width.toDouble() / contentSize.height
55
+ // TODO: Take sensor orientation into account here
56
+ val contentAspectRatio = contentSize.height.toDouble() / contentSize.width
49
57
val containerAspectRatio = containerSize.width.toDouble() / containerSize.height
50
58
51
59
val widthOverHeight = when (resizeMode) {
0 commit comments