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

4580c218737149bf44d012a110612010?s=128

Sebastiano Poggi

April 23, 2019
Tweet

Transcript

  1. Uplift Material shadows on Android Sebastiano Poggi @seebrock3r JetBrains

  2. • Design system • Design system system • Material Theming

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

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

    FABs, etc • They can sometimes change “parent” • E.g., shared element transitions Meaningful motion
  5. • 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
  6. • 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
  7. • 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
  8. • The Z coordinate is called elevation • android:elevation •

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

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

    Positioned in screen coordinates • A point light • 600dp above the screen, 800dp wide
  11. 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
  12. Let there be light

  13. Let there be light umbra penumbra vs

  14. Let there be light umbra penumbra vs

  15. Let there be light umbra penumbra vs

  16. Ambient shadow Spot shadow

  17. Ambient shadow Spot shadow

  18. Ambient shadow Spot shadow

  19. Spot shadows movement

  20. Spot shadows movement

  21. Spot shadows movement

  22. Spot shadows movement

  23. Spot shadows movement The spot shadow “goes down” relative to

    its caster as the Y coordinate increases
  24. Spot shadows movement The spot shadow “goes down” relative to

    its caster as the Y coordinate increases
  25. • 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
  26. • 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
  27. Spot shadows and elevation elevation: 2 dp *not on scale

  28. elevation: 2 dp delta: *not on scale Spot shadows and

    elevation
  29. elevation: 2 dp delta: elevation: 10 dp *not on scale

    Spot shadows and elevation
  30. elevation: 2 dp delta: elevation: 10 dp *not on scale

    delta: Spot shadows and elevation
  31. • 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
  32. • 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
  33. 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
  34. 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
  35. 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
  36. • You can swap and tweak outlines • Custom ViewOutlineProvider

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

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

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

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

    Custom ViewOutlineProvider • Many possibilities • Change shadow shape • Shadow size and position • Look out for rendering artefacts
  41. • 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
  42. Coloured shadows #000000 #FF0000 • New APIs in Android Pie

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

    (API 28) • Set the spot and ambient shadow colours • outlineAmbientShadowColor & outlineSpotShadowColor
  44. 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
  45. • 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
  46. #000000 [ ] Shadows opacity

  47. [ ] 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
  48. 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
  49. Colour and opacity • “Right” alpha depends on colours •

    Both shadow and background
  50. Colour and opacity • “Right” alpha depends on colours •

    Both shadow and background
  51. Colour and opacity • “Right” alpha depends on colours •

    Both shadow and background
  52. Colour and opacity • “Right” alpha depends on colours •

    Both shadow and background
  53. 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
  54. • 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
  55. 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
  56. 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
  57. 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
  58. 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
  59. 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
  60. 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
  61. 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
  62. 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
  63. 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
  64. 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
  65. 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
  66. 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)
  67. • 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?
  68. What about pre-P?

  69. What about pre-P? • Use resource overrides for theme

  70. What about pre-P? • Use resource overrides for theme •

    Base theme in res/values
  71. What about pre-P? • Use resource overrides for theme •

    Base theme in res/values • Theme for API 21–27 in res/values
  72. 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
  73. 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+
  74. 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+
  75. Tying it all together

  76. Tying it all together

  77. Tying it all together

  78. Tying it all together

  79. Tying it all together Search “uplift”

  80. 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
  81. 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!
  82. 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!
  83. 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
  84. 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
  85. THANK YOU

  86. Uplift Material shadows on Android Sebastiano Poggi @seebrock3r JetBrains Slides

    at: bit.ly/uplift-slides