Slide 1

Slide 1 text

Uplift Material shadows on Android Sebastiano Poggi @seebrock3r JetBrains

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

• 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

Slide 6

Slide 6 text

• 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

Slide 7

Slide 7 text

• 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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

Let there be light

Slide 13

Slide 13 text

Let there be light umbra penumbra vs

Slide 14

Slide 14 text

Let there be light umbra penumbra vs

Slide 15

Slide 15 text

Let there be light umbra penumbra vs

Slide 16

Slide 16 text

Ambient shadow Spot shadow

Slide 17

Slide 17 text

Ambient shadow Spot shadow

Slide 18

Slide 18 text

Ambient shadow Spot shadow

Slide 19

Slide 19 text

Spot shadows movement

Slide 20

Slide 20 text

Spot shadows movement

Slide 21

Slide 21 text

Spot shadows movement

Slide 22

Slide 22 text

Spot shadows movement

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

• 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

Slide 26

Slide 26 text

• 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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

• 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

Slide 32

Slide 32 text

• 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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

Manipulating outlines • You can swap and tweak outlines • Custom ViewOutlineProvider • Many possibilities • Change shadow shape • Shadow size and position • Look out for rendering artefacts

Slide 41

Slide 41 text

• 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

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

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

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

• 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

Slide 46

Slide 46 text

#000000 [ ] Shadows opacity

Slide 47

Slide 47 text

[ ] 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

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

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

Slide 51

Slide 51 text

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

Slide 52

Slide 52 text

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

Slide 53

Slide 53 text

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

Slide 54

Slide 54 text

• 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

Slide 55

Slide 55 text

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

Slide 56

Slide 56 text

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

Slide 57

Slide 57 text

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

Slide 58

Slide 58 text

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

Slide 59

Slide 59 text

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

Slide 60

Slide 60 text

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

Slide 61

Slide 61 text

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

Slide 62

Slide 62 text

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

Slide 63

Slide 63 text

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

Slide 64

Slide 64 text

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

Slide 65

Slide 65 text

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

Slide 66

Slide 66 text

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)

Slide 67

Slide 67 text

• 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?

Slide 68

Slide 68 text

What about pre-P?

Slide 69

Slide 69 text

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

Slide 70

Slide 70 text

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

Slide 71

Slide 71 text

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

Slide 72

Slide 72 text

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

Slide 73

Slide 73 text

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+

Slide 74

Slide 74 text

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+

Slide 75

Slide 75 text

Tying it all together

Slide 76

Slide 76 text

Tying it all together

Slide 77

Slide 77 text

Tying it all together

Slide 78

Slide 78 text

Tying it all together

Slide 79

Slide 79 text

Tying it all together Search “uplift”

Slide 80

Slide 80 text

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

Slide 81

Slide 81 text

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!

Slide 82

Slide 82 text

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!

Slide 83

Slide 83 text

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

Slide 84

Slide 84 text

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

Slide 85

Slide 85 text

THANK YOU

Slide 86

Slide 86 text

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