public class Canvas extends Object
For more information about how to use Canvas, read the Canvas and Drawables developer guide.
Modifier and Type | Class and Description |
---|---|
static class |
Canvas.EdgeType |
static interface |
Canvas.Saveflags |
static class |
Canvas.VertexMode |
Modifier and Type | Field and Description |
---|---|
static int |
ALL_SAVE_FLAG
Restore everything when restore() is called (standard save flags).
|
static int |
CLIP_SAVE_FLAG
Restore the current clip when restore() is called.
|
static int |
CLIP_TO_LAYER_SAVE_FLAG
Clip drawing to the bounds of the offscreen layer, omit at your own peril.
|
static int |
FULL_COLOR_LAYER_SAVE_FLAG
The layer requires full 8-bit precision for each color channel.
|
static int |
HAS_ALPHA_LAYER_SAVE_FLAG
The layer requires a per-pixel alpha channel.
|
static int |
MATRIX_SAVE_FLAG
Restore the current matrix when restore() is called.
|
protected int |
mDensity |
protected long |
mNativeCanvasWrapper
Should only be assigned in constructors (or setBitmap if software canvas),
freed by NativeAllocation.
|
protected int |
mScreenDensity
Used to determine when compatibility scaling is in effect.
|
static boolean |
sCompatibilityRestore |
Constructor and Description |
---|
Canvas()
Construct an empty raster canvas.
|
Canvas(Bitmap bitmap)
Construct a canvas with the specified bitmap to draw into.
|
Canvas(long nativeCanvas) |
Modifier and Type | Method and Description |
---|---|
protected static void |
checkRange(int length,
int offset,
int count) |
boolean |
clipPath(Path path)
Intersect the current clip with the specified path.
|
boolean |
clipPath(Path path,
Region.Op op)
Modify the current clip with the specified path.
|
boolean |
clipRect(float left,
float top,
float right,
float bottom)
Intersect the current clip with the specified rectangle, which is
expressed in local coordinates.
|
boolean |
clipRect(float left,
float top,
float right,
float bottom,
Region.Op op)
Modify the current clip with the specified rectangle, which is
expressed in local coordinates.
|
boolean |
clipRect(int left,
int top,
int right,
int bottom)
Intersect the current clip with the specified rectangle, which is
expressed in local coordinates.
|
boolean |
clipRect(Rect rect)
Intersect the current clip with the specified rectangle, which is
expressed in local coordinates.
|
boolean |
clipRect(RectF rect)
Intersect the current clip with the specified rectangle, which is
expressed in local coordinates.
|
boolean |
clipRect(RectF rect,
Region.Op op)
Modify the current clip with the specified rectangle.
|
boolean |
clipRect(Rect rect,
Region.Op op)
Modify the current clip with the specified rectangle, which is
expressed in local coordinates.
|
boolean |
clipRegion(Region region)
Deprecated.
Unlike all other clip calls this API does not respect the
current matrix. Use
clipRect(Rect) as an alternative. |
boolean |
clipRegion(Region region,
Region.Op op)
Deprecated.
Unlike all other clip calls this API does not respect the
current matrix. Use
clipRect(Rect) as an alternative. |
void |
concat(Matrix matrix)
Preconcat the current matrix with the specified matrix.
|
void |
drawArc(float left,
float top,
float right,
float bottom,
float startAngle,
float sweepAngle,
boolean useCenter,
Paint paint)
Draw the specified arc, which will be scaled to fit inside the
specified oval.
|
void |
drawArc(RectF oval,
float startAngle,
float sweepAngle,
boolean useCenter,
Paint paint)
Draw the specified arc, which will be scaled to fit inside the
specified oval.
|
void |
drawARGB(int a,
int r,
int g,
int b)
Fill the entire canvas' bitmap (restricted to the current clip) with the
specified ARGB color, using srcover porterduff mode.
|
void |
drawBitmap(Bitmap bitmap,
float left,
float top,
Paint paint)
Draw the specified bitmap, with its top/left corner at (x,y), using
the specified paint, transformed by the current matrix.
|
void |
drawBitmap(Bitmap bitmap,
Matrix matrix,
Paint paint)
Draw the bitmap using the specified matrix.
|
void |
drawBitmap(Bitmap bitmap,
Rect src,
RectF dst,
Paint paint)
Draw the specified bitmap, scaling/translating automatically to fill
the destination rectangle.
|
void |
drawBitmap(Bitmap bitmap,
Rect src,
Rect dst,
Paint paint)
Draw the specified bitmap, scaling/translating automatically to fill
the destination rectangle.
|
void |
drawBitmap(int[] colors,
int offset,
int stride,
float x,
float y,
int width,
int height,
boolean hasAlpha,
Paint paint)
Deprecated.
Usage with a
hardware accelerated canvas
requires an internal copy of color buffer contents every time this method is called. Using a
Bitmap avoids this copy, and allows the application to more explicitly control the lifetime
and copies of pixel data. |
void |
drawBitmap(int[] colors,
int offset,
int stride,
int x,
int y,
int width,
int height,
boolean hasAlpha,
Paint paint)
Deprecated.
Usage with a
hardware accelerated canvas
requires an internal copy of color buffer contents every time this method is called. Using a
Bitmap avoids this copy, and allows the application to more explicitly control the lifetime
and copies of pixel data. |
void |
drawBitmapMesh(Bitmap bitmap,
int meshWidth,
int meshHeight,
float[] verts,
int vertOffset,
int[] colors,
int colorOffset,
Paint paint)
Draw the bitmap through the mesh, where mesh vertices are evenly
distributed across the bitmap.
|
void |
drawCircle(float cx,
float cy,
float radius,
Paint paint)
Draw the specified circle using the specified paint.
|
void |
drawColor(int color)
Fill the entire canvas' bitmap (restricted to the current clip) with the
specified color, using srcover porterduff mode.
|
void |
drawColor(int color,
PorterDuff.Mode mode)
Fill the entire canvas' bitmap (restricted to the current clip) with the
specified color and porter-duff xfermode.
|
void |
drawLine(float startX,
float startY,
float stopX,
float stopY,
Paint paint)
Draw a line segment with the specified start and stop x,y coordinates,
using the specified paint.
|
void |
drawLines(float[] pts,
int offset,
int count,
Paint paint)
Draw a series of lines.
|
void |
drawLines(float[] pts,
Paint paint) |
void |
drawOval(float left,
float top,
float right,
float bottom,
Paint paint)
Draw the specified oval using the specified paint.
|
void |
drawOval(RectF oval,
Paint paint)
Draw the specified oval using the specified paint.
|
void |
drawPaint(Paint paint)
Fill the entire canvas' bitmap (restricted to the current clip) with
the specified paint.
|
void |
drawPatch(NinePatch patch,
RectF dst,
Paint paint)
Draws the specified bitmap as an N-patch (most often, a 9-patches.)
|
void |
drawPatch(NinePatch patch,
Rect dst,
Paint paint)
Draws the specified bitmap as an N-patch (most often, a 9-patches.)
|
void |
drawPath(Path path,
Paint paint)
Draw the specified path using the specified paint.
|
void |
drawPicture(Picture picture)
Save the canvas state, draw the picture, and restore the canvas state.
|
void |
drawPicture(Picture picture,
Rect dst)
Draw the picture, stretched to fit into the dst rectangle.
|
void |
drawPicture(Picture picture,
RectF dst)
Draw the picture, stretched to fit into the dst rectangle.
|
void |
drawPoint(float x,
float y,
Paint paint)
Helper for drawPoints() for drawing a single point.
|
void |
drawPoints(float[] pts,
int offset,
int count,
Paint paint)
Draw a series of points.
|
void |
drawPoints(float[] pts,
Paint paint)
Helper for drawPoints() that assumes you want to draw the entire array
|
void |
drawPosText(char[] text,
int index,
int count,
float[] pos,
Paint paint)
Deprecated.
This method does not support glyph composition and decomposition and
should therefore not be used to render complex scripts. It also doesn't
handle supplementary characters (eg emoji).
|
void |
drawPosText(String text,
float[] pos,
Paint paint)
Deprecated.
This method does not support glyph composition and decomposition and
should therefore not be used to render complex scripts. It also doesn't
handle supplementary characters (eg emoji).
|
void |
drawRect(float left,
float top,
float right,
float bottom,
Paint paint)
Draw the specified Rect using the specified paint.
|
void |
drawRect(RectF rect,
Paint paint)
Draw the specified Rect using the specified paint.
|
void |
drawRect(Rect r,
Paint paint)
Draw the specified Rect using the specified Paint.
|
void |
drawRGB(int r,
int g,
int b)
Fill the entire canvas' bitmap (restricted to the current clip) with the
specified RGB color, using srcover porterduff mode.
|
void |
drawRoundRect(float left,
float top,
float right,
float bottom,
float rx,
float ry,
Paint paint)
Draw the specified round-rect using the specified paint.
|
void |
drawRoundRect(RectF rect,
float rx,
float ry,
Paint paint)
Draw the specified round-rect using the specified paint.
|
void |
drawText(char[] text,
int index,
int count,
float x,
float y,
Paint paint)
Draw the text, with origin at (x,y), using the specified paint.
|
void |
drawText(CharSequence text,
int start,
int end,
float x,
float y,
Paint paint)
Draw the specified range of text, specified by start/end, with its
origin at (x,y), in the specified Paint.
|
void |
drawText(String text,
float x,
float y,
Paint paint)
Draw the text, with origin at (x,y), using the specified paint.
|
void |
drawText(String text,
int start,
int end,
float x,
float y,
Paint paint)
Draw the text, with origin at (x,y), using the specified paint.
|
void |
drawTextOnPath(char[] text,
int index,
int count,
Path path,
float hOffset,
float vOffset,
Paint paint)
Draw the text, with origin at (x,y), using the specified paint, along
the specified path.
|
void |
drawTextOnPath(String text,
Path path,
float hOffset,
float vOffset,
Paint paint)
Draw the text, with origin at (x,y), using the specified paint, along
the specified path.
|
void |
drawTextRun(char[] text,
int index,
int count,
int contextIndex,
int contextCount,
float x,
float y,
boolean isRtl,
Paint paint)
Draw a run of text, all in a single direction, with optional context for complex text
shaping.
|
void |
drawTextRun(CharSequence text,
int start,
int end,
int contextStart,
int contextEnd,
float x,
float y,
boolean isRtl,
Paint paint)
Draw a run of text, all in a single direction, with optional context for complex text
shaping.
|
void |
drawVertices(Canvas.VertexMode mode,
int vertexCount,
float[] verts,
int vertOffset,
float[] texs,
int texOffset,
int[] colors,
int colorOffset,
short[] indices,
int indexOffset,
int indexCount,
Paint paint)
Draw the array of vertices, interpreted as triangles (based on mode).
|
static void |
freeCaches()
Free up as much memory as possible from private caches (e.g. fonts, images)
|
static void |
freeTextLayoutCaches()
Free up text layout caches
|
Rect |
getClipBounds()
Retrieve the bounds of the current clip (in local coordinates).
|
boolean |
getClipBounds(Rect bounds)
Return the bounds of the current clip (in local coordinates) in the
bounds parameter, and return true if it is non-empty.
|
int |
getDensity()
Returns the target density of the canvas.
|
DrawFilter |
getDrawFilter() |
protected GL |
getGL()
Deprecated.
This method is not supported and should not be invoked.
|
int |
getHeight()
Returns the height of the current drawing layer
|
Matrix |
getMatrix()
Deprecated.
Hardware accelerated canvases may have any
matrix when passed to a View or Drawable, as it is implementation defined where in the
hierarchy such canvases are created. It is recommended in such cases to either draw contents
irrespective of the current matrix, or to track relevant transform state outside of the
canvas. |
void |
getMatrix(Matrix ctm)
Deprecated.
Hardware accelerated canvases may have any
matrix when passed to a View or Drawable, as it is implementation defined where in the
hierarchy such canvases are created. It is recommended in such cases to either draw contents
irrespective of the current matrix, or to track relevant transform state outside of the
canvas. |
int |
getMaximumBitmapHeight()
Returns the maximum allowed height for bitmaps drawn with this canvas.
|
int |
getMaximumBitmapWidth()
Returns the maximum allowed width for bitmaps drawn with this canvas.
|
long |
getNativeCanvasWrapper() |
int |
getSaveCount()
Returns the number of matrix/clip states on the Canvas' private stack.
|
int |
getWidth()
Returns the width of the current drawing layer
|
void |
insertInorderBarrier() |
void |
insertReorderBarrier() |
boolean |
isHardwareAccelerated()
Indicates whether this Canvas uses hardware acceleration.
|
boolean |
isOpaque()
Return true if the device that the current layer draws into is opaque
(i.e. does not support per-pixel alpha).
|
boolean |
isRecordingFor(Object o) |
boolean |
quickReject(float left,
float top,
float right,
float bottom,
Canvas.EdgeType type)
Return true if the specified rectangle, after being transformed by the
current matrix, would lie completely outside of the current clip.
|
boolean |
quickReject(Path path,
Canvas.EdgeType type)
Return true if the specified path, after being transformed by the
current matrix, would lie completely outside of the current clip.
|
boolean |
quickReject(RectF rect,
Canvas.EdgeType type)
Return true if the specified rectangle, after being transformed by the
current matrix, would lie completely outside of the current clip.
|
void |
release()
Releases the resources associated with this canvas.
|
void |
restore()
This call balances a previous call to save(), and is used to remove all
modifications to the matrix/clip state since the last save call.
|
void |
restoreToCount(int saveCount)
Efficient way to pop any calls to save() that happened after the save
count reached saveCount.
|
void |
rotate(float degrees)
Preconcat the current matrix with the specified rotation.
|
void |
rotate(float degrees,
float px,
float py)
Preconcat the current matrix with the specified rotation.
|
int |
save()
Saves the current matrix and clip onto a private stack.
|
int |
save(int saveFlags)
Based on saveFlags, can save the current matrix and clip onto a private
stack.
|
int |
saveLayer(float left,
float top,
float right,
float bottom,
Paint paint)
Convenience for saveLayer(left, top, right, bottom, paint,
ALL_SAVE_FLAG ) |
int |
saveLayer(float left,
float top,
float right,
float bottom,
Paint paint,
int saveFlags)
Helper version of saveLayer() that takes 4 values rather than a RectF.
|
int |
saveLayer(RectF bounds,
Paint paint)
Convenience for saveLayer(bounds, paint,
ALL_SAVE_FLAG ) |
int |
saveLayer(RectF bounds,
Paint paint,
int saveFlags)
This behaves the same as save(), but in addition it allocates and
redirects drawing to an offscreen bitmap.
|
int |
saveLayerAlpha(float left,
float top,
float right,
float bottom,
int alpha)
Helper for saveLayerAlpha(left, top, right, bottom, alpha,
ALL_SAVE_FLAG ) |
int |
saveLayerAlpha(float left,
float top,
float right,
float bottom,
int alpha,
int saveFlags)
Helper for saveLayerAlpha() that takes 4 values instead of a RectF.
|
int |
saveLayerAlpha(RectF bounds,
int alpha)
Convenience for saveLayerAlpha(bounds, alpha,
ALL_SAVE_FLAG ) |
int |
saveLayerAlpha(RectF bounds,
int alpha,
int saveFlags)
This behaves the same as save(), but in addition it allocates and
redirects drawing to an offscreen bitmap.
|
void |
scale(float sx,
float sy)
Preconcat the current matrix with the specified scale.
|
void |
scale(float sx,
float sy,
float px,
float py)
Preconcat the current matrix with the specified scale.
|
void |
setBitmap(Bitmap bitmap)
Specify a bitmap for the canvas to draw into.
|
void |
setDensity(int density)
Specifies the density for this Canvas' backing bitmap.
|
void |
setDrawFilter(DrawFilter filter) |
void |
setHighContrastText(boolean highContrastText) |
void |
setMatrix(Matrix matrix)
Completely replace the current matrix with the specified matrix.
|
void |
setScreenDensity(int density) |
void |
skew(float sx,
float sy)
Preconcat the current matrix with the specified skew.
|
protected void |
throwIfCannotDraw(Bitmap bitmap) |
void |
translate(float dx,
float dy)
Preconcat the current matrix with the specified translation
|
public static boolean sCompatibilityRestore
protected long mNativeCanvasWrapper
protected int mDensity
protected int mScreenDensity
public static final int MATRIX_SAVE_FLAG
public static final int CLIP_SAVE_FLAG
public static final int HAS_ALPHA_LAYER_SAVE_FLAG
public static final int FULL_COLOR_LAYER_SAVE_FLAG
public static final int CLIP_TO_LAYER_SAVE_FLAG
Note: it is strongly recommended to not
omit this flag for any call to saveLayer()
and
saveLayerAlpha()
variants. Not passing this flag generally
triggers extremely poor performance with hardware accelerated rendering.
public static final int ALL_SAVE_FLAG
Note: for performance reasons, it is
strongly recommended to pass this - the complete set of flags - to any
call to saveLayer()
and saveLayerAlpha()
variants.
public Canvas()
Bitmap.DENSITY_NONE
;
this will typically be replaced when a target bitmap is set for the
canvas.public Canvas(Bitmap bitmap)
The initial target density of the canvas is the same as the given bitmap's density.
bitmap
- Specifies a mutable bitmap for the canvas to draw into.public Canvas(long nativeCanvas)
public long getNativeCanvasWrapper()
public boolean isRecordingFor(Object o)
@Deprecated protected GL getGL()
public boolean isHardwareAccelerated()
public void setBitmap(Bitmap bitmap)
bitmap
- Specifies a mutable bitmap for the canvas to draw into.setDensity(int)
,
getDensity()
public void setHighContrastText(boolean highContrastText)
public void insertReorderBarrier()
public void insertInorderBarrier()
public boolean isOpaque()
public int getWidth()
public int getHeight()
public int getDensity()
Returns the target density of the canvas. The default density is
derived from the density of its backing bitmap, or
Bitmap.DENSITY_NONE
if there is not one.
setDensity(int)
,
Bitmap.getDensity()
public void setDensity(int density)
Specifies the density for this Canvas' backing bitmap. This modifies
the target density of the canvas itself, as well as the density of its
backing bitmap via Bitmap.setDensity(int)
.
density
- The new target density of the canvas, which is used
to determine the scaling factor when drawing a bitmap into it. Use
Bitmap.DENSITY_NONE
to disable bitmap scaling.getDensity()
,
Bitmap.setDensity(int)
public void setScreenDensity(int density)
public int getMaximumBitmapWidth()
getMaximumBitmapHeight()
public int getMaximumBitmapHeight()
getMaximumBitmapWidth()
public int save()
Subsequent calls to translate,scale,rotate,skew,concat or clipRect, clipPath will all operate as usual, but when the balancing call to restore() is made, those calls will be forgotten, and the settings that existed before the save() will be reinstated.
public int save(int saveFlags)
Note: if possible, use the parameter-less save(). It is simpler and faster than individually disabling the saving of matrix or clip with this method.
Subsequent calls to translate,scale,rotate,skew,concat or clipRect, clipPath will all operate as usual, but when the balancing call to restore() is made, those calls will be forgotten, and the settings that existed before the save() will be reinstated.
saveFlags
- flag bits that specify which parts of the Canvas state
to save/restorepublic int saveLayer(RectF bounds, Paint paint, int saveFlags)
Note: this method is very expensive,
incurring more than double rendering cost for contained content. Avoid
using this method, especially if the bounds provided are large, or if
the CLIP_TO_LAYER_SAVE_FLAG
is omitted from the
saveFlags
parameter. It is recommended to use a
hardware layer
on a View
to apply an xfermode, color filter, or alpha, as it will perform much
better than this method.
All drawing calls are directed to a newly allocated offscreen bitmap. Only when the balancing call to restore() is made, is that offscreen buffer drawn back to the current target of the Canvas (either the screen, it's target Bitmap, or the previous layer).
Attributes of the Paint - alpha
,
Xfermode
, and
ColorFilter
are applied when the
offscreen bitmap is drawn back when restore() is called.
bounds
- May be null. The maximum size the offscreen bitmap
needs to be (in local coordinates)paint
- This is copied, and is applied to the offscreen when
restore() is called.saveFlags
- see _SAVE_FLAG constants, generally ALL_SAVE_FLAG
is recommended
for performance reasons.public int saveLayer(RectF bounds, Paint paint)
ALL_SAVE_FLAG
)public int saveLayer(float left, float top, float right, float bottom, Paint paint, int saveFlags)
public int saveLayer(float left, float top, float right, float bottom, Paint paint)
ALL_SAVE_FLAG
)public int saveLayerAlpha(RectF bounds, int alpha, int saveFlags)
Note: this method is very expensive,
incurring more than double rendering cost for contained content. Avoid
using this method, especially if the bounds provided are large, or if
the CLIP_TO_LAYER_SAVE_FLAG
is omitted from the
saveFlags
parameter. It is recommended to use a
hardware layer
on a View
to apply an xfermode, color filter, or alpha, as it will perform much
better than this method.
All drawing calls are directed to a newly allocated offscreen bitmap. Only when the balancing call to restore() is made, is that offscreen buffer drawn back to the current target of the Canvas (either the screen, it's target Bitmap, or the previous layer).
The alpha
parameter is applied when the offscreen bitmap is
drawn back when restore() is called.
bounds
- The maximum size the offscreen bitmap needs to be
(in local coordinates)alpha
- The alpha to apply to the offscreen when it is
drawn during restore()saveFlags
- see _SAVE_FLAG constants, generally ALL_SAVE_FLAG
is recommended
for performance reasons.public int saveLayerAlpha(RectF bounds, int alpha)
ALL_SAVE_FLAG
)public int saveLayerAlpha(float left, float top, float right, float bottom, int alpha, int saveFlags)
public int saveLayerAlpha(float left, float top, float right, float bottom, int alpha)
ALL_SAVE_FLAG
)public void restore()
public int getSaveCount()
public void restoreToCount(int saveCount)
saveCount
- The save level to restore to.public void translate(float dx, float dy)
dx
- The distance to translate in Xdy
- The distance to translate in Ypublic void scale(float sx, float sy)
sx
- The amount to scale in Xsy
- The amount to scale in Ypublic final void scale(float sx, float sy, float px, float py)
sx
- The amount to scale in Xsy
- The amount to scale in Ypx
- The x-coord for the pivot point (unchanged by the scale)py
- The y-coord for the pivot point (unchanged by the scale)public void rotate(float degrees)
degrees
- The amount to rotate, in degreespublic final void rotate(float degrees, float px, float py)
degrees
- The amount to rotate, in degreespx
- The x-coord for the pivot point (unchanged by the rotation)py
- The y-coord for the pivot point (unchanged by the rotation)public void skew(float sx, float sy)
sx
- The amount to skew in Xsy
- The amount to skew in Ypublic void concat(Matrix matrix)
matrix
- The matrix to preconcatenate with the current matrixpublic void setMatrix(Matrix matrix)
concat(Matrix)
,
scale(float, float)
, translate(float, float)
and
rotate(float)
instead of this method.matrix
- The matrix to replace the current matrix with. If it is
null, set the current matrix to identity.concat(Matrix)
@Deprecated public void getMatrix(Matrix ctm)
Hardware accelerated
canvases may have any
matrix when passed to a View or Drawable, as it is implementation defined where in the
hierarchy such canvases are created. It is recommended in such cases to either draw contents
irrespective of the current matrix, or to track relevant transform state outside of the
canvas.@Deprecated public final Matrix getMatrix()
Hardware accelerated
canvases may have any
matrix when passed to a View or Drawable, as it is implementation defined where in the
hierarchy such canvases are created. It is recommended in such cases to either draw contents
irrespective of the current matrix, or to track relevant transform state outside of the
canvas.public boolean clipRect(RectF rect, Region.Op op)
rect
- The rect to intersect with the current clipop
- How the clip is modifiedpublic boolean clipRect(Rect rect, Region.Op op)
rect
- The rectangle to intersect with the current clip.op
- How the clip is modifiedpublic boolean clipRect(RectF rect)
rect
- The rectangle to intersect with the current clip.public boolean clipRect(Rect rect)
rect
- The rectangle to intersect with the current clip.public boolean clipRect(float left, float top, float right, float bottom, Region.Op op)
left
- The left side of the rectangle to intersect with the
current cliptop
- The top of the rectangle to intersect with the current
clipright
- The right side of the rectangle to intersect with the
current clipbottom
- The bottom of the rectangle to intersect with the current
clipop
- How the clip is modifiedpublic boolean clipRect(float left, float top, float right, float bottom)
left
- The left side of the rectangle to intersect with the
current cliptop
- The top of the rectangle to intersect with the current clipright
- The right side of the rectangle to intersect with the
current clipbottom
- The bottom of the rectangle to intersect with the current
clippublic boolean clipRect(int left, int top, int right, int bottom)
left
- The left side of the rectangle to intersect with the
current cliptop
- The top of the rectangle to intersect with the current clipright
- The right side of the rectangle to intersect with the
current clipbottom
- The bottom of the rectangle to intersect with the current
clippublic boolean clipPath(Path path, Region.Op op)
path
- The path to operate on the current clipop
- How the clip is modifiedpublic boolean clipPath(Path path)
path
- The path to intersect with the current clip@Deprecated public boolean clipRegion(Region region, Region.Op op)
clipRect(Rect)
as an alternative.region
- The region to operate on the current clip, based on opop
- How the clip is modified@Deprecated public boolean clipRegion(Region region)
clipRect(Rect)
as an alternative.region
- The region to operate on the current clip, based on oppublic DrawFilter getDrawFilter()
public void setDrawFilter(DrawFilter filter)
public boolean quickReject(RectF rect, Canvas.EdgeType type)
rect
- the rect to compare with the current cliptype
- Canvas.EdgeType.AA
if the path should be considered antialiased,
since that means it may affect a larger area (more pixels) than
non-antialiased (Canvas.EdgeType.BW
).public boolean quickReject(Path path, Canvas.EdgeType type)
path
- The path to compare with the current cliptype
- Canvas.EdgeType.AA
if the path should be considered antialiased,
since that means it may affect a larger area (more pixels) than
non-antialiased (Canvas.EdgeType.BW
).public boolean quickReject(float left, float top, float right, float bottom, Canvas.EdgeType type)
left
- The left side of the rectangle to compare with the
current cliptop
- The top of the rectangle to compare with the current
clipright
- The right side of the rectangle to compare with the
current clipbottom
- The bottom of the rectangle to compare with the
current cliptype
- Canvas.EdgeType.AA
if the path should be considered antialiased,
since that means it may affect a larger area (more pixels) than
non-antialiased (Canvas.EdgeType.BW
).public boolean getClipBounds(Rect bounds)
bounds
- Return the clip bounds here. If it is null, ignore it but
still return true if the current clip is non-empty.public final Rect getClipBounds()
public void drawRGB(int r, int g, int b)
r
- red component (0..255) of the color to draw onto the canvasg
- green component (0..255) of the color to draw onto the canvasb
- blue component (0..255) of the color to draw onto the canvaspublic void drawARGB(int a, int r, int g, int b)
a
- alpha component (0..255) of the color to draw onto the canvasr
- red component (0..255) of the color to draw onto the canvasg
- green component (0..255) of the color to draw onto the canvasb
- blue component (0..255) of the color to draw onto the canvaspublic void drawColor(int color)
color
- the color to draw onto the canvaspublic void drawColor(int color, PorterDuff.Mode mode)
color
- the color to draw withmode
- the porter-duff mode to apply to the colorpublic void drawPaint(Paint paint)
paint
- The paint used to draw onto the canvaspublic void drawPoints(float[] pts, int offset, int count, Paint paint)
pts
- Array of points to draw [x0 y0 x1 y1 x2 y2 ...]offset
- Number of values to skip before starting to draw.count
- The number of values to process, after skipping offset
of them. Since one point uses two values, the number of
"points" that are drawn is really (count >> 1).paint
- The paint used to draw the pointspublic void drawPoints(float[] pts, Paint paint)
public void drawPoint(float x, float y, Paint paint)
public void drawLine(float startX, float startY, float stopX, float stopY, Paint paint)
Note that since a line is always "framed", the Style is ignored in the paint.
Degenerate lines (length is 0) will not be drawn.
startX
- The x-coordinate of the start point of the linestartY
- The y-coordinate of the start point of the linepaint
- The paint used to draw the linepublic void drawLines(float[] pts, int offset, int count, Paint paint)
pts
- Array of points to draw [x0 y0 x1 y1 x2 y2 ...]offset
- Number of values in the array to skip before drawing.count
- The number of values in the array to process, after
skipping "offset" of them. Since each line uses 4 values,
the number of "lines" that are drawn is really
(count >> 2).paint
- The paint used to draw the pointspublic void drawLines(float[] pts, Paint paint)
public void drawRect(RectF rect, Paint paint)
rect
- The rect to be drawnpaint
- The paint used to draw the rectpublic void drawRect(Rect r, Paint paint)
r
- The rectangle to be drawn.paint
- The paint used to draw the rectanglepublic void drawRect(float left, float top, float right, float bottom, Paint paint)
left
- The left side of the rectangle to be drawntop
- The top side of the rectangle to be drawnright
- The right side of the rectangle to be drawnbottom
- The bottom side of the rectangle to be drawnpaint
- The paint used to draw the rectpublic void drawOval(RectF oval, Paint paint)
oval
- The rectangle bounds of the oval to be drawnpublic void drawOval(float left, float top, float right, float bottom, Paint paint)
public void drawCircle(float cx, float cy, float radius, Paint paint)
cx
- The x-coordinate of the center of the cirle to be drawncy
- The y-coordinate of the center of the cirle to be drawnradius
- The radius of the cirle to be drawnpaint
- The paint used to draw the circlepublic void drawArc(RectF oval, float startAngle, float sweepAngle, boolean useCenter, Paint paint)
Draw the specified arc, which will be scaled to fit inside the specified oval.
If the start angle is negative or >= 360, the start angle is treated as start angle modulo 360.
If the sweep angle is >= 360, then the oval is drawn completely. Note that this differs slightly from SkPath::arcTo, which treats the sweep angle modulo 360. If the sweep angle is negative, the sweep angle is treated as sweep angle modulo 360
The arc is drawn clockwise. An angle of 0 degrees correspond to the geometric angle of 0 degrees (3 o'clock on a watch.)
oval
- The bounds of oval used to define the shape and size
of the arcstartAngle
- Starting angle (in degrees) where the arc beginssweepAngle
- Sweep angle (in degrees) measured clockwiseuseCenter
- If true, include the center of the oval in the arc, and
close it if it is being stroked. This will draw a wedgepaint
- The paint used to draw the arcpublic void drawArc(float left, float top, float right, float bottom, float startAngle, float sweepAngle, boolean useCenter, Paint paint)
Draw the specified arc, which will be scaled to fit inside the specified oval.
If the start angle is negative or >= 360, the start angle is treated as start angle modulo 360.
If the sweep angle is >= 360, then the oval is drawn completely. Note that this differs slightly from SkPath::arcTo, which treats the sweep angle modulo 360. If the sweep angle is negative, the sweep angle is treated as sweep angle modulo 360
The arc is drawn clockwise. An angle of 0 degrees correspond to the geometric angle of 0 degrees (3 o'clock on a watch.)
startAngle
- Starting angle (in degrees) where the arc beginssweepAngle
- Sweep angle (in degrees) measured clockwiseuseCenter
- If true, include the center of the oval in the arc, and
close it if it is being stroked. This will draw a wedgepaint
- The paint used to draw the arcpublic void drawRoundRect(RectF rect, float rx, float ry, Paint paint)
rect
- The rectangular bounds of the roundRect to be drawnrx
- The x-radius of the oval used to round the cornersry
- The y-radius of the oval used to round the cornerspaint
- The paint used to draw the roundRectpublic void drawRoundRect(float left, float top, float right, float bottom, float rx, float ry, Paint paint)
rx
- The x-radius of the oval used to round the cornersry
- The y-radius of the oval used to round the cornerspaint
- The paint used to draw the roundRectpublic void drawPath(Path path, Paint paint)
path
- The path to be drawnpaint
- The paint used to draw the pathprotected void throwIfCannotDraw(Bitmap bitmap)
public void drawPatch(NinePatch patch, Rect dst, Paint paint)
patch
- The ninepatch object to renderdst
- The destination rectangle.paint
- The paint to draw the bitmap with. may be nullpublic void drawPatch(NinePatch patch, RectF dst, Paint paint)
patch
- The ninepatch object to renderdst
- The destination rectangle.paint
- The paint to draw the bitmap with. may be nullpublic void drawBitmap(Bitmap bitmap, float left, float top, Paint paint)
Note: if the paint contains a maskfilter that generates a mask which extends beyond the bitmap's original width/height (e.g. BlurMaskFilter), then the bitmap will be drawn as if it were in a Shader with CLAMP mode. Thus the color outside of the original width/height will be the edge color replicated.
If the bitmap and canvas have different densities, this function will take care of automatically scaling the bitmap to draw at the same density as the canvas.
bitmap
- The bitmap to be drawnleft
- The position of the left side of the bitmap being drawntop
- The position of the top side of the bitmap being drawnpaint
- The paint used to draw the bitmap (may be null)public void drawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint)
Note: if the paint contains a maskfilter that generates a mask which extends beyond the bitmap's original width/height (e.g. BlurMaskFilter), then the bitmap will be drawn as if it were in a Shader with CLAMP mode. Thus the color outside of the original width/height will be the edge color replicated.
This function ignores the density associated with the bitmap. This is because the source and destination rectangle coordinate spaces are in their respective densities, so must already have the appropriate scaling factor applied.
bitmap
- The bitmap to be drawnsrc
- May be null. The subset of the bitmap to be drawndst
- The rectangle that the bitmap will be scaled/translated
to fit intopaint
- May be null. The paint used to draw the bitmappublic void drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint)
Note: if the paint contains a maskfilter that generates a mask which extends beyond the bitmap's original width/height (e.g. BlurMaskFilter), then the bitmap will be drawn as if it were in a Shader with CLAMP mode. Thus the color outside of the original width/height will be the edge color replicated.
This function ignores the density associated with the bitmap. This is because the source and destination rectangle coordinate spaces are in their respective densities, so must already have the appropriate scaling factor applied.
bitmap
- The bitmap to be drawnsrc
- May be null. The subset of the bitmap to be drawndst
- The rectangle that the bitmap will be scaled/translated
to fit intopaint
- May be null. The paint used to draw the bitmap@Deprecated public void drawBitmap(int[] colors, int offset, int stride, float x, float y, int width, int height, boolean hasAlpha, Paint paint)
hardware accelerated
canvas
requires an internal copy of color buffer contents every time this method is called. Using a
Bitmap avoids this copy, and allows the application to more explicitly control the lifetime
and copies of pixel data.colors
- Array of colors representing the pixels of the bitmapoffset
- Offset into the array of colors for the first pixelstride
- The number of colors in the array between rows (must be
>= width or <= -width).x
- The X coordinate for where to draw the bitmapy
- The Y coordinate for where to draw the bitmapwidth
- The width of the bitmapheight
- The height of the bitmaphasAlpha
- True if the alpha channel of the colors contains valid
values. If false, the alpha byte is ignored (assumed to
be 0xFF for every pixel).paint
- May be null. The paint used to draw the bitmap@Deprecated public void drawBitmap(int[] colors, int offset, int stride, int x, int y, int width, int height, boolean hasAlpha, Paint paint)
hardware accelerated
canvas
requires an internal copy of color buffer contents every time this method is called. Using a
Bitmap avoids this copy, and allows the application to more explicitly control the lifetime
and copies of pixel data.public void drawBitmap(Bitmap bitmap, Matrix matrix, Paint paint)
bitmap
- The bitmap to drawmatrix
- The matrix used to transform the bitmap when it is drawnpaint
- May be null. The paint used to draw the bitmapprotected static void checkRange(int length, int offset, int count)
public void drawBitmapMesh(Bitmap bitmap, int meshWidth, int meshHeight, float[] verts, int vertOffset, int[] colors, int colorOffset, Paint paint)
bitmap
- The bitmap to draw using the meshmeshWidth
- The number of columns in the mesh. Nothing is drawn if
this is 0meshHeight
- The number of rows in the mesh. Nothing is drawn if
this is 0verts
- Array of x,y pairs, specifying where the mesh should be
drawn. There must be at least
(meshWidth+1) * (meshHeight+1) * 2 + vertOffset values
in the arrayvertOffset
- Number of verts elements to skip before drawingcolors
- May be null. Specifies a color at each vertex, which is
interpolated across the cell, and whose values are
multiplied by the corresponding bitmap colors. If not null,
there must be at least (meshWidth+1) * (meshHeight+1) +
colorOffset values in the array.colorOffset
- Number of color elements to skip before drawingpaint
- May be null. The paint used to draw the bitmappublic void drawVertices(Canvas.VertexMode mode, int vertexCount, float[] verts, int vertOffset, float[] texs, int texOffset, int[] colors, int colorOffset, short[] indices, int indexOffset, int indexCount, Paint paint)
mode
- How to interpret the array of verticesvertexCount
- The number of values in the vertices array (and
corresponding texs and colors arrays if non-null). Each logical
vertex is two values (x, y), vertexCount must be a multiple of 2.verts
- Array of vertices for the meshvertOffset
- Number of values in the verts to skip before drawing.texs
- May be null. If not null, specifies the coordinates to sample
into the current shader (e.g. bitmap tile or gradient)texOffset
- Number of values in texs to skip before drawing.colors
- May be null. If not null, specifies a color for each
vertex, to be interpolated across the triangle.colorOffset
- Number of values in colors to skip before drawing.indices
- If not null, array of indices to reference into the
vertex (texs, colors) array.indexCount
- number of entries in the indices array (if not null).paint
- Specifies the shader to use if the texs array is non-null.public void drawText(char[] text, int index, int count, float x, float y, Paint paint)
text
- The text to be drawnx
- The x-coordinate of the origin of the text being drawny
- The y-coordinate of the baseline of the text being drawnpaint
- The paint used for the text (e.g. color, size, style)public void drawText(String text, float x, float y, Paint paint)
text
- The text to be drawnx
- The x-coordinate of the origin of the text being drawny
- The y-coordinate of the baseline of the text being drawnpaint
- The paint used for the text (e.g. color, size, style)public void drawText(String text, int start, int end, float x, float y, Paint paint)
text
- The text to be drawnstart
- The index of the first character in text to drawend
- (end - 1) is the index of the last character in text to drawx
- The x-coordinate of the origin of the text being drawny
- The y-coordinate of the baseline of the text being drawnpaint
- The paint used for the text (e.g. color, size, style)public void drawText(CharSequence text, int start, int end, float x, float y, Paint paint)
text
- The text to be drawnstart
- The index of the first character in text to drawend
- (end - 1) is the index of the last character in text
to drawx
- The x-coordinate of origin for where to draw the texty
- The y-coordinate of origin for where to draw the textpaint
- The paint used for the text (e.g. color, size, style)public void drawTextRun(char[] text, int index, int count, int contextIndex, int contextCount, float x, float y, boolean isRtl, Paint paint)
See drawTextRun(CharSequence, int, int, int, int, float, float, boolean, Paint)
for more details. This method uses a character array rather than CharSequence to
represent the string. Also, to be consistent with the pattern established in
drawText(char[], int, int, float, float, android.graphics.Paint)
, in this method count
and contextCount
are used rather
than offsets of the end position; count = end - start, contextCount = contextEnd -
contextStart
.
text
- the text to renderindex
- the start of the text to rendercount
- the count of chars to rendercontextIndex
- the start of the context for shaping. Must be
no greater than index.contextCount
- the number of characters in the context for shaping.
contexIndex + contextCount must be no less than index + count.x
- the x position at which to draw the texty
- the y position at which to draw the textisRtl
- whether the run is in RTL directionpaint
- the paintpublic void drawTextRun(CharSequence text, int start, int end, int contextStart, int contextEnd, float x, float y, boolean isRtl, Paint paint)
The run of text includes the characters from start
to end
in the text. In
addition, the range contextStart
to contextEnd
is used as context for the
purpose of complex text shaping, such as Arabic text potentially shaped differently based on
the text next to it.
All text outside the range contextStart..contextEnd
is ignored. The text between
start
and end
will be laid out and drawn.
The direction of the run is explicitly specified by isRtl
. Thus, this method is
suitable only for runs of a single direction. Alignment of the text is as determined by the
Paint's TextAlign value. Further, 0 <= contextStart <= start <= end <= contextEnd
<= text.length
must hold on entry.
Also see Paint.getRunAdvance(char[], int, int, int, int, boolean, int)
for a corresponding method to
measure the text; the advance width of the text drawn matches the value obtained from that
method.
text
- the text to renderstart
- the start of the text to render. Data before this position
can be used for shaping context.end
- the end of the text to render. Data at or after this
position can be used for shaping context.contextStart
- the index of the start of the shaping contextcontextEnd
- the index of the end of the shaping contextx
- the x position at which to draw the texty
- the y position at which to draw the textisRtl
- whether the run is in RTL directionpaint
- the paintdrawTextRun(char[], int, int, int, int, float, float, boolean, Paint)
@Deprecated public void drawPosText(char[] text, int index, int count, float[] pos, Paint paint)
text
- The text to be drawnindex
- The index of the first character to drawcount
- The number of characters to draw, starting from index.pos
- Array of [x,y] positions, used to position each
characterpaint
- The paint used for the text (e.g. color, size, style)@Deprecated public void drawPosText(String text, float[] pos, Paint paint)
text
- The text to be drawnpos
- Array of [x,y] positions, used to position each characterpaint
- The paint used for the text (e.g. color, size, style)public void drawTextOnPath(char[] text, int index, int count, Path path, float hOffset, float vOffset, Paint paint)
text
- The text to be drawnpath
- The path the text should follow for its baselinehOffset
- The distance along the path to add to the text's
starting positionvOffset
- The distance above(-) or below(+) the path to position
the textpaint
- The paint used for the text (e.g. color, size, style)public void drawTextOnPath(String text, Path path, float hOffset, float vOffset, Paint paint)
text
- The text to be drawnpath
- The path the text should follow for its baselinehOffset
- The distance along the path to add to the text's
starting positionvOffset
- The distance above(-) or below(+) the path to position
the textpaint
- The paint used for the text (e.g. color, size, style)public void drawPicture(Picture picture)
Note: This forces the picture to internally call
Picture.endRecording()
in order to prepare for playback.
picture
- The picture to be drawnpublic void drawPicture(Picture picture, RectF dst)
public void drawPicture(Picture picture, Rect dst)
public void release()
public static void freeCaches()
public static void freeTextLayoutCaches()