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. Uplift
    Material shadows
    on Android
    Sebastiano
    Poggi
    @seebrock3r
    JetBrains

    View Slide

  2. • Design system
    • Design system system
    • Material Theming
    • Physical metaphor
    • Paper and ink
    • Not skeuomorphism
    Material Design

    View Slide

  3. • Magical sheets of material
    • Metaphor of physical paper
    • Fixed shape
    • Any shape, any size
    • React to lights, cast shadows
    What is THE Material?

    View Slide

  4. • Material sheets can move around
    • E.g., bottom sheets, FABs, etc
    • They can sometimes change “parent”
    • E.g., shared element transitions
    Meaningful motion

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  8. • The Z coordinate is called elevation
    • android:elevation
    • Elevation is measured in dp
    Elevation
    z
    z = 5dp
    • Animated with translationZ
    translationZ = 3dp
    +

    View Slide

  9. Elevation
    z
    z = 5dp
    translationZ = 3dp
    +
    • The Z coordinate is called elevation
    • android:elevation
    • Elevation is measured in dp
    • Animated with translationZ

    View Slide

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

    View Slide

  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

    View Slide

  12. Let there be light

    View Slide

  13. Let there be light
    umbra
    penumbra
    vs

    View Slide

  14. Let there be light
    umbra
    penumbra
    vs

    View Slide

  15. Let there be light
    umbra
    penumbra
    vs

    View Slide

  16. Ambient shadow
    Spot shadow

    View Slide

  17. Ambient shadow
    Spot shadow

    View Slide

  18. Ambient shadow
    Spot shadow

    View Slide

  19. Spot shadows movement

    View Slide

  20. Spot shadows movement

    View Slide

  21. Spot shadows movement

    View Slide

  22. Spot shadows movement

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  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

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  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

    View Slide

  46. #000000
    [
    ]
    Shadows opacity

    View Slide

  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

    View Slide

  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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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)

    View Slide

  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?

    View Slide

  68. What about pre-P?

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  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+

    View Slide

  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+

    View Slide

  75. Tying it all together

    View Slide

  76. Tying it all together

    View Slide

  77. Tying it all together

    View Slide

  78. Tying it all together

    View Slide

  79. Tying it all together
    Search “uplift”

    View Slide

  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

    View Slide

  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!

    View Slide

  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!

    View Slide

  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

    View Slide

  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

    View Slide

  85. THANK
    YOU

    View Slide

  86. Uplift
    Material shadows
    on Android
    Sebastiano Poggi
    @seebrock3r JetBrains
    Slides at: bit.ly/uplift-slides

    View Slide