Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Uplift — Material shadows on Android

Uplift — Material shadows on Android

In this session, we'll cover how elevation works on Android. From an overview of the available APIs, seeing what is new in Android P, and getting to the nitty gritty of how shadows are rendered on HWUI and Skia. Lastly, we'll learn how to take advantage of the existing APIs and work within their limitations to obtain original and unique results.

This talk follows the track of existing blog article(s) on the topic: https://blog.usejournal.com/playing-with-elevation-in-android-91af4f3be596 and its two follow-ups. There is also an open source app, Uplift, which will be used to showcase the effects as we talk about them: https://github.com/rock3r/uplift

Keynote file and PDF: https://www.dropbox.com/sh/8dwfli2yn8bghjb/AACgTQtOe3SWCDz6Lrb6ZDUia?dl=0
Screen recording available here: https://youtu.be/WQa8NWEeG5Y

Sebastiano Poggi

April 23, 2019
Tweet

More Decks by Sebastiano Poggi

Other Decks in Programming

Transcript

  1. • Design system • Design system system • Material Theming

    • Physical metaphor • Paper and ink • Not skeuomorphism Material Design
  2. • Magical sheets of material • Metaphor of physical paper

    • Fixed shape • Any shape, any size • React to lights, cast shadows What is THE Material?
  3. • Material sheets can move around • E.g., bottom sheets,

    FABs, etc • They can sometimes change “parent” • E.g., shared element transitions Meaningful motion
  4. • Magical ink on magical paper • Content on the

    sheets is ink • Ink has a colour, material is generally white • Ink can animate freely on its sheet A colourful system Lorem ipsum Dolor sit amet, consectetur adipiscing elit. Lorem ipsum, dolor sit amet
  5. • Everything exists in a physical space • Sheets have

    X and Y coordinates • But also a Z • The screen “background” is Z = 0 The environment x y z
  6. • Everything exists in a physical space • Sheets have

    X and Y coordinates • But also a Z • The screen “background” is Z = 0 • Sheets have Z >= 0 The environment x y z z = 5
  7. • The Z coordinate is called elevation • android:elevation •

    Elevation is measured in dp Elevation z z = 5dp • Animated with translationZ translationZ = 3dp +
  8. Elevation z z = 5dp translationZ = 3dp + •

    The Z coordinate is called elevation • android:elevation • Elevation is measured in dp • Animated with translationZ
  9. Let there be light • There are two lights •

    Positioned in screen coordinates • A point light • 600dp above the screen, 800dp wide
  10. Let there be light • There are two lights •

    Positioned in screen coordinates • A point light • 600dp above the screen, 800dp wide • An omnidirectional light • At infinite distance above the screen 420dp 800dp
  11. • Shadows are always black #000000 • Shadows are very

    subtle • Ambient shadow is 3.9% opaque • Spot shadow is 19% opaque • …except on TVs • 15% and 30% opaque respectively Shadows in Android normal TV
  12. • Android provides one obvious knob: elevation • Makes shadow

    smaller or larger • Elevation is the Z coordinate • Changes distance from sheet and spotlight • Impacts the Y parallax amount Controlling the shadow
  13. elevation: 2 dp delta: elevation: 10 dp *not on scale

    delta: Spot shadows and elevation
  14. • Android uses Outline since Lollipop • Outlines have a

    shape and an alpha • Outlines provide the shape • Shapes used for clipping, and light occlusion • …aka shadows • Views have ViewOutlineProviders Shadow shapes
  15. • A number of possible shapes • Empty, rounded rects,

    generic convex paths • Rects and ellipses are rounded rects • Views get outlines from their background Shadow shapes
  16. Drawable outlines Drawable ShapeDrawable BitmapDrawable • View backgrounds are Drawables

    • Different drawables handle outlines differently • The base Drawable has a zero-alpha rect • BitmapDrawables have a rect, too • Non-zero alpha only if fully opaque • ShapeDrawables match their shape
  17. Drawable outlines Drawable ShapeDrawable BitmapDrawable • LayerDrawable and TransitionDrawable use

    first wrapped bitmap with an outline, if any • StateListDrawable and LevelListDrawable use “current” outline • NinePatchDrawable uses user-defined optical bounds, or gets rounded rect from AAPT Transition/ LayerDrawable NinePatch Drawable State/Level ListDrawable
  18. Drawable outlines • InsetDrawable uses the outline of the wrapped

    drawable • DrawableWrappers use wrapped drawable outline, or the default zero-alpha bounds • RotateDrawable, ClipDrawable, ScaleDrawable Drawable ShapeDrawable BitmapDrawable Transition/ LayerDrawable NinePatch Drawable Inset Drawable State/Level ListDrawable Drawable
 Wrapper
  19. • You can swap and tweak outlines • Custom ViewOutlineProvider

    • Many possibilities • Change shadow shape • Shadow size and position Manipulating outlines
  20. Manipulating outlines • You can swap and tweak outlines •

    Custom ViewOutlineProvider • Many possibilities • Change shadow shape • Shadow size and position
  21. Manipulating outlines • You can swap and tweak outlines •

    Custom ViewOutlineProvider • Many possibilities • Change shadow shape • Shadow size and position
  22. Manipulating outlines • You can swap and tweak outlines •

    Custom ViewOutlineProvider • Many possibilities • Change shadow shape • Shadow size and position
  23. Manipulating outlines • You can swap and tweak outlines •

    Custom ViewOutlineProvider • Many possibilities • Change shadow shape • Shadow size and position • Look out for rendering artefacts
  24. • Material specs don’t impose a shadow colour • Shadows

    are always black in reality • There is no way to control the colour either • …or at least, not before Pie Coloured shadows #000000
  25. Coloured shadows #000000 #FF0000 • New APIs in Android Pie

    (API 28) • Set the spot and ambient shadow colours • outlineAmbientShadowColor & outlineSpotShadowColor
  26. Coloured shadows #000000 #FF0000 • New APIs in Android Pie

    (API 28) • Set the spot and ambient shadow colours • outlineAmbientShadowColor & outlineSpotShadowColor
  27. Coloured shadows #000000 #FF0000 • New APIs in Android Pie

    (API 28) • Set the spot and ambient shadow colours • outlineAmbientShadowColor & outlineSpotShadowColor • Colours should be opaque • But they still kinda look like crap
  28. • They look like crap because of low alpha •

    Alpha is controlled by the theme • Starting from Lollipop (API 21) • ambientShadowAlpha & spotShadowAlpha • Used to make shadows clearer in TV themes Normal TV Shadows opacity
  29. [ ] 600dp Light source distance Shadow colour (opaque) *

    = Shadow alpha (from theme) Final shadow colour (0, 0, 600) 0.19 #FF000000 #30000000 #FF0000 #000000 Shadows opacity
  30. Coloured shadows [ ] 600dp Light source distance Shadow colour

    (opaque) * = Shadow alpha (from theme) Final shadow colour (0, 0, 600) 0.19 #FF0000 #000000 #FFFF0000 #30FF0000
  31. Colour and opacity • Theme attributes have a fixed value

    • Cannot change at runtime (no APIs) • Applied to all views • “Right” alpha depends on colours • Both shadow and background
  32. • Difficult to have only some coloured shadows • Settings

    for one may not work for others Colour and opacity Checkout Cart Shipping & payment PURCHASE PURCHASE 123 Fake St, N1 2SA London, UK Amex ending in 1234 $ Express shipping — FREE Delivered the next day Broccoli In cart: 3 £ 3.45 £1.15 each Banana In cart: 7 £ 2.59 £0.37 each Total: £ 6.14
  33. Colour and opacity Checkout Cart Shipping & payment PURCHASE PURCHASE

    123 Fake St, N1 2SA London, UK Amex ending in 1234 $ Express shipping — FREE Delivered the next day Broccoli In cart: 3 £ 3.45 £1.15 each Banana In cart: 7 £ 2.59 £0.37 each Total: £ 6.14 • Difficult to have only some coloured shadows • Settings for one may not work for others
  34. Colour and opacity • Difficult to have only some coloured

    shadows • Settings for one may not work for others Checkout Cart Shipping & payment PURCHASE PURCHASE 123 Fake St, N1 2SA London, UK Amex ending in 1234 $ Express shipping — FREE Delivered the next day Broccoli In cart: 3 £ 3.45 £1.15 each Banana In cart: 7 £ 2.59 £0.37 each Total: £ 6.14
  35. Colour and opacity Checkout Cart Shipping & payment PURCHASE PURCHASE

    123 Fake St, N1 2SA London, UK Amex ending in 1234 $ Express shipping — FREE Delivered the next day Broccoli In cart: 3 £ 3.45 £1.15 each Banana In cart: 7 £ 2.59 £0.37 each Total: £ 6.14 • Difficult to have only some coloured shadows • Settings for one may not work for others
  36. Colour and opacity Checkout Cart Shipping & payment PURCHASE PURCHASE

    123 Fake St, N1 2SA London, UK Amex ending in 1234 $ Express shipping — FREE Delivered the next day Broccoli In cart: 3 £ 3.45 £1.15 each Banana In cart: 7 £ 2.59 £0.37 each Total: £ 6.14 • Difficult to have only some coloured shadows • Settings for one may not work for others • You can hack around it • Theme overlays may help • Most likely not worth the pain • There is a Better WayTM
  37. Colour and opacity • Difficult to have only some coloured

    shadows • Settings for one may not work for others • You can hack around it • Theme overlays may help • Most likely not worth the pain • There is a Better WayTM
  38. A Better WayTM to do coloured shadows • Two factors

    define the final shadow colour • The alpha, set by the theme ambientShadowAlpha and spotShadowAlpha • The colour set by outlineAmbientShadowColor and outlineSpotShadowColor [ ] 600dp Light source distance Shadow colour (opaque) * = Shadow alpha (from theme) Final shadow colour (0, 0, 600) 0.19 #FF000000 #30000000
  39. A Better WayTM to do coloured shadows • The theme

    affects everything • A pain to change per-view, no runtime control • Easy to get it wrong — themes can be confusing [ ] 600dp Light source distance Shadow colour (opaque) * = Shadow alpha (from theme) Final shadow colour (0, 0, 600) 0.19 #FF000000 #30000000
  40. A Better WayTM to do coloured shadows • Shadow colours

    are easier to use • Only change shadow colours for some views • We can also change them at runtime [ ] 600dp Light source distance Shadow colour (opaque) * = Shadow alpha (from theme) Final shadow colour (0, 0, 600) 0.19 #FF000000 #30000000
  41. A Better WayTM to do coloured shadows [ ] 600dp

    Light source distance Shadow colour (opaque) * 0.19 1.00 Shadow alpha (from theme) = #FF000000 #FFFF0000 #30000000 #FFFF0000 Final shadow colour (0, 0, 600) • Final alpha now fully controlled by colours’ alpha • The trick: set the alphas to 1.0
  42. A Better WayTM to do coloured shadows [ ] 600dp

    (0, 0, 600) Light source distance Shadow colour (opaque) * 1.00 0.19 Shadow alpha (from theme) = #FF000000 #FFFF0000 #60FF0000 #30000000 #FFFF0000 #60FF0000 Final shadow colour • Final alpha now fully controlled by colours’ alpha • The trick: set the alphas to 1.0
  43. A Better WayTM to do coloured shadows [ ] 600dp

    (0, 0, 600) Light source distance Shadow colour (opaque) * 1.00 Shadow alpha (from theme) = #FFFF0000 #60FF0000 #FFFF0000 #60FF0000 Final shadow colour • Final alpha now fully controlled by colours’ alpha • The trick: set the alphas to 1.0
  44. A Better WayTM to do coloured shadows [ ] 600dp

    (0, 0, 600) Light source distance Shadow colour (opaque) * 1.00 Shadow alpha (from theme) = #FFFF0000 #60FF0000 #FFFF0000 #60FF0000 Final shadow colour • Set black with alpha colours as shadow colour in theme • #0A000000 for outlineAmbientShadowColor (black, 3.9% alpha) • #30000000 for outlineSpotShadowColor (black, 19% alpha)
  45. • We only consider API 21+ • The only knobs

    are shadow alphas • Changing those alone is not useful • Things really look weird if you do What about pre-P?
  46. What about pre-P? • Use resource overrides for theme •

    Base theme in res/values • Theme for API 21–27 in res/values
  47. What about pre-P? • Use resource overrides for theme •

    Base theme in res/values • Theme for API 21–27 in res/values • Theme for API 28+ in res/values-v28
  48. What about pre-P? • Use resource overrides for theme •

    Base theme in res/values • Theme for API 21–27 in res/values • Theme for API 28+ in res/values-v28 • Only change shadows in API 28+
  49. res/values res/values-v28 What about pre-P? • Use resource overrides for

    theme • Base theme in res/values • Theme for API 21–27 in res/values • Theme for API 28+ in res/values-v28 • Only change shadows in API 28+
  50. One more thing… • Now we control all elevation shadows

    • [Insert maniacal laughter] • Can colour just some shadows • Or you can animate them • Animate translationZ (elevation) • Animate the colour/alpha
  51. Don’t try this at home • The ultimate hack: move

    the spotlight • Cannot move the ambient light • LITERALLY no good reason to move them • But we like having fun!
  52. Don’t try this at home • The ultimate hack: move

    the spotlight • Cannot move the ambient light • LITERALLY no good reason to move them • But we like having fun!
  53. Don’t try this at home • Use private theme attributes

    • Set *android:lightY • Asterisk bypasses AAPT validation • SERIOUSLY THOUGH — do not do this, ever! • It can break at any time • You cannot get anything good out of this
  54. Don’t try this at home • Use private theme attributes

    • Set *android:lightY • Asterisk bypasses AAPT validation • SERIOUSLY THOUGH — do not do this, ever! • It can break at any time • You cannot get anything good out of this DO NOT TRY THIS AT HOME