5

As the documentation says, you can add and customize the material design shadow-style by android:elevation. This works fine with all elements I came across yet.

But what if I want to get the same (or nearly the same) behavior for shapes I draw on a Canvas? - Currently I'm playing around with Paint.setShadowLayer(), but the results are far from 'material'-style compared with the native implementation.

Are there any libraries or ways to achieve the same effect? Preferably by adding the elevation level?

rnrneverdies
  • 15,243
  • 9
  • 65
  • 95
malte
  • 1,171
  • 1
  • 15
  • 28

1 Answers1

4

The method Android Lollipop is using for rendering shadows is pretty easy:

  • draw black shape of a view
  • blur it with ScriptIntrisincBlur
  • draw it beneath the view

You can apply this method to your shapes. I've implemented these steps in my library capable of drawing realtime shadows on all recent Androids down to cupcake. You can find it here: https://github.com/ZieIony/Carbon/blob/master/carbon/src/main/java/carbon/widget/FrameLayout.java#L207

Edit: link to Android shadow drawing code: https://github.com/android/platform_frameworks_base/blob/4d9ffdf95237c4ee59514c678fea6ff157e0eee3/libs/hwui/OpenGLRenderer.cpp#L2507

Zielony
  • 16,239
  • 6
  • 34
  • 39
  • 1
    That is definitely not the method that Android uses. – Chris Banes Mar 24 '16 at 11:35
  • 1
    True, this is the method used for drop shadows (http://android-developers.blogspot.com/2013/08/renderscript-intrinsics.html). Could you write more about Lollipop shadows? – Zielony Mar 24 '16 at 12:07