1

If I add an elevation to a small, wide view on Android 5.0, the shadow looks really weird. However, on Android 5.1 it looks ok. For an example, take a look at the following comparison image (please click to view at full resolution):

Comparison between 5.0 and 5.1

On 5.1 the shape of the shadow is uniform, while on 5.0 it gets thinner towards the edges of the screen. The thinner (i.e. smaller) the view, the more apparent the effect is.

I'm not sure if this is a bug on either version, however I need the shadow to look on both versions like it looks on 5.1 now.

Kirill Rakhman
  • 42,195
  • 18
  • 124
  • 148
  • not exactly sure how it looks "weird", could you please explain further – tyczj Sep 15 '15 at 18:29
  • Please take a look at the comparison image (click on it to view at full resolution). On 5.1 the shape of the shadow is uniform, while on 5.0 it gets thinner towards the edges of the screen. – Kirill Rakhman Sep 15 '15 at 18:33
  • The way shadows are rendered changed slightly between OS versions. There is no developer-facing API for modifying this behavior. You will not be able to change it short of avoiding using framework-generated shadows. – alanv Sep 17 '15 at 14:15

1 Answers1

1

Here's a hacky, partial workaround. The shape of the shadow depends on the height of the view or more precisely the height of the outline. So we're going to set a custom outline like so:

view.setOutlineProvider(new ViewOutlineProvider() {
    @Override
    public void getOutline(final View view, final Outline outline) {
        outline.setRect(0, -128, view.getWidth(), view.getHeight());
    }
});

Instead of -128 pick any sufficient large number (or even better, load a dimension value from the resources). Here's what it looks:

enter image description here

Unfortunately, this hack causes weird artifacts to be rendered above the view. However, those won't be visible if you have another view (like the toolbar) above it. Maybe it's possible to clip the artifacts by putting the view in a separate layout and playing around with its bounds.

Kirill Rakhman
  • 42,195
  • 18
  • 124
  • 148