graphics in android 1 fall 2012 cs2302: programming principles
TRANSCRIPT
1
Graphics in Android
Fall 2012 CS2302: Programming Principles
Canvas
Class android.graphics.Canvas represents a surface on which to draw
Some of the useful drawing methods1. drawArc draws an arc of a circle. Can be used for 'pie
segments' as well.
2. drawBitmap draws an image represented as a bitmap
3. drawCircle
4. drawOval
5. drawRect
6. drawText
7. drawColor fills the background of the canvas with a color
Fall 2012 CS2302: Programming Principles2
Path
Fall 2012 CS2302: Programming Principles3
A Path can, for example, be used to build up a polygon. A path is understood as being drawn by a pen. When the pen is
touching the drawing surface, a visible trace is left. However, the pen can also be moved without leaving a visible mark.
At any stage during the construction of a path, there is a current point. This is the current position of the pen.
The lineTo moves the pen to a new point, drawing a straight line from the previous current point to the point given by the parameters to the method call. The end point of the line becomes the new current point.
By contrast, the moveTo method moves the pen, changing the current point, but does not leave a trace. The path should begin with a moveTo call so that the path can start at a well determined position.
The close adds a line segment from the current point back to the first point on the path. This signals that the path is closed and, therefore, can be filled with color.
Paint
Fall 2012 CS2302: Programming Principles4
In Android, the android.graphics.Paint class is used to contain many details of how drawing should be done. Most drawing methods require a Paint object be provided as an argument.
The color of the drawing is one property. The style property is specified by a value of
class Paint.Style. The three values specify whether the shape is to be outlined, filled or both filled and outlined.