Putting it All Together
Summary
In this chapter, we have learned more than we actually promised at the beginning.
What we have learned is listed as follows:
• Planning our application and the use of story boards
• Creating a rich, complex user interface with nested layouts
• Creation of graphic objects using just XML
• Creation of our own separate View class and referencing to that class
in XML files and our main activity file code
• Use of the Path, Paint, Bitmap, and Canvas objects
• Defining listeners and capturing touch events
• Responding to those touch events
• Switching the color for the paint brush
• Working with a filesystem and output streams
• Saving files to the storage devices
• Adding permissions to the AndroidManifest.xml file
• Clearing the Canvas, drawing cache, and creating a new drawing
• Erasing the already drawn painting using very simple logic
We made it to the end of this book. This book only focuses on the very basics,
starting from level 0 to mid-level knowledge of working with Canvas for Android.
Still, the knowledge that is enclosed in this book will work for all kinds of basic 2D
Android graphic applications. For a more complex application with animation, we
might need advance-level knowledge of Android Canvas and some core knowledge
of working with 3D in OpenGL ES. OpenGL ES is a flavor of OpenGL for embedded
devices or handheld devices.
[ 90 ]
A Index
Android Canvas 5 C
Android Canvas application
Canvas
about 6 drawing on 28
Application Name 7
Package Name 7 class properties
Project Name 7 defining 61
SDK version, selecting 8
Target SDK 8 code file, application 15-19
Theme 8-13 color selection
Android Virtual Device (AVD) 12
ANR (Application Not Responding) 22 enabling 81-84
application configuration file, application 14
code file 15-19 constructor, custom-view class 63
configuration file 14 control panel
eraser, enabling 88, 89
layout file 14 designing 72
mining 14 createBitmap() method 27
new drawing, creating 88 custom-view class
painting, saving 85-88
Application development about 61
about 56-60 class properties, defining 61
custom-view class 61 constructor 63
final output 66, 67 drawing method 63
main activity class 66, 67 measurement method 64
Application Name, Android Canvas methods 63
objects, defining 61
application 7 touch event 64, 65
AsyncTask class 24
D
B
dm_touched variable 65
Bitmap object 86 draw9patch tool 46, 47
Drawables
about 31
from resource image 32-36
from resource XML 36-38
Drawing
on Canvas 28
on SurfaceView 30
on View 29
drawing method, custom-view class 63 N
drawing threads
need for 22 new drawing
draw() method 39 creating 88
E NinePatch image
about 43, 44
eraser creating 44-49
enabling, in application 88, 89 using 50-54
F O
FileOutputStream object 86 objects
final output 66, 67 defining 61
G onClick() method 83
onCreate() method 66, 81, 83, 85, 88
getHolder() method 30 onDraw() method 27, 29, 39, 78
onTouchEvent() method 64, 78
H OvalShape() method 38
Handler objects P
running, on UI thread 24
Package Name, Android Canvas
I application 7
ImageView object 33, 38 paint brush 72
inflate() method 36 paintClicked method 83
invalidate() method 29, 66, 79 paint_colors property 82
painting
L
saving 85-88
layout touch, used for 78, 79
setting up 73-77 pixels
about 55
layout file, application 14 Project Name, Android Canvas
lockCanvas() method 30
application 7
M
R
main activity class 66, 67
measurement method, custom-view class 64 resources object 38
multithreading run() method 23
issues, in Android 23 S
myPath object 86
savePaintingButton button 88
screen density 19
screen orientation 19, 72
screen size 19
SDK version
selecting 8
[ 92 ]
setColor() method 83 U
setLayoutParams method 33
setupDrawing() method 76, 78 UI thread
ShapeDrawable object 38 Handler objects, running 24
Shape Drawables 38, 39
SurfaceHandler object 30 unlockCanvasAndPost() method 30
SurfaceView user interface
drawing on 30 about 22, 71, 72
control panel, designing 72
T layout, setting up 73-77
paint brush 72
Target SDK, Android Canvas application 8 screen orientation 72
Theme, Android Canvas application 8-13
Thread class V
about 23 View
AsyncTask class 24 drawing on 29
Handler objects, running on UI thread 24
touch View object 86
enabling 78, 79
used, for painting 78, 79
touch event, custom-view class 64, 65
[ 93 ]
Thank you for buying
Learning Android Canvas
About Packt Publishing
Packt, pronounced 'packed', published its first book "Mastering phpMyAdmin for Effective
MySQL Management" in April 2004 and subsequently continued to specialize in publishing
highly focused books on specific technologies and solutions.
Our books and publications share the experiences of your fellow IT professionals in adapting
and customizing today's systems, applications, and frameworks. Our solution based books
give you the knowledge and power to customize the software and technologies you're using
to get the job done. Packt books are more specific and less general than the IT books you have
seen in the past. Our unique business model allows us to bring you more focused information,
giving you more of what you need to know, and less of what you don't.
Packt is a modern, yet unique publishing company, which focuses on producing quality,
cutting-edge books for communities of developers, administrators, and newbies alike.
For more information, please visit our website: www.packtpub.com.
About Packt Open Source
In 2010, Packt launched two new brands, Packt Open Source and Packt Enterprise, in order
to continue its focus on specialization. This book is part of the Packt Open Source brand,
home to books published on software built around Open Source licenses, and offering
information to anybody from advanced developers to budding web designers. The Open
Source brand also runs Packt's Open Source Royalty Scheme, by which Packt gives a royalty
to each Open Source project about whose software a book is sold.
Writing for Packt
We welcome all inquiries from people who are interested in authoring. Book proposals
should be sent to [email protected]. If your book idea is still at an early stage and you
would like to discuss it first before writing a formal book proposal, contact us; one of our
commissioning editors will get in touch with you.
We're not just looking for published authors; if you have strong technical skills but no writing
experience, our experienced editors can help you develop a writing career, or simply get some
additional reward for your expertise.
Android Development Tools for
Eclipse
ISBN: 978-1-78216-110-3 Paperback: 144 pages
Set up, build, and publish Android projects quickly
using Android Development Tools for Eclipse
1. Build Android applications using ADT
for Eclipse
2. Generate Android application skeleton code
using wizards
3. Advertise and monetize your applications
Android Studio Application
Development
ISBN: 978-1-78328-527-3 Paperback: 110 pages
Create visually appealing applications using the new
IntelliJ IDE Android Studio
1. Familiarize yourself with Android Studio IDE
2. Enhance the user interface for your app using
the graphical editor feature
3. Explore the various features involved
in developing an android app and
implement them
Please check www.PacktPub.com for information on our titles
Android Native Development Kit
Cookbook
ISBN: 978-1-84969-150-5 Paperback: 346 pages
A step-by-step tutorial with more than 60 concise
recipes on Android NDK development skills
1. Build, debug, and profile Android NDK apps
2. Implement part of Android apps in native
C/C++ code
3. Optimize code performance in assembly with
Android NDK
Android 4: New Features for
Application Development
ISBN: 978-1-84951-952-6 Paperback: 166 pages
Develop Android applications using the new features
of Android Ice Cream Sandwich
1. Learn new APIs in Android 4
2. Get familiar with the best practices in
developing Android applications
3. Step-by-step approach with clearly explained
sample codes
Please check www.PacktPub.com for information on our titles