A somewhat well-known issue in Android, since Lollipop and the introduction of Material Design, is that elevated View
s with see-through backgrounds reveal ugly artifacts from their shadow draws behind them.
Those visual glitches are all caused by the shadows being adjusted for increasing elevation by kind of "zooming out" on the gradients, which causes their inner borders to shrink inward within the View
's bounds, normally unseen in ones with opaque backgrounds. No clipping is done there, and every relevant check I've come across in the source turns the shadow off completely when that artifact would be seen, so that clipping is seemingly omitted intentionally, likely for efficiency.
As you might expect, many questions have been posted about it here over the years, but turning it off or somehow avoiding it altogether seem to be about the only generally effective solutions other users have found, as well:
- How to set semi transparent background color for Android CardView?
- CardView background alpha colour not working correctly
- Cardview - rounded corners with transparent background
- Elevation + transparency bug on Android Lollipop
- How to create a shape with a transparent background and a shadow, but the shadow should not be visible behind the shape outline?
- CardView with transparent background issue on > API 21
- Weird view behavior when an elevation and a transparent background color are used on API 21
- Remove background colour from FloatingActionButton
- Android Floating Action Button Semi Transparent Background Color
- How to set border and background color of Floating Action Button with transparency through xml?
- How to center image in FloatingActionButton behind transparent background?
- How to remove those dark circular background from floating action button?
- Android transparency and Shadows
- a shadow appear below material button after setting background color tint in android
- Multiple layers of shadows in android recyclerview elevation
- How to adjust shadow in translucent navbar?
- Transparent white button looks bad in Android Material Design
Various bespoke workarounds are available for a few common setups, some users ignore it or don't realize what it is, and some have even integrated the effect into their designs, but I still have not found a single example where it's been truly fixed. I haven't gone digging into the native graphics code yet, but I also can't find any instance in the SDK source where anything is done about it other than disabling the shadow when that might be visible, and if we're not able to do it at the app level it doesn't really matter what the low-level graphics stuff can do.
There doesn't seem to be much out there about the general problem, but recently I'd shared some information about it on this old question concerning CardView
, including a couple of basic techniques for creating clipped shadow replicas as replacements. However, the examples in that answer are quite specific, and adjusting them for multiple different View
s would be tedious and error-prone, and would likely require some not-insignificant modifications to an existing setup in order to add them.
Is there any way to apply those workarounds generally, and with minimal changes?