java.lang.Object | |
↳ | android.graphics.Canvas |
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).
For more information about how to use Canvas, read the Canvas and Drawables developer guide.
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Canvas()
Construct an empty raster canvas.
|
|||||||||||
Canvas(Bitmap bitmap)
Construct a canvas with the specified bitmap to draw into.
|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
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(Rect rect, Region.Op op)
Modify 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(int left, int top, int right, int 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)
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(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(Rect rect)
Intersect the current clip with the specified rectangle, which is expressed in local coordinates.
|
||||||||||
boolean | clipRegion(Region region)
Intersect the current clip with the specified region.
|
||||||||||
boolean | clipRegion(Region region, Region.Op op)
Modify the current clip with the specified region.
|
||||||||||
void | concat(Matrix matrix)
Preconcat the current matrix with the specified matrix.
|
||||||||||
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 | 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. canvas.drawArc(rect,//弧线所使用的矩形区域大小 |
||||||||||
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.
|
||||||||||
void | drawBitmap(Bitmap bitmap, Matrix matrix, Paint paint)
Draw the bitmap using the specified matrix.
|
||||||||||
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
|
||||||||||
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, 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, Rect src, Rect dst, Paint paint)
Draw the specified bitmap, scaling/translating automatically to fill the destination rectangle.
|
||||||||||
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)绘制直线(startX,StartY)和(stopX,stopY)两点
Draw a line segment with the specified start and stop x,y coordinates, using the specified paint.
|
||||||||||
void | drawLines(float[] pts, Paint paint) | ||||||||||
void | drawLines(float[] pts, int offset, int count, Paint paint)
Draw a series of lines.
|
||||||||||
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 | drawPath(Path path, Paint paint)
Draw the specified path using the specified paint.
Path path = newPath(); //定义一条路径 path.moveTo(10, 10); //移动到 坐标10,10 path.lineTo(50, 60); path.lineTo(200,80); path.lineTo(10, 10); canvas.drawPath(path, paint); |
||||||||||
void | drawPicture(Picture picture, RectF dst)
Draw the picture, stretched to fit into the dst rectangle.
|
||||||||||
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 | 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)
Draw the text in the array, with each character's origin specified by the pos array.
|
||||||||||
void | drawPosText(String text, float[] pos, Paint paint)
Draw the text in the array, with each character's origin specified by the pos array.
//按照既定点 绘制文本内容 canvas.drawPosText("Android777",newfloat[]{ 10,10,//第一个字母在坐标10,10 20,20,//第二个字母在坐标20,20 30,30,//.... 40,40, 50,50, 60,60, 70,70, 80,80, 90,90, 100,100 }, 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 | 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 | drawRoundRect(RectF rect, float rx, float ry, Paint paint)绘制圆角矩阵
Draw the specified round-rect using the specified paint.
canvas.drawRoundRect(rect, 30,//x轴的半径 30,//y轴的半径 paint); |
||||||||||
void | drawText(String text, float x, float y, Paint paint)绘制文本
Draw the text, with origin at (x,y), using the specified paint.
x默认是字符串的左边在屏幕的位置,如果设置了paint.setTextAlign(Paint.Align.CENTER);那就是字符的中心
y是指定这个字符baseline在屏幕上的位置
|
||||||||||
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(char[] text, int index, int count, float x, float y, Paint paint)从字符数组的inder位置开始count个字符绘制
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(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 | 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 | 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).
|
||||||||||
final Rect | getClipBounds()
Retrieve the clip bounds.
|
||||||||||
boolean | getClipBounds(Rect bounds)
Retrieve the clip bounds, returning true if they are non-empty.
|
||||||||||
int | getDensity()
Returns the target density of the canvas. |
||||||||||
DrawFilter | getDrawFilter() | ||||||||||
int | getHeight()
Returns the height of the current drawing layer
|
||||||||||
void | getMatrix(Matrix ctm)
Return, in ctm, the current transformation matrix.
|
||||||||||
final Matrix | getMatrix()
Return a new matrix with a copy of the canvas' current transformation matrix.
|
||||||||||
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.
|
||||||||||
int | getSaveCount()
Returns the number of matrix/clip states on the Canvas' private stack.
|
||||||||||
int | getWidth()
Returns the width of the current drawing layer
|
||||||||||
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.
|
||||||||||
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(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(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 | restore()复原sava()方法之前保存的东西资源
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.
|
||||||||||
final 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.
保存已经由canvas绘画出来的东西,在save()和restore()方法之间的操作不对它们造成影响,例如旋转(roate)等。
而且对canvas的操作(roate和translate)都是临时的,restore()后不再存在 请参考:Android canvas的save()和restore()用法 |
||||||||||
int | save(int saveFlags)
Based on saveFlags, can save the current matrix and clip onto a private stack.
|
||||||||||
int | saveLayer(RectF bounds, Paint paint, int saveFlags)
This behaves the same as save(), but in addition it allocates an offscreen bitmap.
|
||||||||||
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 | saveLayerAlpha(RectF bounds, int alpha, int saveFlags)
This behaves the same as save(), but in addition it allocates an offscreen bitmap.
|
||||||||||
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.
|
||||||||||
void | scale(float sx, float sy)
Preconcat the current matrix with the specified scale.
|
||||||||||
final 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 | setMatrix(Matrix matrix)
Completely replace the current matrix with the specified matrix.
|
||||||||||
void | skew(float sx, float sy)
Preconcat the current matrix with the specified skew.
|
||||||||||
void | translate(float dx, float dy)
Preconcat the current matrix with the specified translation
在当前的坐标上平移(x,y)个像素单位 若dx <0 ,沿x轴向上平移; dx >0 沿x轴向下平移 若dy <0 ,沿y轴向上平移; dy >0 沿y轴向下平移 |
@Override
protected void onDraw(Canvas canvas) {
paint.setAntiAlias(true);
paint.setStyle(Style.STROKE);
canvas.translate(canvas.getWidth()/2, 200); //将位置移动画纸的坐标点:150,150
canvas.drawCircle(0, 0, 100, paint); //画圆圈
//使用path绘制路径文字
canvas.save();
canvas.translate(-75, -75);
Path path = new Path();
path.addArc(new RectF(0,0,150,150), -180, 180);
Paint citePaint = new Paint(paint);
citePaint.setTextSize(14);
citePaint.setStrokeWidth(1);
canvas.drawTextOnPath("http://www.android777.com", path, 28, 0, citePaint);
canvas.restore();
Paint tmpPaint = new Paint(paint); //小刻度画笔对象
tmpPaint.setStrokeWidth(1);
float y=100;
int count = 60; //总刻度数
for(int i=0 ; i