Android Programming Press on the image to return to the main documentation page.

Drawing (Core)

List of types:

Bitmap
BitmapDrawable
Canvas
ColorDrawable
GradientDrawable
Path
Rect
StateListDrawable

Bitmap

An object that holds a bitmap image. The bitmap can be loaded from a file or other input stream, or can be set from a different bitmap.
Loading large bitmaps can easily lead to out of memory exceptions. This is true even if the file is compressed and not large as the bitmap is stored uncompressed in memory.
For large images you can call InitializeSample and load a subsample of the image. The whole image will be loaded with a lower resolution.

Events:

None

Members:


  Crop (Left As Int, Top As Int, Width As Int, Height As Int) As Bitmap

  GetPixel (x As Int, y As Int) As Int

  Height As Int [read only]

  Initialize (Dir As String, FileName As String)

  Initialize2 (InputStream As java.io.InputStream)

  Initialize3 (Bitmap As android.graphics.Bitmap)

  InitializeMutable (Width As Int, Height As Int)

  InitializeResize (Dir As String, FileName As String, Width As Int, Height As Int, KeepAspectRatio As Boolean)

  InitializeSample (Dir As String, FileName As String, MaxWidth As Int, MaxHeight As Int)

  IsInitialized As Boolean

  Resize (Width As Float, Height As Float, KeepAspectRatio As Boolean) As Bitmap

  Rotate (Degrees As Float) As Bitmap

  Scale As Float [read only]

  Width As Int [read only]

  WriteToStream (OutputStream As java.io.OutputStream, Quality As Int, Format As android.graphics.Bitmap.CompressFormat)

Members description:

Crop (Left As Int, Top As Int, Width As Int, Height As Int) As Bitmap
Returns a new cropped bitmap.
GetPixel (x As Int, y As Int) As Int
Returns the color of the pixel at the specified position.
Height As Int [read only]
Returns the bitmap height.
Initialize (Dir As String, FileName As String)
Initializes the bitmap from the given file.
Note that the image will be downsampled if there is not enough memory available.
Example:
Dim Bitmap1 As Bitmap
Bitmap1.Initialize(File.DirAssets, "X.jpg")
Initialize2 (InputStream As java.io.InputStream)
Initializes the bitmap from the given stream.
Initialize3 (Bitmap As android.graphics.Bitmap)
Initializes the bitmap with a copy of the original image (copying is done if necessary).
InitializeMutable (Width As Int, Height As Int)
Creates a new mutable bitmap with the specified dimensions. You can use a Canvas object to draw on this bitmap.
InitializeResize (Dir As String, FileName As String, Width As Int, Height As Int, KeepAspectRatio As Boolean)
Initializes the bitmap and sets its size.
Note that the bitmap scale will be the same as the device scale.
InitializeSample (Dir As String, FileName As String, MaxWidth As Int, MaxHeight As Int)
Initializes the bitmap from the given file.
The decoder will subsample the bitmap if MaxWidth or MaxHeight are smaller than the bitmap dimensions.
This can save a lot of memory when loading large images.
Note that the actual dimensions may be larger than the specified values.
IsInitialized As Boolean
Resize (Width As Float, Height As Float, KeepAspectRatio As Boolean) As Bitmap
Returns a new bitmap with the given width and height.
Note that the bitmap scale will be the same as the device scale.
Rotate (Degrees As Float) As Bitmap
Returns a new rotated bitmap. The bitmap will be rotated clockwise.
Scale As Float [read only]
Returns the bitmap scale.
Width As Int [read only]
Returns the bitmap width.
WriteToStream (OutputStream As java.io.OutputStream, Quality As Int, Format As android.graphics.Bitmap.CompressFormat)
Writes the bitmap to the output stream.
Quality - Value between 0 (smaller size, lower quality) to 100 (larger size, higher quality),
which is a hint for the compressor for the required quality.
Format - JPEG or PNG.

Example:
Dim Out As OutputStream
Out = File.OpenOutput(File.DirRootExternal, "Test.png", False)
Bitmap1.WriteToStream(out, 100, "PNG")
Out.Close

BitmapDrawable

