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

Unexpected results when using with Compose Modifier.drawWithContent and BlendMode #352

Closed
adrinieto opened this issue May 8, 2024 · 4 comments

Comments

@adrinieto
Copy link

adrinieto commented May 8, 2024

When using Modifier.drawWithContent and BlendMode.BlendMode.SrcIn I get a different result than in a real device or emulator.
For example, this is the Compose code to build a partially filled star icon:

Icon(
	painterResource(R.drawable.ic_star), contentDescription = null,
	modifier = Modifier
	    .size(200.dp)
	    .drawWithContent {
	        drawContent()
	        drawRect(Color.Yellow, size = Size(size.width * 0.7f, size.height), blendMode = BlendMode.SrcIn)
	    },
	)

Here are some results I got using the sample Android app in ComposeTest.

If I use createComposeRule, that internally uses a ComponentActivity I get a square image.

  @get:Rule
  val composeTestRule = createComposeRule()

image

But, If I use RoborazziTransparentActivity it works.

 @get:Rule
  val composeTestRule = createAndroidComposeRule<RoborazziTransparentActivity>()

image

After further investigation, it seems related with the colorBackground. It only works with a transparent background:

    <item name="android:colorBackground">@android:color/transparent</item>

When used in Compose Previews it works as expected, so I'm not sure what's going on.

Is this a problem of Roborazzi or a limitation of using Layoutlib?

Thanks in advance.

@adrinieto
Copy link
Author

In case it helps, to make BlendMode.SrcIn work in Compose .graphicsLayer(compositingStrategy = CompositingStrategy.Offscreen) is also needed. But the result is the same with Roborazzi.

https://stackoverflow.com/a/69790654/1518996

@takahirom
Copy link
Owner

Cloud you try to use hardware rendering mode?
#255 (comment)

@adrinieto
Copy link
Author

Thanks for your fast response. It works! 🙌

Should we enable this flag for all the project? Or only for specific screens? I see it also solves issues with elevations, shadows, and gradients. I guess there are some side effects and that's why it's not enabled by default.

@takahirom
Copy link
Owner

Good question! I think you can check this issue. I think we can enable the flag in the next Robolectric version for all projects
#296

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

2 participants