4

A somewhat well-known issue in Android, since Lollipop and the introduction of Material Design, is that elevated Views with see-through backgrounds reveal ugly artifacts from their shadow draws behind them.

Four mock-ups with various Views showing the issue.

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:

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 Views 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?

ZedAlpha
  • 219
  • 2
  • 8
  • 2
    I had an answer here, but it grew to be too complex and ended up a library. I don't want to keep rewriting the README into a post suitable for this site every time I update, so it's [here](https://github.com/zed-alpha/shadow-gadgets), if anybody's interested. I'll leave the question (even though it's kind of awkwardly tailored to the now-deleted answer), since the list of links might be useful to someone. – ZedAlpha May 28 '22 at 02:02

0 Answers0