A drawable that draws a bitmap. The bitmap is set during initialization.
You can change the way the bitmap appears by changing the Gravity property.
Example:
Dim bd As BitmapDrawable
bd.Initialize(LoadBitmap(File.DirAssets, "SomeImage.png"))
bd.Gravity = Gravity.FILL
Activity.Background = bd

This is an 'Activity Object', it cannot be declared under Sub Process_Globals.

Events:

None

Members:


  Bitmap As android.graphics.Bitmap [read only]

  Gravity As Int

  Initialize (Bitmap As android.graphics.Bitmap)

  IsInitialized As Boolean

Members description:

Bitmap As android.graphics.Bitmap [read only]
Returns the internal Bitmap.
Gravity As Int
Gets or sets the gravity value. This value affects the way the image will be drawn.
Example:
BitmapDrawable1.Gravity = Gravity.FILL
Initialize (Bitmap As android.graphics.Bitmap)
IsInitialized As Boolean

Canvas

A Canvas is an object that draws on other views or (mutable) bitmaps.
When the canvas is initialized and set to draw on a view, a new mutable bitmap is created for that view background, the current view's background
is copied to the new bitmap and the canvas is set to draw on the new bitmap.
The canvas drawings are not immediately updated on the screen. You should call the target view Invalidate method to make it refresh the view.
This is useful as it allows you to make several drawings and only then refresh the display.
The canvas can be temporary limited to a specific region (and thus only affect this region). This is done by calling ClipPath. Removing the clipping is done by calling RemoveClip.
You can get the bitmap that the canvas draws on with the Bitmap property.
This is an 'Activity Object', it cannot be declared under Sub Process_Globals.

Events:

None

Members:


  AntiAlias As Boolean

  Bitmap As Bitmap [read only]

  ClipPath (Path1 As android.graphics.Path)

  DrawBitmap (Bitmap1 As android.graphics.Bitmap, SrcRect As android.graphics.Rect, DestRect As android.graphics.Rect)

  DrawBitmapFlipped (Bitmap1 As android.graphics.Bitmap, SrcRect As android.graphics.Rect, DestRect As android.graphics.Rect, Vertically As Boolean, Horizontally As Boolean)

  DrawBitmapRotated (Bitmap1 As android.graphics.Bitmap, SrcRect As android.graphics.Rect, DestRect As android.graphics.Rect, Degrees As Float)

  DrawCircle (x As Float, y As Float, Radius As Float, Color As Int, Filled As Boolean, StrokeWidth As Float)

  DrawColor (Color As Int)

  DrawDrawable (Drawable1 As android.graphics.drawable.Drawable, DestRect As android.graphics.Rect)

  DrawDrawableRotate (Drawable1 As android.graphics.drawable.Drawable, DestRect As android.graphics.Rect, Degrees As Float)

  DrawLine (x1 As Float, y1 As Float, x2 As Float, y2 As Float, Color As Int, StrokeWidth As Float)

  DrawOval (Rect1 As android.graphics.Rect, Color As Int, Filled As Boolean, StrokeWidth As Float)

  DrawOvalRotated (Rect1 As android.graphics.Rect, Color As Int, Filled As Boolean, StrokeWidth As Float, Degrees As Float)

  DrawPath (Path1 As android.graphics.Path, Color As Int, Filled As Boolean, StrokeWidth As Float)

  DrawPoint (x As Float, y As Float, Color As Int)

  DrawRect (Rect1 As android.graphics.Rect, Color As Int, Filled As Boolean, StrokeWidth As Float)

  DrawRectRotated (Rect1 As android.graphics.Rect, Color As Int, Filled As Boolean, StrokeWidth As Float, Degrees As Float)

  DrawText (Text As String, x As Float, y As Float, Typeface1 As android.graphics.Typeface, TextSize As Float, Color As Int, Align1 As android.graphics.Paint.Align)

  DrawTextRotated (Text As String, x As Float, y As Float, Typeface1 As android.graphics.Typeface, TextSize As Float, Color As Int, Align1 As android.graphics.Paint.Align, Degree As Float)

  Initialize (Target As android.view.View)

  Initialize2 (Bitmap As android.graphics.Bitmap)

  MeasureStringHeight (Text As String, Typeface As android.graphics.Typeface, TextSize As Float) As Float

  MeasureStringWidth (Text As String, Typeface As android.graphics.Typeface, TextSize As Float) As Float

  RemoveClip

