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

Android Canvas in Depth

Android Canvas in Depth

As developers we always thrive to deliver the best experience in our apps, yet we've all been in situations where the Android UI views were not enough for our needs.

In this talk, we'll dive into custom drawing with Android Canvas, along with what happens behind the scenes. Showcasing some of Nik's latest work we will discover different manipulation techniques with the canvas and how it can be animated by the user's microphone input. This includes tips to remember when going custom, the pros and cons of custom drawing and how we can profile the GPU to optimise our apps.

Nikolai Shevchenko

October 22, 2019
Tweet

More Decks by Nikolai Shevchenko

Other Decks in Programming

Transcript

  1. Busuu 2019 3 Android dev for 5 years Love art

    Love animations and drawing Let’s talk Canvas and optimisation Nik Shevchenko
  2. Busuu 2019 4 Canvas is a the main class for

    drawing 2D graphics Part of android.graphics collection What Text, Lines, Ovals, Arcs, Bitmaps…. Where x, y coordinates How Paint object What is a Canvas Bitmap Canvas Button
  3. Busuu 2019 5 What is a Canvas Every View has

    a canvas fun onDraw(Canvas canvas) To redraw - invalidate() Rendering commands stored in DisplayList Sent to GPU for OpenGL to consume Bitmap Canvas Button
  4. Busuu 2019 6 What is a Canvas Every View has

    a canvas fun onDraw(Canvas canvas) To redraw - invalidate() Rendering commands stored in DisplayList Sent to GPU for OpenGL to consume CPU GPU Bitmap Canvas Button
  5. Busuu 2019 7 onDraw -> Dispaly List Drawn out: how

    Android renders (Google I/O '18) https://www.youtube.com/watch?v=zdQRIYOST64
  6. Busuu 2019 9 Layouts Quick but not always calls propagate

    in the hierarchy Prompt to overdraw Double Taxation
  7. Busuu 2019 11 Prompt to overdraw Painter's algorithm redrawing pixels

    Turn on in the settings Remove backgrounds Reducing transparency
  8. Busuu 2019 13 calls propagate in the hierarchy Prompt to

    overdraw Double Taxation Layouts Solutions - Tools Lint Layout inspector GPU overdraw settings Profile GPU Rendering Systrace
  9. Busuu 2019 17 Systrace App in Android tools Android 10+

    Perfetto UI adb pull /data/local/traces/
  10. Busuu 2019 31 Speech Recognition Animation Nothing similar out there

    Randomised waves from a circle Based on mic input 31
  11. Busuu 2019 32 1. Calculate random arcs around the circle

    2. Random heights for each arcs 3. Draw them 4. Animate them up and down 5. Clear & Repeat The steps
  12. Busuu 2019 33 1. Calculate random Arcs 2. Random heights

    for each arcs heights = angles.map { random(20) * voiceAmplitude } voiceAmplitude = [0 - 1]
  13. Busuu 2019 34 Draw Arcs private val path = Path()

    for (i in 0 until angles.size) { path.reset() // calculate oval path.addOval(oval, Path.Direction.CW) rotationAngle = currentAngle /2 + futureAngle /2 canvas.rotate(rotationAngle, center.x, center.y) canvas.drawPath(path, wavePaint) }
  14. Busuu 2019 35 Draw Arcs private val path = Path()

    for (i in 0 until angles.size) { path.reset() // calculate oval path.addOval(oval, Path.Direction.CW) rotationAngle = currentAngle /2 + futureAngle /2 canvas.rotate(rotationAngle, center.x, center.y) canvas.drawPath(path, wavePaint) }
  15. Busuu 2019 36 Draw Arcs + randomness Arcs -> Ovals

    Paint(ANTI_ALIAS_FLAG).apply { setStyle(Paint.Style.STROKE) setStyle(Paint.Style.FILL)
  16. Busuu 2019 40 Where are we... + Random Arcs +

    Random height for each arc + White circle on top + 3 layers with different + Animation
  17. Busuu 2019 46 Iterations + Add a “Dominant” and 2

    subdominants + Add microphone + Add Alpha and gradients
  18. Busuu 2019 47 Iterations + Add a “Dominant” and 2

    subdominants + Add microphone + Add Alpha and gradients + Slower and darker when moving
  19. Busuu 2019 48 Iterations + Add a “Dominant” and 2

    subdominants + Add microphone + Add Alpha and gradients + Slower and darker when moving Ship it!
  20. Busuu 2019 50 To remember Don’t allocate memory in onDraw()

    Preview in AS works after compiling - mock with isInEditMode Accessibility - setContentDescription() Don’t Canvas everything - business / performance needs
  21. Busuu 2019 Busuu 2019 51 Thanks for listening! Embrace custom

    drawing! Make it unique. Embrace open source! https://github.com/nshevchenko/SpeechWaves Come work with us - busuu.com/jobs Have a great evening! Nik