Canvaspublic class Canvas extends Object The Canvas class holds the "draw" calls. To draw something, you need
4 basic components: A Bitmap to hold the pixels, a Canvas to host
the draw calls (writing into the bitmap), a drawing primitive (e.g. Rect,
Path, text, Bitmap), and a paint (to describe the colors and styles for the
drawing).
Developer Guides
For more information about how to use Canvas, read the
Canvas and Drawables developer guide. |
Fields Summary |
---|
private long | mNativeCanvasWrapper | private Bitmap | mBitmap | private DrawFilter | mDrawFilter | protected int | mDensity | protected int | mScreenDensityUsed to determine when compatibility scaling is in effect. | private int | mSurfaceFormat | public static final int | DIRECTION_LTRFlag for drawTextRun indicating left-to-right run direction. | public static final int | DIRECTION_RTLFlag for drawTextRun indicating right-to-left run direction. | private static final int | MAXMIMUM_BITMAP_SIZE | private final CanvasFinalizer | mFinalizer | public static final int | MATRIX_SAVE_FLAGRestore the current matrix when restore() is called. | public static final int | CLIP_SAVE_FLAGRestore the current clip when restore() is called. | public static final int | HAS_ALPHA_LAYER_SAVE_FLAGThe layer requires a per-pixel alpha channel. | public static final int | FULL_COLOR_LAYER_SAVE_FLAGThe layer requires full 8-bit precision for each color channel. | public static final int | CLIP_TO_LAYER_SAVE_FLAGClip drawing to the bounds of the offscreen layer, omit at your own peril.
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_FLAGRestore everything when restore() is called (standard save flags).
Note: for performance reasons, it is
strongly recommended to pass this - the complete set of flags - to any
call to saveLayer() and saveLayerAlpha()
variants. |
Constructors Summary |
---|
public Canvas()Construct an empty raster canvas. Use setBitmap() to specify a bitmap to
draw into. The initial target density is {@link Bitmap#DENSITY_NONE};
this will typically be replaced when a target bitmap is set for the
canvas.
if (!isHardwareAccelerated()) {
// 0 means no native bitmap
mNativeCanvasWrapper = initRaster(0);
mFinalizer = new CanvasFinalizer(mNativeCanvasWrapper);
} else {
mFinalizer = null;
}
| public Canvas(Bitmap bitmap)Construct a canvas with the specified bitmap to draw into. The bitmap
must be mutable.
The initial target density of the canvas is the same as the given
bitmap's density.
if (!bitmap.isMutable()) {
throw new IllegalStateException("Immutable bitmap passed to Canvas constructor");
}
throwIfCannotDraw(bitmap);
mNativeCanvasWrapper = initRaster(bitmap.ni());
mFinalizer = new CanvasFinalizer(mNativeCanvasWrapper);
mBitmap = bitmap;
mDensity = bitmap.mDensity;
| public Canvas(long nativeCanvas)
if (nativeCanvas == 0) {
throw new IllegalStateException();
}
mNativeCanvasWrapper = nativeCanvas;
mFinalizer = new CanvasFinalizer(mNativeCanvasWrapper);
mDensity = Bitmap.getDefaultDensity();
|
Methods Summary |
---|
protected static void | checkRange(int length, int offset, int count)
if ((offset | count) < 0 || offset + count > length) {
throw new ArrayIndexOutOfBoundsException();
}
| public boolean | clipPath(Path path, Region.Op op)Modify the current clip with the specified path.
return native_clipPath(mNativeCanvasWrapper, path.ni(), op.nativeInt);
| public boolean | clipPath(Path path)Intersect the current clip with the specified path.
return clipPath(path, Region.Op.INTERSECT);
| public boolean | clipRect(RectF rect, Region.Op op)Modify the current clip with the specified rectangle.
return native_clipRect(mNativeCanvasWrapper, rect.left, rect.top, rect.right, rect.bottom,
op.nativeInt);
| public boolean | clipRect(Rect rect, Region.Op op)Modify the current clip with the specified rectangle, which is
expressed in local coordinates.
return native_clipRect(mNativeCanvasWrapper, rect.left, rect.top, rect.right, rect.bottom,
op.nativeInt);
| public boolean | clipRect(RectF rect)Intersect the current clip with the specified rectangle, which is
expressed in local coordinates.
return native_clipRect(mNativeCanvasWrapper, rect.left, rect.top, rect.right, rect.bottom,
Region.Op.INTERSECT.nativeInt);
| public boolean | clipRect(Rect rect)Intersect the current clip with the specified rectangle, which is
expressed in local coordinates.
return native_clipRect(mNativeCanvasWrapper, rect.left, rect.top, rect.right, rect.bottom,
Region.Op.INTERSECT.nativeInt);
| public 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.
return native_clipRect(mNativeCanvasWrapper, left, top, right, bottom, op.nativeInt);
| public boolean | clipRect(float left, float top, float right, float bottom)Intersect the current clip with the specified rectangle, which is
expressed in local coordinates.
return native_clipRect(mNativeCanvasWrapper, left, top, right, bottom,
Region.Op.INTERSECT.nativeInt);
| public boolean | clipRect(int left, int top, int right, int bottom)Intersect the current clip with the specified rectangle, which is
expressed in local coordinates.
return native_clipRect(mNativeCanvasWrapper, left, top, right, bottom,
Region.Op.INTERSECT.nativeInt);
| public boolean | clipRegion(Region region, Region.Op op)Modify the current clip with the specified region. Note that unlike
clipRect() and clipPath() which transform their arguments by the
current matrix, clipRegion() assumes its argument is already in the
coordinate system of the current layer's bitmap, and so not
transformation is performed.
return native_clipRegion(mNativeCanvasWrapper, region.ni(), op.nativeInt);
| public boolean | clipRegion(Region region)Intersect the current clip with the specified region. Note that unlike
clipRect() and clipPath() which transform their arguments by the
current matrix, clipRegion() assumes its argument is already in the
coordinate system of the current layer's bitmap, and so not
transformation is performed.
return clipRegion(region, Region.Op.INTERSECT);
| public void | concat(Matrix matrix)Preconcat the current matrix with the specified matrix. If the specified
matrix is null, this method does nothing.
if (matrix != null) native_concat(mNativeCanvasWrapper, matrix.native_instance);
| public 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.
drawColor(Color.argb(a, r, g, b));
| public 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.)
drawArc(oval.left, oval.top, oval.right, oval.bottom, startAngle, sweepAngle, useCenter,
paint);
| public 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.)
native_drawArc(mNativeCanvasWrapper, left, top, right, bottom, startAngle, sweepAngle,
useCenter, paint.mNativePaint);
| public 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.
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.
throwIfCannotDraw(bitmap);
native_drawBitmap(mNativeCanvasWrapper, bitmap.ni(), left, top,
paint != null ? paint.mNativePaint : 0, mDensity, mScreenDensity, bitmap.mDensity);
| public void | drawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint)Draw the specified bitmap, scaling/translating automatically to fill
the destination rectangle. If the source rectangle is not null, it
specifies the subset of the bitmap to draw.
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.
if (dst == null) {
throw new NullPointerException();
}
throwIfCannotDraw(bitmap);
final long nativePaint = paint == null ? 0 : paint.mNativePaint;
float left, top, right, bottom;
if (src == null) {
left = top = 0;
right = bitmap.getWidth();
bottom = bitmap.getHeight();
} else {
left = src.left;
right = src.right;
top = src.top;
bottom = src.bottom;
}
native_drawBitmap(mNativeCanvasWrapper, bitmap.ni(), left, top, right, bottom,
dst.left, dst.top, dst.right, dst.bottom, nativePaint, mScreenDensity,
bitmap.mDensity);
| public void | drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint)Draw the specified bitmap, scaling/translating automatically to fill
the destination rectangle. If the source rectangle is not null, it
specifies the subset of the bitmap to draw.
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.
if (dst == null) {
throw new NullPointerException();
}
throwIfCannotDraw(bitmap);
final long nativePaint = paint == null ? 0 : paint.mNativePaint;
int left, top, right, bottom;
if (src == null) {
left = top = 0;
right = bitmap.getWidth();
bottom = bitmap.getHeight();
} else {
left = src.left;
right = src.right;
top = src.top;
bottom = src.bottom;
}
native_drawBitmap(mNativeCanvasWrapper, bitmap.ni(), left, top, right, bottom,
dst.left, dst.top, dst.right, dst.bottom, nativePaint, mScreenDensity,
bitmap.mDensity);
| public void | drawBitmap(int[] colors, int offset, int stride, float x, float y, int width, int height, boolean hasAlpha, Paint paint)Treat the specified array of colors as a bitmap, and draw it. This gives
the same result as first creating a bitmap from the array, and then
drawing it, but this method avoids explicitly creating a bitmap object
which can be more efficient if the colors are changing often.
// check for valid input
if (width < 0) {
throw new IllegalArgumentException("width must be >= 0");
}
if (height < 0) {
throw new IllegalArgumentException("height must be >= 0");
}
if (Math.abs(stride) < width) {
throw new IllegalArgumentException("abs(stride) must be >= width");
}
int lastScanline = offset + (height - 1) * stride;
int length = colors.length;
if (offset < 0 || (offset + width > length) || lastScanline < 0
|| (lastScanline + width > length)) {
throw new ArrayIndexOutOfBoundsException();
}
// quick escape if there's nothing to draw
if (width == 0 || height == 0) {
return;
}
// punch down to native for the actual draw
native_drawBitmap(mNativeCanvasWrapper, colors, offset, stride, x, y, width, height, hasAlpha,
paint != null ? paint.mNativePaint : 0);
| public void | drawBitmap(int[] colors, int offset, int stride, int x, int y, int width, int height, boolean hasAlpha, Paint paint)Legacy version of drawBitmap(int[] colors, ...) that took ints for x,y
// call through to the common float version
drawBitmap(colors, offset, stride, (float)x, (float)y, width, height,
hasAlpha, paint);
| public void | drawBitmap(Bitmap bitmap, Matrix matrix, Paint paint)Draw the bitmap using the specified matrix.
nativeDrawBitmapMatrix(mNativeCanvasWrapper, bitmap.ni(), matrix.ni(),
paint != null ? paint.mNativePaint : 0);
| public 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. There are meshWidth+1 vertices across, and
meshHeight+1 vertices down. The verts array is accessed in row-major
order, so that the first meshWidth+1 vertices are distributed across the
top of the bitmap from left to right. A more general version of this
method is drawVertices().
if ((meshWidth | meshHeight | vertOffset | colorOffset) < 0) {
throw new ArrayIndexOutOfBoundsException();
}
if (meshWidth == 0 || meshHeight == 0) {
return;
}
int count = (meshWidth + 1) * (meshHeight + 1);
// we mul by 2 since we need two floats per vertex
checkRange(verts.length, vertOffset, count * 2);
if (colors != null) {
// no mul by 2, since we need only 1 color per vertex
checkRange(colors.length, colorOffset, count);
}
nativeDrawBitmapMesh(mNativeCanvasWrapper, bitmap.ni(), meshWidth, meshHeight,
verts, vertOffset, colors, colorOffset,
paint != null ? paint.mNativePaint : 0);
| public void | drawCircle(float cx, float cy, float radius, Paint paint)Draw the specified circle using the specified paint. If radius is <= 0,
then nothing will be drawn. The circle will be filled or framed based
on the Style in the paint.
native_drawCircle(mNativeCanvasWrapper, cx, cy, radius, paint.mNativePaint);
| public void | drawColor(int color)Fill the entire canvas' bitmap (restricted to the current clip) with the
specified color, using srcover porterduff mode.
native_drawColor(mNativeCanvasWrapper, color, PorterDuff.Mode.SRC_OVER.nativeInt);
| public 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.
native_drawColor(mNativeCanvasWrapper, color, mode.nativeInt);
| public 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.
Note that since a line is always "framed", the Style is ignored in the paint.
Degenerate lines (length is 0) will not be drawn.
native_drawLine(mNativeCanvasWrapper, startX, startY, stopX, stopY, paint.mNativePaint);
| public void | drawLines(float[] pts, int offset, int count, Paint paint)Draw a series of lines. Each line is taken from 4 consecutive values
in the pts array. Thus to draw 1 line, the array must contain at least 4
values. This is logically the same as drawing the array as follows:
drawLine(pts[0], pts[1], pts[2], pts[3]) followed by
drawLine(pts[4], pts[5], pts[6], pts[7]) and so on.
native_drawLines(mNativeCanvasWrapper, pts, offset, count, paint.mNativePaint);
| public void | drawLines(float[] pts, Paint paint)
drawLines(pts, 0, pts.length, paint);
| public void | drawOval(RectF oval, Paint paint)Draw the specified oval using the specified paint. The oval will be
filled or framed based on the Style in the paint.
if (oval == null) {
throw new NullPointerException();
}
drawOval(oval.left, oval.top, oval.right, oval.bottom, paint);
| public void | drawOval(float left, float top, float right, float bottom, Paint paint)Draw the specified oval using the specified paint. The oval will be
filled or framed based on the Style in the paint.
native_drawOval(mNativeCanvasWrapper, left, top, right, bottom, paint.mNativePaint);
| public void | drawPaint(Paint paint)Fill the entire canvas' bitmap (restricted to the current clip) with
the specified paint. This is equivalent (but faster) to drawing an
infinitely large rectangle with the specified paint.
native_drawPaint(mNativeCanvasWrapper, paint.mNativePaint);
| public void | drawPatch(NinePatch patch, Rect dst, Paint paint)Draws the specified bitmap as an N-patch (most often, a 9-patches.)
patch.drawSoftware(this, dst, paint);
| public void | drawPatch(NinePatch patch, RectF dst, Paint paint)Draws the specified bitmap as an N-patch (most often, a 9-patches.)
patch.drawSoftware(this, dst, paint);
| public void | drawPath(Path path, Paint paint)Draw the specified path using the specified paint. The path will be
filled or framed based on the Style in the paint.
native_drawPath(mNativeCanvasWrapper, path.ni(), paint.mNativePaint);
| public void | drawPicture(Picture picture)Save the canvas state, draw the picture, and restore the canvas state.
This differs from picture.draw(canvas), which does not perform any
save/restore.
Note: This forces the picture to internally call
{@link Picture#endRecording} in order to prepare for playback.
picture.endRecording();
int restoreCount = save();
picture.draw(this);
restoreToCount(restoreCount);
| public void | drawPicture(Picture picture, RectF dst)Draw the picture, stretched to fit into the dst rectangle.
save();
translate(dst.left, dst.top);
if (picture.getWidth() > 0 && picture.getHeight() > 0) {
scale(dst.width() / picture.getWidth(), dst.height() / picture.getHeight());
}
drawPicture(picture);
restore();
| public void | drawPicture(Picture picture, Rect dst)Draw the picture, stretched to fit into the dst rectangle.
save();
translate(dst.left, dst.top);
if (picture.getWidth() > 0 && picture.getHeight() > 0) {
scale((float) dst.width() / picture.getWidth(),
(float) dst.height() / picture.getHeight());
}
drawPicture(picture);
restore();
| public void | drawPoint(float x, float y, Paint paint)Helper for drawPoints() for drawing a single point.
native_drawPoint(mNativeCanvasWrapper, x, y, paint.mNativePaint);
| public void | drawPoints(float[] pts, int offset, int count, Paint paint)Draw a series of points. Each point is centered at the coordinate
specified by pts[], and its diameter is specified by the paint's stroke
width (as transformed by the canvas' CTM), with special treatment for
a stroke width of 0, which always draws exactly 1 pixel (or at most 4
if antialiasing is enabled). The shape of the point is controlled by
the paint's Cap type. The shape is a square, unless the cap type is
Round, in which case the shape is a circle.
native_drawPoints(mNativeCanvasWrapper, pts, offset, count, paint.mNativePaint);
| public void | drawPoints(float[] pts, Paint paint)Helper for drawPoints() that assumes you want to draw the entire array
drawPoints(pts, 0, pts.length, paint);
| public void | drawPosText(char[] text, int index, int count, float[] pos, Paint paint)Draw the text in the array, with each character's origin specified by
the pos array.
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).
if (index < 0 || index + count > text.length || count*2 > pos.length) {
throw new IndexOutOfBoundsException();
}
for (int i = 0; i < count; i++) {
drawText(text, index + i, 1, pos[i * 2], pos[i * 2 + 1], paint);
}
| public void | drawPosText(java.lang.String text, float[] pos, Paint paint)Draw the text in the array, with each character's origin specified by
the pos array.
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).
drawPosText(text.toCharArray(), 0, text.length(), pos, paint);
| public 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.
drawColor(Color.rgb(r, g, b));
| public void | drawRect(RectF rect, Paint paint)Draw the specified Rect using the specified paint. The rectangle will
be filled or framed based on the Style in the paint.
native_drawRect(mNativeCanvasWrapper,
rect.left, rect.top, rect.right, rect.bottom, paint.mNativePaint);
| public void | drawRect(Rect r, Paint paint)Draw the specified Rect using the specified Paint. The rectangle
will be filled or framed based on the Style in the paint.
drawRect(r.left, r.top, r.right, r.bottom, paint);
| public void | drawRect(float left, float top, float right, float bottom, Paint paint)Draw the specified Rect using the specified paint. The rectangle will
be filled or framed based on the Style in the paint.
native_drawRect(mNativeCanvasWrapper, left, top, right, bottom, paint.mNativePaint);
| public void | drawRoundRect(RectF rect, float rx, float ry, Paint paint)Draw the specified round-rect using the specified paint. The roundrect
will be filled or framed based on the Style in the paint.
drawRoundRect(rect.left, rect.top, rect.right, rect.bottom, rx, ry, paint);
| public 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. The roundrect
will be filled or framed based on the Style in the paint.
native_drawRoundRect(mNativeCanvasWrapper, left, top, right, bottom, rx, ry, paint.mNativePaint);
| public void | drawText(java.lang.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. The origin is interpreted
based on the Align setting in the Paint.
if ((start | end | (end - start) | (text.length() - end)) < 0) {
throw new IndexOutOfBoundsException();
}
if (text instanceof String || text instanceof SpannedString ||
text instanceof SpannableString) {
native_drawText(mNativeCanvasWrapper, text.toString(), start, end, x, y,
paint.mBidiFlags, paint.mNativePaint, paint.mNativeTypeface);
} else if (text instanceof GraphicsOperations) {
((GraphicsOperations) text).drawText(this, start, end, x, y,
paint);
} else {
char[] buf = TemporaryBuffer.obtain(end - start);
TextUtils.getChars(text, start, end, buf, 0);
native_drawText(mNativeCanvasWrapper, buf, 0, end - start, x, y,
paint.mBidiFlags, paint.mNativePaint, paint.mNativeTypeface);
TemporaryBuffer.recycle(buf);
}
| public 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. The
origin is interpreted based on the Align setting in the paint.
if ((index | count | (index + count) |
(text.length - index - count)) < 0) {
throw new IndexOutOfBoundsException();
}
native_drawText(mNativeCanvasWrapper, text, index, count, x, y, paint.mBidiFlags,
paint.mNativePaint, paint.mNativeTypeface);
| public void | drawText(java.lang.String text, float x, float y, Paint paint)Draw the text, with origin at (x,y), using the specified paint. The
origin is interpreted based on the Align setting in the paint.
native_drawText(mNativeCanvasWrapper, text, 0, text.length(), x, y, paint.mBidiFlags,
paint.mNativePaint, paint.mNativeTypeface);
| public void | drawText(java.lang.String text, int start, int end, float x, float y, Paint paint)Draw the text, with origin at (x,y), using the specified paint.
The origin is interpreted based on the Align setting in the paint.
if ((start | end | (end - start) | (text.length() - end)) < 0) {
throw new IndexOutOfBoundsException();
}
native_drawText(mNativeCanvasWrapper, text, start, end, x, y, paint.mBidiFlags,
paint.mNativePaint, paint.mNativeTypeface);
| public 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. The paint's Align setting determins where along the
path to start the text.
if (index < 0 || index + count > text.length) {
throw new ArrayIndexOutOfBoundsException();
}
native_drawTextOnPath(mNativeCanvasWrapper, text, index, count,
path.ni(), hOffset, vOffset,
paint.mBidiFlags, paint.mNativePaint, paint.mNativeTypeface);
| public void | drawTextOnPath(java.lang.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. The paint's Align setting determins where along the
path to start the text.
if (text.length() > 0) {
native_drawTextOnPath(mNativeCanvasWrapper, text, path.ni(), hOffset, vOffset,
paint.mBidiFlags, paint.mNativePaint, paint.mNativeTypeface);
}
| public void | drawTextRun(char[] text, int index, int count, int contextIndex, int contextCount, float x, float y, boolean isRtl, Paint paint)Render a run of all LTR or all RTL text, with shaping. This does not run
bidi on the provided text, but renders it as a uniform right-to-left or
left-to-right run, as indicated by dir. Alignment of the text is as
determined by the Paint's TextAlign value.
if (text == null) {
throw new NullPointerException("text is null");
}
if (paint == null) {
throw new NullPointerException("paint is null");
}
if ((index | count | text.length - index - count) < 0) {
throw new IndexOutOfBoundsException();
}
native_drawTextRun(mNativeCanvasWrapper, text, index, count,
contextIndex, contextCount, x, y, isRtl, paint.mNativePaint, paint.mNativeTypeface);
| public void | drawTextRun(java.lang.CharSequence text, int start, int end, int contextStart, int contextEnd, float x, float y, boolean isRtl, Paint paint)Render a run of all LTR or all RTL text, with shaping. This does not run
bidi on the provided text, but renders it as a uniform right-to-left or
left-to-right run, as indicated by dir. Alignment of the text is as
determined by the Paint's TextAlign value.
if (text == null) {
throw new NullPointerException("text is null");
}
if (paint == null) {
throw new NullPointerException("paint is null");
}
if ((start | end | end - start | text.length() - end) < 0) {
throw new IndexOutOfBoundsException();
}
if (text instanceof String || text instanceof SpannedString ||
text instanceof SpannableString) {
native_drawTextRun(mNativeCanvasWrapper, text.toString(), start, end,
contextStart, contextEnd, x, y, isRtl, paint.mNativePaint, paint.mNativeTypeface);
} else if (text instanceof GraphicsOperations) {
((GraphicsOperations) text).drawTextRun(this, start, end,
contextStart, contextEnd, x, y, isRtl, paint);
} else {
int contextLen = contextEnd - contextStart;
int len = end - start;
char[] buf = TemporaryBuffer.obtain(contextLen);
TextUtils.getChars(text, contextStart, contextEnd, buf, 0);
native_drawTextRun(mNativeCanvasWrapper, buf, start - contextStart, len,
0, contextLen, x, y, isRtl, paint.mNativePaint, paint.mNativeTypeface);
TemporaryBuffer.recycle(buf);
}
| public void | drawVertices(android.graphics.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). The
verts array is required, and specifies the x,y pairs for each vertex. If
texs is non-null, then it is used to specify the coordinate in shader
coordinates to use at each vertex (the paint must have a shader in this
case). If there is no texs array, but there is a color array, then each
color is interpolated across its corresponding triangle in a gradient. If
both texs and colors arrays are present, then they behave as before, but
the resulting color at each pixels is the result of multiplying the
colors from the shader and the color-gradient together. The indices array
is optional, but if it is present, then it is used to specify the index
of each triangle, rather than just walking through the arrays in order.
checkRange(verts.length, vertOffset, vertexCount);
if (texs != null) {
checkRange(texs.length, texOffset, vertexCount);
}
if (colors != null) {
checkRange(colors.length, colorOffset, vertexCount / 2);
}
if (indices != null) {
checkRange(indices.length, indexOffset, indexCount);
}
nativeDrawVertices(mNativeCanvasWrapper, mode.nativeInt, vertexCount, verts,
vertOffset, texs, texOffset, colors, colorOffset,
indices, indexOffset, indexCount, paint.mNativePaint);
| private static native void | finalizer(long nativeCanvas)
| public static native void | freeCaches()Free up as much memory as possible from private caches (e.g. fonts, images)
| public static native void | freeTextLayoutCaches()Free up text layout caches
| public 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. This can be useful
in a way similar to quickReject, in that it tells you that drawing
outside of these bounds will be clipped out.
return native_getClipBounds(mNativeCanvasWrapper, bounds);
| public final Rect | getClipBounds()Retrieve the bounds of the current clip (in local coordinates).
Rect r = new Rect();
getClipBounds(r);
return r;
| public int | getDensity()Returns the target density of the canvas. The default density is
derived from the density of its backing bitmap, or
{@link Bitmap#DENSITY_NONE} if there is not one.
return mDensity;
| public DrawFilter | getDrawFilter()
return mDrawFilter;
| protected javax.microedition.khronos.opengles.GL | getGL()Returns null.
return null;
| public int | getHeight()Returns the height of the current drawing layer
return native_getHeight(mNativeCanvasWrapper);
| public void | getMatrix(Matrix ctm)Return, in ctm, the current transformation matrix. This does not alter
the matrix in the canvas, but just returns a copy of it.
native_getCTM(mNativeCanvasWrapper, ctm.native_instance);
| public final Matrix | getMatrix()Return a new matrix with a copy of the canvas' current transformation
matrix.
Matrix m = new Matrix();
//noinspection deprecation
getMatrix(m);
return m;
| public int | getMaximumBitmapHeight()Returns the maximum allowed height for bitmaps drawn with this canvas.
Attempting to draw with a bitmap taller than this value will result
in an error.
return MAXMIMUM_BITMAP_SIZE;
| public int | getMaximumBitmapWidth()Returns the maximum allowed width for bitmaps drawn with this canvas.
Attempting to draw with a bitmap wider than this value will result
in an error.
return MAXMIMUM_BITMAP_SIZE;
| public long | getNativeCanvasWrapper()
return mNativeCanvasWrapper;
| public int | getSaveCount()Returns the number of matrix/clip states on the Canvas' private stack.
This will equal # save() calls - # restore() calls.
return native_getSaveCount(mNativeCanvasWrapper);
| public int | getWidth()Returns the width of the current drawing layer
return native_getWidth(mNativeCanvasWrapper);
| private static native long | initRaster(long nativeBitmapOrZero)
| public void | insertInorderBarrier()
| public void | insertReorderBarrier()
| public boolean | isHardwareAccelerated()Indicates whether this Canvas uses hardware acceleration.
Note that this method does not define what type of hardware acceleration
may or may not be used.
return false;
| public boolean | isOpaque()Return true if the device that the current layer draws into is opaque
(i.e. does not support per-pixel alpha).
return native_isOpaque(mNativeCanvasWrapper);
| public boolean | isRecordingFor(java.lang.Object o) return false;
| private static native void | nativeDrawBitmapMatrix(long nativeCanvas, long nativeBitmap, long nativeMatrix, long nativePaint)
| private static native void | nativeDrawBitmapMesh(long nativeCanvas, long nativeBitmap, int meshWidth, int meshHeight, float[] verts, int vertOffset, int[] colors, int colorOffset, long nativePaint)
| private static native void | nativeDrawVertices(long nativeCanvas, int mode, int n, float[] verts, int vertOffset, float[] texs, int texOffset, int[] colors, int colorOffset, short[] indices, int indexOffset, int indexCount, long nativePaint)
| private static native void | nativeSetDrawFilter(long nativeCanvas, long nativeFilter)
| private static native boolean | native_clipPath(long nativeCanvas, long nativePath, int regionOp)
| private static native boolean | native_clipRect(long nativeCanvas, float left, float top, float right, float bottom, int regionOp)
| private static native boolean | native_clipRegion(long nativeCanvas, long nativeRegion, int regionOp)
| private static native void | native_concat(long nativeCanvas, long nativeMatrix)
| private static native void | native_drawArc(long nativeCanvas, float left, float top, float right, float bottom, float startAngle, float sweep, boolean useCenter, long nativePaint)
| private native void | native_drawBitmap(long nativeCanvas, long nativeBitmap, float left, float top, long nativePaintOrZero, int canvasDensity, int screenDensity, int bitmapDensity)
| private native void | native_drawBitmap(long nativeCanvas, long nativeBitmap, float srcLeft, float srcTop, float srcRight, float srcBottom, float dstLeft, float dstTop, float dstRight, float dstBottom, long nativePaintOrZero, int screenDensity, int bitmapDensity)
| private static native void | native_drawBitmap(long nativeCanvas, int[] colors, int offset, int stride, float x, float y, int width, int height, boolean hasAlpha, long nativePaintOrZero)
| private static native void | native_drawCircle(long nativeCanvas, float cx, float cy, float radius, long nativePaint)
| private static native void | native_drawColor(long nativeCanvas, int color, int mode)
| private static native void | native_drawLine(long nativeCanvas, float startX, float startY, float stopX, float stopY, long nativePaint)
| private static native void | native_drawLines(long canvasHandle, float[] pts, int offset, int count, long paintHandle)
| private static native void | native_drawOval(long nativeCanvas, float left, float top, float right, float bottom, long nativePaint)
| private static native void | native_drawPaint(long nativeCanvas, long nativePaint)
| private static native void | native_drawPath(long nativeCanvas, long nativePath, long nativePaint)
| private static native void | native_drawPoint(long canvasHandle, float x, float y, long paintHandle)
| private static native void | native_drawPoints(long canvasHandle, float[] pts, int offset, int count, long paintHandle)
| private static native void | native_drawRect(long nativeCanvas, float left, float top, float right, float bottom, long nativePaint)
| private static native void | native_drawRoundRect(long nativeCanvas, float left, float top, float right, float bottom, float rx, float ry, long nativePaint)
| private static native void | native_drawText(long nativeCanvas, char[] text, int index, int count, float x, float y, int flags, long nativePaint, long nativeTypeface)
| private static native void | native_drawText(long nativeCanvas, java.lang.String text, int start, int end, float x, float y, int flags, long nativePaint, long nativeTypeface)
| private static native void | native_drawTextOnPath(long nativeCanvas, char[] text, int index, int count, long nativePath, float hOffset, float vOffset, int bidiFlags, long nativePaint, long nativeTypeface)
| private static native void | native_drawTextOnPath(long nativeCanvas, java.lang.String text, long nativePath, float hOffset, float vOffset, int flags, long nativePaint, long nativeTypeface)
| private static native void | native_drawTextRun(long nativeCanvas, java.lang.String text, int start, int end, int contextStart, int contextEnd, float x, float y, boolean isRtl, long nativePaint, long nativeTypeface)
| private static native void | native_drawTextRun(long nativeCanvas, char[] text, int start, int count, int contextStart, int contextCount, float x, float y, boolean isRtl, long nativePaint, long nativeTypeface)
| private static native void | native_getCTM(long nativeCanvas, long nativeMatrix)
| private static native boolean | native_getClipBounds(long nativeCanvas, Rect bounds)
| private static native int | native_getHeight(long canvasHandle)
| private static native int | native_getSaveCount(long canvasHandle)
| private static native int | native_getWidth(long canvasHandle)
| private static native boolean | native_isOpaque(long canvasHandle)
| private static native boolean | native_quickReject(long nativeCanvas, long nativePath)
| private static native boolean | native_quickReject(long nativeCanvas, float left, float top, float right, float bottom)
| private static native void | native_restore(long canvasHandle)
| private static native void | native_restoreToCount(long canvasHandle, int saveCount)
| private static native void | native_rotate(long canvasHandle, float degrees)
| private static native int | native_save(long canvasHandle, int saveFlags)
| private static native int | native_saveLayer(long nativeCanvas, float l, float t, float r, float b, long nativePaint, int layerFlags)
| private static native int | native_saveLayerAlpha(long nativeCanvas, float l, float t, float r, float b, int alpha, int layerFlags)
| private static native void | native_scale(long canvasHandle, float sx, float sy)
| private static native void | native_setBitmap(long canvasHandle, long bitmapHandle, boolean copyState)
| private static native void | native_setMatrix(long nativeCanvas, long nativeMatrix)
| private static native void | native_skew(long canvasHandle, float sx, float sy)
| private static native void | native_translate(long canvasHandle, float dx, float dy)
| public boolean | quickReject(RectF rect, android.graphics.Canvas$EdgeType type)Return true if the specified rectangle, after being transformed by the
current matrix, would lie completely outside of the current clip. Call
this to check if an area you intend to draw into is clipped out (and
therefore you can skip making the draw calls).
return native_quickReject(mNativeCanvasWrapper,
rect.left, rect.top, rect.right, rect.bottom);
| public boolean | quickReject(Path path, android.graphics.Canvas$EdgeType type)Return true if the specified path, after being transformed by the
current matrix, would lie completely outside of the current clip. Call
this to check if an area you intend to draw into is clipped out (and
therefore you can skip making the draw calls). Note: for speed it may
return false even if the path itself might not intersect the clip
(i.e. the bounds of the path intersects, but the path does not).
return native_quickReject(mNativeCanvasWrapper, path.ni());
| public boolean | quickReject(float left, float top, float right, float bottom, android.graphics.Canvas$EdgeType type)Return true if the specified rectangle, after being transformed by the
current matrix, would lie completely outside of the current clip. Call
this to check if an area you intend to draw into is clipped out (and
therefore you can skip making the draw calls).
return native_quickReject(mNativeCanvasWrapper, left, top, right, bottom);
| public void | release()Releases the resources associated with this canvas.
mFinalizer.dispose();
| public 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. It is
an error to call restore() more times than save() was called.
native_restore(mNativeCanvasWrapper);
| public void | restoreToCount(int saveCount)Efficient way to pop any calls to save() that happened after the save
count reached saveCount. It is an error for saveCount to be less than 1.
Example:
int count = canvas.save();
... // more calls potentially to save()
canvas.restoreToCount(count);
// now the canvas is back in the same state it was before the initial
// call to save().
native_restoreToCount(mNativeCanvasWrapper, saveCount);
| public void | rotate(float degrees)Preconcat the current matrix with the specified rotation.
native_rotate(mNativeCanvasWrapper, degrees);
| public final void | rotate(float degrees, float px, float py)Preconcat the current matrix with the specified rotation.
translate(px, py);
rotate(degrees);
translate(-px, -py);
| public int | save()Saves the current matrix and clip onto a private stack.
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.
return native_save(mNativeCanvasWrapper, MATRIX_SAVE_FLAG | CLIP_SAVE_FLAG);
| public int | save(int saveFlags)Based on saveFlags, can save the current matrix and clip onto a private
stack.
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.
return native_save(mNativeCanvasWrapper, saveFlags);
| public 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.
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 {@link #CLIP_TO_LAYER_SAVE_FLAG} is omitted from the
{@code saveFlags} parameter. It is recommended to use a
{@link android.view.View#LAYER_TYPE_HARDWARE 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 - {@link Paint#getAlpha() alpha},
{@link Paint#getXfermode() Xfermode}, and
{@link Paint#getColorFilter() ColorFilter} are applied when the
offscreen bitmap is drawn back when restore() is called.
if (bounds == null) {
bounds = new RectF(getClipBounds());
}
return saveLayer(bounds.left, bounds.top, bounds.right, bounds.bottom, paint, saveFlags);
| public int | saveLayer(RectF bounds, Paint paint)Convenience for saveLayer(bounds, paint, {@link #ALL_SAVE_FLAG})
return saveLayer(bounds, paint, ALL_SAVE_FLAG);
| public 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.
return native_saveLayer(mNativeCanvasWrapper, left, top, right, bottom,
paint != null ? paint.mNativePaint : 0,
saveFlags);
| public int | saveLayer(float left, float top, float right, float bottom, Paint paint)Convenience for saveLayer(left, top, right, bottom, paint, {@link #ALL_SAVE_FLAG})
return saveLayer(left, top, right, bottom, paint, ALL_SAVE_FLAG);
| public 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.
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 {@link #CLIP_TO_LAYER_SAVE_FLAG} is omitted from the
{@code saveFlags} parameter. It is recommended to use a
{@link android.view.View#LAYER_TYPE_HARDWARE 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 {@code alpha} parameter is applied when the offscreen bitmap is
drawn back when restore() is called.
if (bounds == null) {
bounds = new RectF(getClipBounds());
}
return saveLayerAlpha(bounds.left, bounds.top, bounds.right, bounds.bottom, alpha, saveFlags);
| public int | saveLayerAlpha(RectF bounds, int alpha)Convenience for saveLayerAlpha(bounds, alpha, {@link #ALL_SAVE_FLAG})
return saveLayerAlpha(bounds, alpha, ALL_SAVE_FLAG);
| public 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.
alpha = Math.min(255, Math.max(0, alpha));
return native_saveLayerAlpha(mNativeCanvasWrapper, left, top, right, bottom,
alpha, saveFlags);
| public int | saveLayerAlpha(float left, float top, float right, float bottom, int alpha)Helper for saveLayerAlpha(left, top, right, bottom, alpha, {@link #ALL_SAVE_FLAG})
return saveLayerAlpha(left, top, right, bottom, alpha, ALL_SAVE_FLAG);
| public void | scale(float sx, float sy)Preconcat the current matrix with the specified scale.
native_scale(mNativeCanvasWrapper, sx, sy);
| public final void | scale(float sx, float sy, float px, float py)Preconcat the current matrix with the specified scale.
translate(px, py);
scale(sx, sy);
translate(-px, -py);
| public void | setBitmap(Bitmap bitmap)Specify a bitmap for the canvas to draw into. All canvas state such as
layers, filters, and the save/restore stack are reset with the exception
of the current matrix and clip stack. Additionally, as a side-effect
the canvas' target density is updated to match that of the bitmap.
if (isHardwareAccelerated()) {
throw new RuntimeException("Can't set a bitmap device on a HW accelerated canvas");
}
if (bitmap == null) {
native_setBitmap(mNativeCanvasWrapper, 0, false);
mDensity = Bitmap.DENSITY_NONE;
} else {
if (!bitmap.isMutable()) {
throw new IllegalStateException();
}
throwIfCannotDraw(bitmap);
native_setBitmap(mNativeCanvasWrapper, bitmap.ni(), true);
mDensity = bitmap.mDensity;
}
mBitmap = bitmap;
| 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 {@link Bitmap#setDensity(int) Bitmap.setDensity(int)}.
if (mBitmap != null) {
mBitmap.setDensity(density);
}
mDensity = density;
| public void | setDrawFilter(DrawFilter filter)
long nativeFilter = 0;
if (filter != null) {
nativeFilter = filter.mNativeInt;
}
mDrawFilter = filter;
nativeSetDrawFilter(mNativeCanvasWrapper, nativeFilter);
| public void | setHighContrastText(boolean highContrastText)
| public void | setMatrix(Matrix matrix)Completely replace the current matrix with the specified matrix. If the
matrix parameter is null, then the current matrix is reset to identity.
Note: it is recommended to use {@link #concat(Matrix)},
{@link #scale(float, float)}, {@link #translate(float, float)} and
{@link #rotate(float)} instead of this method.
native_setMatrix(mNativeCanvasWrapper,
matrix == null ? 0 : matrix.native_instance);
| private void | setNativeBitmap(long bitmapHandle)setBitmap() variant for native callers with a raw bitmap handle.
native_setBitmap(mNativeCanvasWrapper, bitmapHandle, false);
| public void | setScreenDensity(int density)
mScreenDensity = density;
| public void | setViewport(int width, int height)Set the viewport dimensions if this canvas is GL based. If it is not,
this method is ignored and no exception is thrown.
| public void | skew(float sx, float sy)Preconcat the current matrix with the specified skew.
native_skew(mNativeCanvasWrapper, sx, sy);
| protected static void | throwIfCannotDraw(Bitmap bitmap)
if (bitmap.isRecycled()) {
throw new RuntimeException("Canvas: trying to use a recycled bitmap " + bitmap);
}
if (!bitmap.isPremultiplied() && bitmap.getConfig() == Bitmap.Config.ARGB_8888 &&
bitmap.hasAlpha()) {
throw new RuntimeException("Canvas: trying to use a non-premultiplied bitmap "
+ bitmap);
}
| public void | translate(float dx, float dy)Preconcat the current matrix with the specified translation
native_translate(mNativeCanvasWrapper, dx, dy);
|
|