Members description:

AntiAlias As Boolean
Gets or sets whether antialiasing will be applied.
Bitmap As Bitmap [read only]
Returns the bitmap that the canvas draws to.
Example: Saves the drawing to a file
Dim Out As OutputStream
Out = File.OpenOutput(File.DirRootExternal, "Test.png", False)
Canvas1.Bitmap.WriteToStream(out, 100, "PNG")
Out.Close
ClipPath (Path1 As android.graphics.Path)
Clips the drawing area to the given path.

Example: Fills a diamond shape with gradient color.
Dim Gradient1 As GradientDrawable
Dim Clrs(2) As Int
Clrs(0) = Colors.Black
Clrs(1) = Colors.White
Gradient1.Initialize("TOP_BOTTOM", Clrs)
Dim Path1 As Path
Path1.Initialize(50%x, 100%y)
Path1.LineTo(100%x, 50%y)
Path1.LineTo(50%x, 0%y)
Path1.LineTo(0%x, 50%y)
Path1.LineTo(50%x, 100%y)
Canvas1.ClipPath(Path1) 'clip the drawing area to the path.
DestRect.Left = 0%y
DestRect.Top = 0%y
DestRect.Right = 100%x
DestRect.Bottom = 100%y
Canvas1.DrawDrawable(Gradient1, DestRect) 'fill the drawing area with the gradient.
Activity.Invalidate
DrawBitmap (Bitmap1 As android.graphics.Bitmap, SrcRect As android.graphics.Rect, DestRect As android.graphics.Rect)
Draws a bitmap.
SrcRect - The subset of the bitmap that will be drawn. If Null then the complete bitmap will be drawn.
DestRect - The rectangle that the bitmap will be drawn to.

Example:
Dim Bitmap1 As Bitmap
Bitmap1.Initialize(File.DirAssets, "X.jpg")
Dim DestRect As Rect
DestRect.Initialize(10dip, 10dip, 10dip + 100dip, 10dip + 100dip)
Canvas1.DrawBitmap(Bitmap1, Null, DestRect) 'draws the bitmap to the destination rectangle.

Dim SrcRect As Rect
SrcRect.Initialize(0, 0, Bitmap1.Width / 2, Bitmap1.Height) 'the left half of the bitmap.
DestRect.Top = 200dip
DestRect.Bottom = 200dip + 100dip
Canvas1.DrawBitmap(Bitmap1, SrcRect, DestRect) 'draws half of the bitmap.
Activity.Invalidate
DrawBitmapFlipped (Bitmap1 As android.graphics.Bitmap, SrcRect As android.graphics.Rect, DestRect As android.graphics.Rect, Vertically As Boolean, Horizontally As Boolean)
Flips the bitmap and draws it.
SrcRect - The subset of the bitmap that will be drawn. If Null then the complete bitmap will be drawn.
DestRect - The rectangle that the bitmap will be drawn to.
Vertically - Whether to flip the bitmap vertically.
Horizontally - Whether to flip the bitmap horizontally.
Example:
Canvas1.DrawBitmapFlipped(Bitmap1, Null, DestRect, False, True)
DrawBitmapRotated (Bitmap1 As android.graphics.Bitmap, SrcRect As android.graphics.Rect, DestRect As android.graphics.Rect, Degrees As Float)
Rotates the bitmap and draws it.
SrcRect - The subset of the bitmap that will be drawn. If Null then the complete bitmap will be drawn.
DestRect - The rectangle that the bitmap will be drawn to.
Degrees - Number of degrees to rotate the bitmap (clockwise).
Example:
Canvas1.DrawBitmapRotated(Bitmap1, Null, DestRect, 70)
DrawCircle (x As Float, y As Float, Radius As Float, Color As Int, Filled As Boolean, StrokeWidth As Float)
Draws a circle.
Filled - Whether the circle will be filled.
StrokeWidth - The stroke width. Relevant when Filled = False.

