I'm struggling a bit with getting a shadow to show...
# compose
a
I'm struggling a bit with getting a shadow to show for an image (like an elevated profile photo). I'm using the
shadow
modifier as follows:
Copy code
@Composable
@Preview
private fun ProfileItem() {
  Box(modifier = Modifier.background(Color.White).padding(20.dp)) {
    Image(
        painter = painterResource(id = R.drawable.charles),
        contentScale = ContentScale.Crop,
        contentDescription = "Profile Image",
        modifier = Modifier
            .size(48.dp)
            .shadow(8.dp, CircleShape)
    )
  }
}
This results in a circular avatar-esque image, but there's no shadow. However, if I swap out the
Image
Composable with a
Text
Composable, I see a shadow:
Copy code
@Composable
@Preview
private fun ProfileItem() {
  Box(modifier = Modifier.background(Color.White).padding(20.dp)) {
    Text("Testing shadow", modifier = Modifier.size(48.dp).shadow(8.dp, CircleShape))
  }
}
I've tried playing around with padding, nesting boxes, and using a surface instead of the shadow modifier, all without any luck. I'm sure I'm missing something obvious - any ideas?
c
Hmm I see your shadow in Preview in Arctic Fox Canary 15. What do you see?
a
Interesting - I see the circular image but no shower around it. The images I'm using are quite large...could that be impacting things?
This is what I'm getting for reference:
c
I don’t think it should since the shape in the shadow modifier will clip the image. How large is your image?
One way I test if the shadow is there is my adding an alpha modifier right before it
So you can see if it’s actually being drawn
a
Oh that is interesting
I'm seeing something similar to the above
So I'm not sure how to interpret this - it looks like there is a shadow there. Maybe it's just so faint that I'm not seeing it with my naked eye?
Maybe I'm just kind of blind?
c
What does the shadow look like on a physical device?
I wonder if the rendering looks different on Preview between macOS and Windows. Since this what I see on macOS which shows a clearer shadow on the bottom edges
👍 1
Maybe I’m just kind of blind?
Ha no I think the challenge with the shadow system is it’s based on the elevation value, so sometimes it’s not always clearly there when it’s actually there. We don’t have an easy way right now to make the shadow more obvious at the moment (related thread)
a
It's not really any different on device. I think I can convince myself that I actually see the shadow now - thank you! Another thing I'm not really grokking is like how to make the shadow more intense. And by intense I mean see more/darker shadow beneath/around the item in question. Like, if I bump elevation up to 100 or something crazy I don't see much difference, which is confusing to me.
c
Another thing I’m not really grokking is like how to make the shadow more intense.
Yeah it’s not as straightforward as other systems like CSS but for a reason (see linked thread with response from Romain)
Though the developer/designer need is real!
r
With higher elevation your shadow will spread more but will also get “brighter”
Here’s a quick and dirty visualization using a 3D renderer so you understand what happens:
1
The white rectangle is the light source, the circle our image
This is with a low elevation (circle close to the background)
Now with high elevation:
🙏 2
😍 2
You can see how the shadow spreads more but become more diffuse/fuzzy
(the dark area — the umbra — is not visible in your case because the “camera” is pointing straight down)
c
How does one change the light source / “camera” position?
r
You can’t change the camera position but you can move the light source since API 28 I think?
1 sec
Ah it requires to create your own
HardwareRenderer
Copy code
val renderer = HardwareRenderer()
renderer.setSurface(targetSurface)
renderer.setContentRoot(rootRenderNode)
// Ambient shadow, direct shadow
renderer.setLightSourceAlpha(0.1f, 0.5f)

// Position X, Y, Z, radius
renderer.setLightSourceGeometry(
        width() / 2f, 0f, toPx(600f), toPx(800f))
👀 4
a
I think that makes sense from a purely physics point of view. It almost feels like rather than moving the light source position, I want to like...increase the intensity of the light. That feels like it would make the shadow more itense?
1
Also I added a new word to my vocabulary: umbra! 🎉
🌘 1
Also those visualizations are great/very helpful, thank you!
r
Increasing the light intensity won’t make the shadow darker no