Skip to content

Commit

Permalink
fix: Trigger measure and layout manually in PreviewView (#2588)
Browse files Browse the repository at this point in the history
* fix: Trigger `measure` and `layout` manually to fix Preview stretching

* fix: Check for `0`/`NaN`
  • Loading branch information
mrousavy committed Feb 19, 2024
1 parent 9af6e61 commit 7ac6f4d
Showing 1 changed file with 14 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7ac6f4d

Please sign in to comment.