-
Notifications
You must be signed in to change notification settings - Fork 376
Shadows
ZieIony edited this page Feb 5, 2017
·
2 revisions
Views in Material Design are flat, of simple shape, cannot bend or fold. Shadows can be only black, rectangular or oval and work only on flat surfaces. These rules make the shadow casting pretty simple.
Basically a shadow is a blurred, black shape of a view. It means that it's possible to backport shadows using these steps:
- Take a view.
- Create an offscreen bitmap. It's size should be a little bigger than the view.
- Draw the view to that bitmap using LightingColorFilter set to 0,0. It should give you a black shape of that view.
- Blur the bitmap. Blur's radius should be a function of an elevation of the view. There are many different methods of blurring. Probably the best of them is to use RenderScript and ScriptIntrisincBlur.
- Draw the blurred bitmap.
- Draw the view on the bitmap.
The algorithm is fine, but too computation-heavy to work. Carbon actually:
- Draws shapes, not actual views.
- Reuses shadows.
- Uses 9-patches to blur and draw only as little as it's needed.
There are few things to note:
- Shadows have to be drawn not by the view, but by it's parent. On older platforms it's impossible to draw outsize view's bounds correctly.
- RenderScript doesn't work on some devices. In such case you have to use software mode. It should turn on automatically when RenderScript is not an option.
- Lollipop uses a different method.
- On API 21+ native shadows are used.
Copyright 2015 Marcin Korniluk 'Zielony'