Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect image bounds with Modifier.wrapContentSize() #111

Open
saket opened this issue Nov 2, 2024 · 0 comments
Open

Incorrect image bounds with Modifier.wrapContentSize() #111

saket opened this issue Nov 2, 2024 · 0 comments

Comments

@saket
Copy link
Owner

saket commented Nov 2, 2024

ZoomableImage() does not correctly measure images when Modifier.wrapContentSize() is used.

Expected: Image() Actual: ZoomableImage()
image zoomableimage
Image(
  modifier = Modifier.wrapContentSize(),
  painter = assetPainter("cat_1920.jpg"),
  contentDescription = null,
  contentScale = ContentScale.Crop,
  alignment = Alignment.BottomCenter,
)
ZoomableImage(
  modifier = Modifier.wrapContentSize(),
  image = ZoomableImageSource.asset("cat_1920.jpg"),
  contentDescription = null,
  contentScale = ContentScale.Crop,
  alignment = Alignment.BottomCenter,
)
@Composable
internal fun ZoomableImageSource.Companion.asset(assetName: String): ZoomableImageSource {
  val painter = assetPainter(assetName)
  return remember(painter) {
    object : ZoomableImageSource {
      @Composable
      override fun resolve(canvasSize: Flow<Size>): ResolveResult {
        return ResolveResult(
          delegate = ZoomableImageSource.PainterDelegate(painter)
        )
      }
    }
  }
}

@Composable
private fun assetPainter(assetName: String): Painter {
  val context = LocalContext.current
  return remember(assetName) {
    context.assets.open(assetName).use { stream ->
      BitmapPainter(BitmapFactory.decodeStream(stream).asImageBitmap())
    }
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant