SlideShare ist ein Scribd-Unternehmen logo
1 von 178
It’s the arts!
Playing with the Android canvas
(or Let’s have a pretext to vandalize art)
Today
Introduction to canvas
Composing and transforming
Animations!
Today
Some art vandalization!
But first, setup things:
http://sergiandreplace.com/canvas
Introducing
The Canvas
How we paint things in Android?
Open GL
(too complex)
Renderscript
(using a hammer to crack a nut)
(and not really for drawing)
Canvas
(quick and dirty)
What is exactly a “canvas”?
A Canvas works for you as a pretense, or
interface, to the actual surface upon which your
graphics will be drawn — it holds all of your
"draw" calls
Tl;dr: Canvas is the class that translates
drawing actions into a Bitmap
To draw something you need 4 components
(almost always)
A Bitmap to hold the pixels
a Canvas to host the draw calls
(writing into the bitmap)
a drawing primitive
(e.g. Rect, Path, text, Bitmap)
and a Paint
(to describe the colors and styles for the
drawing)
Tag 1_01_start
Let’s put a smile on
that face
Oh, hello beauty!
Remember
Bitmap + Canvas + Paint +Primitive
=
Drawing
Oh, crap!
Let’s improve that smile
Much better,
but…
a bit too straight
RectF allows to reuse coordinates
Bellisima!
but…
Tag start_1_02
In this case, the Paint is not needed
Why so serious?
Here comes the cape crusader!
Let’s get crazy with the rects!
Let’s talk about Bitmaps
Loading a bitmap
But…
A loaded Bitmap is immutable
We can’t draw in it!
Making it mutable
Making it mutable
What?
Bitmaps must be recycled
Recycle marks it as GCable
Still exists, but we can’t draw
Bitmaps are huge
Stored in memory with no compression
java.lang.OutOfMemoryError
(boo!)
Why to load a huge bitmap
if target draw is small?
Branch 1_03_Bitmaps
Composing
&
Transforming
Painting on the activity is cool, but…
Painting views is cooler
Anatomy of a drawing view
1. Create needed objects
2. Measure
3. Draw accordingly
4. Repeat 2 & 3 when needed
The onDraw method
onDraw is executed everytime the view
needs to be redrawn
(or a part of it)
We can request a onDraw with invalidate()
(or postInvalidate if not in UI thread)
onDraw receives a ready-to-use-canvas
Tag 2_01_start
MyFirstDrawingView extends from ImageView
But it adds no behaviour. To work!
Understand drawing order
1. ImageView add src
1. ImageView add src
2. MyFirstDrawingView adds a Vignette
3. View class draws background
1. ImageView add src
2. MyFirstDrawingView adds a Vignette
3. View class draws background
1. ImageView add src
2. MyFirstDrawingView adds a Vignette
Introducing Shaders
Basically, shaders modify the behaviour
of Paint object
BitmapShader
Allows to tile a Bitmap
LinearGradient
RadialGradient
SweepGradient
Guess what they do…
ComposeShader allows to mix two shaders
with an Xfermode
(a really crazy thing)
Paint.setShader(shader)
Let’s combine this!
We want our view
to vignette the
image set in the
layout
You have to:
Create a RadialGradient
(psst, getMeasuredHeight & getMeasuredWidth)
Assign to the Paint object
Use canvas.drawPaint(Paint) to fill it
Pro:
Avoid instantiate in the onDrawMethod
(onSizeChanged is your friend)
Use the drawable size, not the View size
Check ScaleMode and Padding behaviour
And now PorterDuffXMode
PorterDuffModes define images will blend
Don’t worry, trial and error
We could also save a layer
Saving layers allows us to make
transformations
Example
Save layer
Transform canvas 45º
Draw square
Restore canvas
More on transformations later on
We can setup a Paint on saving
It will be applied on restoring
Branch 2_03_apples_on_apples
Create a new bitmap to hold the image
Create a temporary canvas for it
Draw the imageView image on the canvas
Save the canvas using a paint with the PorterDuff
setup the bounds of the drawable mask
Draw the mask on the temporary canvas
Restore the temporary canvas
Draw the bitmap on the real canvas
Pro:
Optimize it to avoid instatiation in the onDraw
Play around with ratios
Transformations
Mainly matrixes and camera
Stoping too obvious jokes before is too late
Remember matrixes from school?
Haha. Don’t worry
But remember
Operations order is important
Matrixes are used for
Translate
Rotate
Scale
Skew
But
Matrix.setRotate
Matrix.setScale
Only scales
We must use preOperation
and postOperation
to concatenat
So
Matrix.preRotate
Matrix.setScale
Rotates and scales
So
Matrix.setRotate
Matrix.postScale
Rotates and scales
Canvas has unity matrix by default
Be careful!
Translate
+
rotate
Rotate
+
translate
Translate
+
rotate
Rotate
+
translate
Translate
+
rotate
Rotate
+
translate
Translate
+
rotate
Rotate
+
translate
Translate
+
rotate
Rotate
+
translate
We can
Canvas.save()
Canvas.setMatrix(matrix)
Canvas.drawSomething()
Canvas.restore()
Or
DrawBitmap with matrix
Cameras allow us to create 3D matrixes
So 3D transformations
(axes x, y and z + perspective)
We perform transformations on camera
And then, we get the corresponding matrix
Tag 2_03_start
First part objective
Make an image turn
You have to:
Calculate center
Create a matrix
Set the rotation of the matrix
Create a new Bitmap as big as view
Create the canvas of that Bitmap
Draw image on the canvas with the matrix
Draw the resulting bitmap on the main canvas
Pro:
Connect a seekBar with the rotation (wow!)
Do it without and with optimization
(is it noticeable?)
Remember!
All these operations and stuff is cute
Combined are WOW!
Animations
We are not going to talk about
Animation or Animator class
But how to move all the things we learned
Basically, a onPaint calling invalidate()
Things to keep in mind
Speed
We want to achieve as FPS as possible
NO INSTATIATION!!
Flags everywhere!
Animating
Started
Folded
…
Create appropiate flags for the status
(boolean or enum, up to you)
Create controls methods
(start, stop, rewind, whatever)
Time between onDraw calls changes
startTime
deltaTime
duration
System.currentTimeInMillis
deltaTime = System.currentTimeMillis() - startTime;
progress = (float) deltaTime / duration);
Animation pattern
(well, my pattern)
Make the methods setup the states
In the onDraw
If/else treating states
Keep in mind what to draw after
& before the animation
Cache, cache and cache
Include also animating states
Animating states should change flags
and/or call invalidate
Animating states must calculate progress
Interpolators
Used to add realism to the animations
Time
Animation progress
Boooooooooooring
Time
Animation progress
Oooooooh!
Time
Animation progress
OOOOOOOOOOHHH!!!!
Time
Animation progress
So, interopolators are just functions
f(delta time) = interpolated time
deltaTime = System.currentTimeMillis() - startTime;
progress = (float) deltaTime / duration);
deltaTime = System.currentTimeMillis() - startTime;
interpolatedTime = interpolator.getInterpolation
((float) deltaTime / duration);
Tag 3_01_start
Newspaper effect!
Newspaper effect!
Follow the pattern and fill it up comments
Pro:
Make things parametized
Add stop/pause
Make real effect (add scale and alpha)
Did you finish the exercise?
So we are finished
There are many other things in Canvas
Believe me
Just check drawing text stuff
Whoever made the TextView is my hero
Whoever made EditText is my God
Hope you enjoyed
Willing to see the things you make with all this
Share them in the GDG group!
It's the arts! Playing around with the Android canvas

Weitere ähnliche Inhalte

Was ist angesagt?

How to make a gif on gimp
How to make a gif on gimpHow to make a gif on gimp
How to make a gif on gimp
CuteGeekyGirl
 

Was ist angesagt? (11)

Multimedia chapter 5
Multimedia chapter 5Multimedia chapter 5
Multimedia chapter 5
 
Development of a 3d animated short film using 3d animation technique
Development of a 3d animated short film using 3d animation techniqueDevelopment of a 3d animated short film using 3d animation technique
Development of a 3d animated short film using 3d animation technique
 
Types of animation
Types of animationTypes of animation
Types of animation
 
Basic Concepts of Animation
Basic Concepts of AnimationBasic Concepts of Animation
Basic Concepts of Animation
 
Animation
AnimationAnimation
Animation
 
Deepak
DeepakDeepak
Deepak
 
Object Oriented 3D Animation Pipeline
Object Oriented 3D Animation PipelineObject Oriented 3D Animation Pipeline
Object Oriented 3D Animation Pipeline
 
How to make a gif on gimp
How to make a gif on gimpHow to make a gif on gimp
How to make a gif on gimp
 
3D Animation Process and Workflow
3D Animation Process and Workflow3D Animation Process and Workflow
3D Animation Process and Workflow
 
Why you need game engine1.pptx
Why you need game engine1.pptxWhy you need game engine1.pptx
Why you need game engine1.pptx
 
2
22
2
 

Andere mochten auch (9)

[Android] 2D Graphics
[Android] 2D Graphics[Android] 2D Graphics
[Android] 2D Graphics
 
Android data binding
Android data bindingAndroid data binding
Android data binding
 
Introduction to Canvas
Introduction to CanvasIntroduction to Canvas
Introduction to Canvas
 
Admob y yo
Admob y yoAdmob y yo
Admob y yo
 
Realm or: How I learned to stop worrying and love my app database
Realm or: How I learned to stop worrying and love my app databaseRealm or: How I learned to stop worrying and love my app database
Realm or: How I learned to stop worrying and love my app database
 
Android master class
Android master classAndroid master class
Android master class
 
Introducción a mobclix
Introducción a mobclixIntroducción a mobclix
Introducción a mobclix
 
My way to clean android (EN) - Android day salamanca edition
My way to clean android (EN) - Android day salamanca editionMy way to clean android (EN) - Android day salamanca edition
My way to clean android (EN) - Android day salamanca edition
 
Introduction to Android Fragments
Introduction to Android FragmentsIntroduction to Android Fragments
Introduction to Android Fragments
 

Ähnlich wie It's the arts! Playing around with the Android canvas