Example:
Canvas1.DrawCircle(150dip, 150dip, 20dip, Colors.Red, False, 10dip)
DrawColor (Color As Int)
Fills the entire canvas with the given color.
Example:
Canvas1.DrawColor(Colors.ARGB(100, 255, 0, 0)) 'fills with semi-transparent red color.
Activity.Invalidate
DrawDrawable (Drawable1 As android.graphics.drawable.Drawable, DestRect As android.graphics.Rect)
Draws a Drawable into the specified rectangle.

Example:
Dim Gradient1 As GradientDrawable
Dim Clrs(2) As Int
Clrs(0) = Colors.Green
Clrs(1) = Colors.Blue
Gradient1.Initialize("TOP_BOTTOM", Clrs)
Canvas1.DrawDrawable(Gradient1, DestRect)
Activity.Invalidate
DrawDrawableRotate (Drawable1 As android.graphics.drawable.Drawable, DestRect As android.graphics.Rect, Degrees As Float)
Rotates and draws a Drawable into the specified rectangle.
Degrees - Number of degrees to rotate (clockwise).
DrawLine (x1 As Float, y1 As Float, x2 As Float, y2 As Float, Color As Int, StrokeWidth As Float)
Draws a line from (x1, y1) to (x2, y2). StrokeWidth determines the width of the line.
Example:
Canvas1.DrawLine(100dip, 100dip, 200dip, 200dip, Colors.Red, 10dip)
Activity.Invalidate
DrawOval (Rect1 As android.graphics.Rect, Color As Int, Filled As Boolean, StrokeWidth As Float)
Draws an oval shape.
Filled - Whether the rectangle will be filled.
StrokeWidth - The stroke width. Relevant when Filled = False.

Example:
Dim Rect1 As Rect
Rect1.Initialize(100dip, 100dip, 200dip, 150dip)
Canvas1.DrawOval(Rect1, Colors.Gray, False, 5dip)
Activity.Invalidate
DrawOvalRotated (Rect1 As android.graphics.Rect, Color As Int, Filled As Boolean, StrokeWidth As Float, Degrees As Float)
Rotates the oval and draws it.
Filled - Whether the rectangle will be filled.
StrokeWidth - The stroke width. Relevant when Filled = False.
Degrees - Number of degrees to rotate the oval (clockwise).
DrawPath (Path1 As android.graphics.Path, Color As Int, Filled As Boolean, StrokeWidth As Float)
Draws the path.
Filled - Whether the path will be filled.
StrokeWidth - The stroke width. Relevant when Filled = False.
Example:
Dim Path1 As Path
Path1.Initialize(50%x, 100%y)
Path1.LineTo(100%x, 50%y)
Path1.LineTo(50%x, 0%y)
Path1.LineTo(0%x, 50%y)
Path1.LineTo(50%x, 100%y)
Canvas1.DrawPath(Path1, Colors.Magenta, False, 10dip)
DrawPoint (x As Float, y As Float, Color As Int)
Draws a point at the specified position and color.

Example:
Canvas1.DrawPoint(50%x, 50%y, Colors.Yellow) 'draws a point in the middle of the screen.
DrawRect (Rect1 As android.graphics.Rect, Color As Int, Filled As Boolean, StrokeWidth As Float)
Draws a rectangle.
Filled - Whether the rectangle will be filled.
StrokeWidth - The stroke width. Relevant when Filled = False.

