public final class Canvas extends AndroidViewComponent implements ComponentContainer
A two-dimensional touch-sensitive rectangular panel on which drawing can be done and sprites can be moved.
Conceptually, a sprite consists of the following layers, from back to front (with items in front being drawn on top):
DrawPoint(int,int)
, #DrawCircle(int,int,float)
,
DrawText(String,int,int)
, and
DrawTextAtAngle(String,int,int,float)
, and
SetBackgroundPixelColor(int,int,int)
SetBackgroundPixelColor(int,int,int)
and
GetBackgroundPixelColor(int,int)
. For historical reasons,
changing the background color or image clears the drawing layer.container
ACCELEROMETER_SENSITIVITY_MODERATE, ACCELEROMETER_SENSITIVITY_STRONG, ACCELEROMETER_SENSITIVITY_WEAK, ALIGNMENT_CENTER, ALIGNMENT_NORMAL, ALIGNMENT_OPPOSITE, ASSET_DIRECTORY, BUTTON_SHAPE_DEFAULT, BUTTON_SHAPE_OVAL, BUTTON_SHAPE_RECT, BUTTON_SHAPE_ROUNDED, COLOR_BLACK, COLOR_BLUE, COLOR_CYAN, COLOR_DEFAULT, COLOR_DKGRAY, COLOR_GRAY, COLOR_GREEN, COLOR_LTGRAY, COLOR_MAGENTA, COLOR_NONE, COLOR_ORANGE, COLOR_PINK, COLOR_RED, COLOR_WHITE, COLOR_YELLOW, DEFAULT_VALUE_COLOR_BLACK, DEFAULT_VALUE_COLOR_BLUE, DEFAULT_VALUE_COLOR_CYAN, DEFAULT_VALUE_COLOR_DEFAULT, DEFAULT_VALUE_COLOR_DKGRAY, DEFAULT_VALUE_COLOR_GRAY, DEFAULT_VALUE_COLOR_GREEN, DEFAULT_VALUE_COLOR_LTGRAY, DEFAULT_VALUE_COLOR_MAGENTA, DEFAULT_VALUE_COLOR_NONE, DEFAULT_VALUE_COLOR_ORANGE, DEFAULT_VALUE_COLOR_PINK, DEFAULT_VALUE_COLOR_RED, DEFAULT_VALUE_COLOR_WHITE, DEFAULT_VALUE_COLOR_YELLOW, DIRECTION_EAST, DIRECTION_MAX, DIRECTION_MIN, DIRECTION_NONE, DIRECTION_NORTH, DIRECTION_NORTHEAST, DIRECTION_NORTHWEST, DIRECTION_SOUTH, DIRECTION_SOUTHEAST, DIRECTION_SOUTHWEST, DIRECTION_WEST, FONT_DEFAULT_SIZE, LAYOUT_ORIENTATION_HORIZONTAL, LAYOUT_ORIENTATION_VERTICAL, LENGTH_FILL_PARENT, LENGTH_PREFERRED, LENGTH_UNKNOWN, SLIDER_MAX_VALUE, SLIDER_MIN_VALUE, SLIDER_THUMB_VALUE, TOAST_LENGTH_LONG, TOAST_LENGTH_SHORT, TYPEFACE_DEFAULT, TYPEFACE_MONOSPACE, TYPEFACE_SANSSERIF, TYPEFACE_SERIF
Constructor and Description |
---|
Canvas(ComponentContainer container) |
Modifier and Type | Method and Description |
---|---|
void |
$add(AndroidViewComponent component)
Adds a component to a container.
|
android.app.Activity |
$context()
Returns the activity context (which can be retrieved from the root
container - aka the form).
|
Form |
$form()
Returns the form that ultimately contains this container.
|
int |
BackgroundColor()
Returns the button's background color as an alpha-red-green-blue
integer, i.e.,
0xAARRGGBB . |
void |
BackgroundColor(int argb)
Specifies the Canvas's background color as an alpha-red-green-blue
integer, i.e.,
0xAARRGGBB . |
java.lang.String |
BackgroundImage()
Returns the path of the canvas background image.
|
void |
BackgroundImage(java.lang.String path)
Specifies the path of the canvas background image.
|
void |
Clear()
Clears the canvas, without removing the background image, if one
was provided.
|
void |
Dragged(float startX,
float startY,
float prevX,
float prevY,
float currentX,
float currentY,
boolean draggedAnySprite)
When the user does a drag from one point (prevX, prevY) to
another (x, y).
|
void |
DrawCircle(int centerX,
int centerY,
float radius,
boolean fill)
Draws a circle (filled in) with the given radius centered at the given coordinates on the canvas
|
void |
DrawLine(int x1,
int y1,
int x2,
int y2)
Draws a line between the given coordinates on the canvas.
|
void |
DrawPoint(int x,
int y)
Draws a point at the given coordinates on the canvas.
|
void |
DrawText(java.lang.String text,
int x,
int y)
Draws the specified text relative to the specified coordinates
using the values of the
FontSize(float) and
TextAlignment(int) properties. |
void |
DrawTextAtAngle(java.lang.String text,
int x,
int y,
float angle)
Draws the specified text starting at the specified coordinates
at the specified angle using the values of the
FontSize(float) and
TextAlignment(int) properties. |
protected void |
findSpriteCollisions(Sprite movedSprite)
Checks if the given sprite now overlaps with or abuts any other sprite
or has ceased to do so.
|
void |
Flung(float x,
float y,
float speed,
float heading,
float xvel,
float yvel,
boolean flungSprite)
When a fling gesture (quick swipe) is made on the canvas: provides
the (x,y) position of the start of the fling, relative to the upper
left of the canvas.
|
float |
FontSize() |
void |
FontSize(float size) |
int |
GetBackgroundPixelColor(int x,
int y)
Gets the color of the given pixel, ignoring sprites.
|
int |
GetPixelColor(int x,
int y)
Gets the color of the given pixel, including sprites.
|
android.view.View |
getView()
Returns the
View that is displayed in the UI. |
void |
Height(int height)
Set the canvas height
The height can only be set to >0 or -1 (automatic) or -2 (fill parent)
|
float |
LineWidth()
Returns the currently specified stroke width
|
void |
LineWidth(float width)
Specifies the stroke width
|
int |
PaintColor()
Returns the currently specified paint color as an alpha-red-green-blue
integer, i.e.,
0xAARRGGBB . |
void |
PaintColor(int argb)
Specifies the paint color as an alpha-red-green-blue integer,
i.e.,
0xAARRGGBB . |
boolean |
ready()
Returns whether the layout associated with this view has been computed.
|
java.lang.String |
Save()
Saves a picture of this Canvas to the device's external storage.
|
java.lang.String |
SaveAs(java.lang.String fileName)
Saves a picture of this Canvas to the device's external storage in the file
named fileName.
|
void |
SetBackgroundPixelColor(int x,
int y,
int color)
Sets the color of the given pixel.
|
void |
setChildHeight(AndroidViewComponent component,
int height) |
void |
setChildWidth(AndroidViewComponent component,
int width) |
int |
TextAlignment()
Returns the alignment of the canvas's text: center, normal
(starting at the specified point in drawText()), or opposite
(ending at the specified point in drawText()).
|
void |
TextAlignment(int alignment)
Specifies the alignment of the canvas's text: center, normal
(starting at the specified point in DrawText() or DrawAngle()),
or opposite (ending at the specified point in DrawText() or
DrawAngle()).
|
void |
TouchDown(float x,
float y)
When the user begins touching the canvas (places finger on canvas and
leaves it there): provides the (x,y) position of the touch, relative
to the upper left of the canvas
|
void |
Touched(float x,
float y,
boolean touchedAnySprite)
When the user touches the canvas and then immediately lifts finger: provides
the (x,y) position of the touch, relative to the upper left of the canvas.
|
void |
TouchUp(float x,
float y)
When the user stops touching the canvas (lifts finger after a
TouchDown event): provides the (x,y) position of the touch, relative
to the upper left of the canvas
|
void |
Width(int width)
Set the canvas width
The width can only be set to >0 or -1 (automatic) or -2 (fill parent).
|
Column, Column, CopyHeight, CopyWidth, getDispatchDelegate, Height, Row, Row, Visible, Visible, Width
public Canvas(ComponentContainer container)
public android.view.View getView()
AndroidViewComponent
View
that is displayed in the UI.getView
in class AndroidViewComponent
public boolean ready()
AndroidViewComponent.Width()
and AndroidViewComponent.Height()
will be properly initialized.true
if it is safe to call AndroidViewComponent.Width()
and AndroidViewComponent.Height()
, false
otherwisepublic android.app.Activity $context()
ComponentContainer
$context
in interface ComponentContainer
public Form $form()
ComponentContainer
$form
in interface ComponentContainer
public void $add(AndroidViewComponent component)
ComponentContainer
$add
in interface ComponentContainer
component
- component associated with viewpublic void setChildWidth(AndroidViewComponent component, int width)
setChildWidth
in interface ComponentContainer
public void setChildHeight(AndroidViewComponent component, int height)
setChildHeight
in interface ComponentContainer
protected void findSpriteCollisions(Sprite movedSprite)
Sprite.CollidedWith(Sprite)
is called for each sprite
with the other sprite as an argument. If two sprites that had been in
collision are no longer colliding,
Sprite.NoLongerCollidingWith(Sprite)
is called for each sprite
with the other as an argument. Collisions are only recognized between
sprites that are both
Sprite.Visible()
and
Sprite.Enabled()
.movedSprite
- the sprite that has just changed positionpublic void Width(int width)
Width
in class AndroidViewComponent
width
- public void Height(int height)
Height
in class AndroidViewComponent
height
- public int BackgroundColor()
0xAARRGGBB
. An alpha of 00
indicates fully transparent and FF
means opaque.public void BackgroundColor(int argb)
0xAARRGGBB
. An alpha of 00
indicates fully transparent and FF
means opaque.
The background color only shows if there is no background image.argb
- background color in the format 0xAARRGGBB, which
includes alpha, red, green, and blue componentspublic java.lang.String BackgroundImage()
public void BackgroundImage(java.lang.String path)
MediaUtil.determineMediaSource(com.google.appinventor.components.runtime.Form, java.lang.String)
for information about what
a path can be.path
- the path of the canvas background imagepublic int PaintColor()
0xAARRGGBB
. An alpha of 00
indicates fully transparent and FF
means opaque.public void PaintColor(int argb)
0xAARRGGBB
. An alpha of 00
indicates fully
transparent and FF
means opaque.argb
- paint color in the format 0xAARRGGBB, which includes
alpha, red, green, and blue componentspublic float FontSize()
public void FontSize(float size)
public float LineWidth()
public void LineWidth(float width)
width
- public int TextAlignment()
Component.ALIGNMENT_NORMAL
,
Component.ALIGNMENT_CENTER
or
Component.ALIGNMENT_OPPOSITE
public void TextAlignment(int alignment)
alignment
- one of Component.ALIGNMENT_NORMAL
,
Component.ALIGNMENT_CENTER
or
Component.ALIGNMENT_OPPOSITE
public void Touched(float x, float y, boolean touchedAnySprite)
x
- x-coordinate of the point that was touchedy
- y-coordinate of the point that was touchedtouchedAnySprite
- true
if a sprite was touched, false
otherwisepublic void TouchDown(float x, float y)
x
- x-coordinate of the point that was touchedy
- y-coordinate of the point that was touchedpublic void TouchUp(float x, float y)
x
- x-coordinate of the point that was touchedy
- y-coordinate of the point that was touchedpublic void Flung(float x, float y, float speed, float heading, float xvel, float yvel, boolean flungSprite)
x
- x-coordinate of touched pointy
- y-coordinate of touched pointspeed
- the speed of the fling sqrt(xspeed^2 + yspeed^2)heading
- the heading of the flingxvel
- the speed in x-direction of the flingyvel
- the speed in y-direction of the flingflungSprite
- true
if a sprite was flung,
false
otherwisepublic void Dragged(float startX, float startY, float prevX, float prevY, float currentX, float currentY, boolean draggedAnySprite)
startX
- the starting x-coordinatestartY
- the starting y-coordinateprevX
- the previous x-coordinate (possibly equal to startX)prevY
- the previous y-coordinate (possibly equal to startY)currentX
- the current x-coordinatecurrentY
- the current y-coordinatedraggedAnySprite
- true
if
Sprite.Dragged(float, float, float, float, float, float)
was called for one or more sprites for this segment, false
otherwisepublic void Clear()
public void DrawPoint(int x, int y)
x
- x coordinatey
- y coordinatepublic void DrawCircle(int centerX, int centerY, float radius, boolean fill)
centerX
- x-coordinate of the center of the circlecenterY
- y-coordinate of the center of the circleradius
- radius of the circlefill
- true for filled circle; false for circle outlinepublic void DrawLine(int x1, int y1, int x2, int y2)
x1
- x coordinate of first pointy1
- y coordinate of first pointx2
- x coordinate of second pointy2
- y coordinate of second pointpublic void DrawText(java.lang.String text, int x, int y)
FontSize(float)
and
TextAlignment(int)
properties.text
- the text to drawx
- the x-coordinate of the originy
- the y-coordinate of the originpublic void DrawTextAtAngle(java.lang.String text, int x, int y, float angle)
FontSize(float)
and
TextAlignment(int)
properties.text
- the text to drawx
- the x-coordinate of the originy
- the y-coordinate of the originangle
- the angle (in degrees) at which to draw the textpublic int GetBackgroundPixelColor(int x, int y)
Gets the color of the given pixel, ignoring sprites.
x
- the x-coordinatey
- the y-coordinateComponent.COLOR_NONE
if that point is not on this Canvaspublic void SetBackgroundPixelColor(int x, int y, int color)
Sets the color of the given pixel. This has no effect if the coordinates are out of bounds.
x
- the x-coordinatey
- the y-coordinatecolor
- the color as an alpha-red-blue-green integerpublic int GetPixelColor(int x, int y)
Gets the color of the given pixel, including sprites.
x
- the x-coordinatey
- the y-coordinateComponent.COLOR_NONE
if that point is not on this Canvaspublic java.lang.String Save()
public java.lang.String SaveAs(java.lang.String fileName)