controlling_animations
controlling_animationscontrolling_animations
controlling_animations
tutorialsruby
 
controlling_animations
controlling_animationscontrolling_animations
controlling_animations
tutorialsruby
 
In this exercise, youre going to begin building a countdown timer..docx
In this exercise, youre going to begin building a countdown timer..docxIn this exercise, youre going to begin building a countdown timer..docx
In this exercise, youre going to begin building a countdown timer..docx
sleeperharwell
 
Crafting interactions with Core Animations, David Ortinau
Crafting interactions with Core Animations, David OrtinauCrafting interactions with Core Animations, David Ortinau
Crafting interactions with Core Animations, David Ortinau
Xamarin
 
Advanced animation techniques
Advanced animation techniquesAdvanced animation techniques
Advanced animation techniques
Charles Flynt
 
Day 1 presentation terminology
Day 1 presentation   terminologyDay 1 presentation   terminology
Day 1 presentation terminology
kelv_w
 

Ähnlich wie It's the arts! Playing around with the Android canvas (20)

Seven Peaks Speaks - Android Jetpack Compose Animation
Seven Peaks Speaks - Android Jetpack Compose AnimationSeven Peaks Speaks - Android Jetpack Compose Animation
Seven Peaks Speaks - Android Jetpack Compose Animation
 
Graphics on the Go
Graphics on the GoGraphics on the Go
Graphics on the Go
 
Advanced Game Development with the Mobile 3D Graphics API
Advanced Game Development with the Mobile 3D Graphics APIAdvanced Game Development with the Mobile 3D Graphics API
Advanced Game Development with the Mobile 3D Graphics API
 
Android animation in android-chapter17
Android animation in android-chapter17Android animation in android-chapter17
Android animation in android-chapter17
 
Animation
AnimationAnimation
Animation
 
controlling_animations
controlling_animationscontrolling_animations
controlling_animations
 
controlling_animations
controlling_animationscontrolling_animations
controlling_animations
 
In this exercise, youre going to begin building a countdown timer..docx
In this exercise, youre going to begin building a countdown timer..docxIn this exercise, youre going to begin building a countdown timer..docx
In this exercise, youre going to begin building a countdown timer..docx
 
Animation
AnimationAnimation
Animation
 
Custom View
Custom ViewCustom View
Custom View
 
Animation
AnimationAnimation
Animation
 
A Simple 3D Graphics Engine Written in Python and Allegro
A Simple 3D Graphics Engine Written in Python and AllegroA Simple 3D Graphics Engine Written in Python and Allegro
A Simple 3D Graphics Engine Written in Python and Allegro
 
Crafting interactions with Core Animations, David Ortinau
Crafting interactions with Core Animations, David OrtinauCrafting interactions with Core Animations, David Ortinau
Crafting interactions with Core Animations, David Ortinau
 
Advanced animation techniques
Advanced animation techniquesAdvanced animation techniques
Advanced animation techniques
 
Fast but not furious: debugging user interaction performance issues
Fast but not furious: debugging user interaction performance issuesFast but not furious: debugging user interaction performance issues
Fast but not furious: debugging user interaction performance issues
 
Day 1 presentation terminology
Day 1 presentation   terminologyDay 1 presentation   terminology
Day 1 presentation terminology
 
Ch07
Ch07Ch07
Ch07
 
Creating an Uber Clone - Part IV - Transcript.pdf
Creating an Uber Clone - Part IV - Transcript.pdfCreating an Uber Clone - Part IV - Transcript.pdf
Creating an Uber Clone - Part IV - Transcript.pdf
 
How to implement react native animations using animated api
How to implement react native animations using animated apiHow to implement react native animations using animated api
How to implement react native animations using animated api
 
Intro to Dimension
Intro to DimensionIntro to Dimension
Intro to Dimension
 

Mehr von Sergi Martínez

Mehr von Sergi Martínez (8)

Kotlin, a modern language for modern times
Kotlin, a modern language for modern timesKotlin, a modern language for modern times
Kotlin, a modern language for modern times
 
What is flutter and why should i care?
What is flutter and why should i care?What is flutter and why should i care?
What is flutter and why should i care?
 
What is flutter and why should i care? Lightning talk
What is flutter and why should i care? Lightning talkWhat is flutter and why should i care? Lightning talk
What is flutter and why should i care? Lightning talk
 
Let’s talk about star wars with Dialog Flow
Let’s talk about star wars with Dialog FlowLet’s talk about star wars with Dialog Flow
Let’s talk about star wars with Dialog Flow
 
Database handling with room
Database handling with roomDatabase handling with room
Database handling with room
 
Smartphones
SmartphonesSmartphones
Smartphones
 
Creating multillingual apps for android
Creating multillingual apps for androidCreating multillingual apps for android
Creating multillingual apps for android
 
Píldoras android i. Intro - 2ª parte
Píldoras android i. Intro - 2ª partePíldoras android i. Intro - 2ª parte
Píldoras android i. Intro - 2ª parte
 

Kürzlich hochgeladen

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Kürzlich hochgeladen (20)

Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 

It's the arts! Playing around with the Android canvas