Example:
Dim Rect1 As Rect
Rect1.Initialize(100dip, 100dip, 200dip, 150dip)
Canvas1.DrawRect(Rect1, Colors.Gray, False, 5dip)
Activity.Invalidate
DrawRectRotated (Rect1 As android.graphics.Rect, Color As Int, Filled As Boolean, StrokeWidth As Float, Degrees As Float)
Rotates the rectangle and draws it.
Filled - Whether the rectangle will be filled.
StrokeWidth - The stroke width. Relevant when Filled = False.
Degrees - Number of degrees to rotate the rectangle (clockwise).
DrawText (Text As String, x As Float, y As Float, Typeface1 As android.graphics.Typeface, TextSize As Float, Color As Int, Align1 As android.graphics.Paint.Align)
Draws the text.
Text - The text to be drawn.
x, y - The origin point.
Typeface1 - Typeface (font) to use.
TextSize - This value will be automatically scaled so do not scale it yourself.
Color - Text color.
Align - The alignment related to the origin. One of the following values: "LEFT", "CENTER", "RIGHT".
Example:
Canvas1.DrawText("This is a nice sentence.", 200dip, 200dip, Typeface.DEFAULT_BOLD, 30, Colors.Blue, "LEFT")
DrawTextRotated (Text As String, x As Float, y As Float, Typeface1 As android.graphics.Typeface, TextSize As Float, Color As Int, Align1 As android.graphics.Paint.Align, Degree As Float)
Rotates the text and draws it.
Text - The text to be drawn.
x, y - The origin point.
Typeface1 - Typeface (font) to use.
TextSize - This value will be automatically scaled so do not scale it yourself.
Color - Text color.
Align - The alignment related to the origin. One of the following values: "LEFT", "CENTER", "RIGHT".
Degrees - Number of degrees to rotate (clockwise).
Example:
Canvas1.DrawTextRotated("This is a nice sentence.", 200dip, 200dip, _
Typeface.DEFAULT_BOLD, 30, Colors.Blue, "CENTER", -45)
Initialize (Target As android.view.View)
Initializes the canvas for drawing on a view.
The view background will be drawn on the canvas during initialization.
Note that you should not change the view's background after calling this method.

Example:
Dim Canvas1 As Canvas
Canvas1.Initialize(Activity) 'this canvas will draw on the activity background
Initialize2 (Bitmap As android.graphics.Bitmap)
Initializes the canvas for drawing on this bitmap.
The bitmap must be mutable. Bitmaps created from files or input streams are NOT mutable.
MeasureStringHeight (Text As String, Typeface As android.graphics.Typeface, TextSize As Float) As Float
Returns the height of the given text.
Example of drawing a blue text with white rectangle as the background:
Dim Rect1 As Rect
Dim width, height As Float
Dim t As String
t = "Text to write"
width = Canvas1.MeasureStringWidth(t, Typeface.DEFAULT, 14)
height = Canvas1.MeasureStringHeight(t, Typeface.DEFAULT, 14)
Rect1.Initialize(100dip, 100dip, 100dip + width, 100dip + height)
Canvas1.DrawRect(Rect1, Colors.White, True, 0)
Canvas1.DrawText(t, Rect1.Left, Rect1.Bottom, Typeface.DEFAULT, 14, Colors.Blue, "LEFT")
MeasureStringWidth (Text As String, Typeface As android.graphics.Typeface, TextSize As Float) As Float
Returns the width of the given text.
Example of drawing a blue text with white rectangle as the background:
Dim Rect1 As Rect
Dim width, height As Float
Dim t As String
t = "Text to write"
width = Canvas1.MeasureStringWidth(t, Typeface.DEFAULT, 14)
height = Canvas1.MeasureStringHeight(t, Typeface.DEFAULT, 14)
Rect1.Initialize(100dip, 100dip, 100dip + width, 100dip + height)
Canvas1.DrawRect(Rect1, Colors.White, True, 0)
Canvas1.DrawText(t, Rect1.Left, Rect1.Bottom, Typeface.DEFAULT, 14, Colors.Blue, "LEFT")
RemoveClip
Removes previous clipped region.

ColorDrawable

A drawable that has a solid color and can have round corners.
Example:
Dim cd As ColorDrawable
cd.Initialize(Colors.Green, 5dip)
Button1.Background = cd

This is an 'Activity Object', it cannot be declared under Sub Process_Globals.

Events:

None

Members:


  Initialize (Color As Int, CornerRadius As Int)

  Initialize2 (Color As Int, CornerRadius As Int, BorderWidth As Int, BorderColor As Int)

  IsInitialized As Boolean

Members description:

Initialize (Color As Int, CornerRadius As Int)
Initializes the drawable with the given color and corners radius.
Initialize2 (Color As Int, CornerRadius As Int, BorderWidth As Int, BorderColor As Int)
IsInitialized As Boolean

GradientDrawable

A drawable that has a gradient color and can have round corners.
This is an 'Activity Object', it cannot be declared under Sub Process_Globals.

Events:

None

Members:


  CornerRadius As Float [write only]

  Initialize (Orientation As android.graphics.drawable.GradientDrawable.Orientation, Colors() As Int)

  IsInitialized As Boolean

