fix: Trigger measure and layout manually in PreviewView (#2588)

* fix: Trigger `measure` and `layout` manually to fix Preview stretching

* fix: Check for `0`/`NaN`
This commit is contained in:
Marc Rousavy 2024-02-19 14:54:13 +01:00 committed by GitHub
parent 9af6e61dc8
commit 7ac6f4d008
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -90,9 +90,23 @@ class PreviewView(context: Context, callback: SurfaceHolder.Callback) :
}
}
override fun requestLayout() {
super.requestLayout()
// Manually trigger measure & layout, as RN on Android skips those.
// See this issue: https://github.com/facebook/react-native/issues/17968#issuecomment-721958427
post {
measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY))
layout(left, top, right, bottom)
}
}
private fun getSize(contentSize: Size, containerSize: Size, resizeMode: ResizeMode): Size {
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