Members description:

CornerRadius As Float [write only]
Sets the radius of the "rectangle" corners.
Set to 0 for square corners.

Example:
Gradient1.CornerRadius = 20dip
Initialize (Orientation As android.graphics.drawable.GradientDrawable.Orientation, Colors() As Int)
Initializes this object.
Orientation - The gradient orientation. Can be one of the following value:
"TOP_BOTTOM"
"TR_BL" (Top-Right to Bottom-Left)
"RIGHT_LEFT"
"BR_TL" (Bottom-Right to Top-Left)
"BOTTOM_TOP"
"BL_TR" (Bottom-Left to Top-Right)
"LEFT_RIGHT"
"TL_BR" (Top-Left to Bottom-Right)

Colors - An array with the gradient colors.

Example:
Dim Gradient1 As GradientDrawable
Dim Clrs(2) As Int
Clrs(0) = Colors.Black
Clrs(1) = Colors.White
Gradient1.Initialize("TOP_BOTTOM", Clrs)
IsInitialized As Boolean

Path

A path is a collection of points that represent a connected path.
The first point is set when it is initialized, and then other points are added with LineTo.

Events:

None

Members:


  Initialize (x As Float, y As Float)

  IsInitialized As Boolean

  LineTo (x As Float, y As Float)

Members description:

Initialize (x As Float, y As Float)
Initializes the path and sets the value of the first point.
IsInitialized As Boolean
LineTo (x As Float, y As Float)
Adds a line from the last point to the specified point.

Rect

Holds four coordinates which represent a rectangle.

Events:

None

Members:


  Bottom As Int

  CenterX As Int [read only]

  CenterY As Int [read only]

  Height As Int

  Initialize (Left As Int, Top As Int, Right As Int, Bottom As Int)

  IsInitialized As Boolean

  Left As Int

  Right As Int

  Top As Int

  Width As Int

Members description:

Bottom As Int
CenterX As Int [read only]
Returns the horizontal center.
CenterY As Int [read only]
Returns the vertical center.
Height As Int
Gets or sets the rectangle height.
Initialize (Left As Int, Top As Int, Right As Int, Bottom As Int)
IsInitialized As Boolean
Left As Int
Right As Int
Top As Int
Width As Int
Gets or sets the rectangle width.

StateListDrawable

A drawable that holds other drawables and chooses the current one based on the view's state.
See the StateListDrawable example.
This is an 'Activity Object', it cannot be declared under Sub Process_Globals.

Events:

None

Members:


  AddCatchAllState (Drawable As android.graphics.drawable.Drawable)

  AddState (State As Int, Drawable As android.graphics.drawable.Drawable)

  AddState2 (State() As Int, Drawable As android.graphics.drawable.Drawable)

  Initialize

  IsInitialized As Boolean

  State_Checked As Int

  State_Disabled As Int

  State_Enabled As Int

  State_Focused As Int

  State_Pressed As Int

  State_Selected As Int

  State_Unchecked As Int

Members description:

AddCatchAllState (Drawable As android.graphics.drawable.Drawable)
Adds the drawable that will be used if no other state matched the current state.
This should always be the last state (states added after this one will never be used).
AddState (State As Int, Drawable As android.graphics.drawable.Drawable)
Adds a state and drawable pair.
Note that the order of states is very important. The first state that matches will be used.
AddState2 (State() As Int, Drawable As android.graphics.drawable.Drawable)
Adds a state and drawable pair. The state is made from a combination of states.
You should not reuse the array specified as it is used internally by StateListDrawable.
Note that the order of states is very important. The first state that matches will be used.
Initialize
Initializes the object.
Example:
Dim Button1 As Button
Button1.Initialize("")
Dim sld As StateListDrawable
sld.Initiailize
sld.AddState (sld.State_Disabled, DisabledDrawable)
sld.AddState (sld.State_Pressed, PressedDrawable)
sld.AddCatchAllState (DefaultDrawable)
Button1.Background = sld
IsInitialized As Boolean
State_Checked As Int
State_Disabled As Int
State_Enabled As Int
State_Focused As Int
State_Pressed As Int
State_Selected As Int
State_Unchecked As Int

Top