从J2ME MIDLET时我们就知道Java提供了Canvas类,而目前在Android平台中,它主要任务为管理绘制过程,TheCanvas class holds the "draw" calls. To draw something, you need 4basic 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).
该类主要提供了三种构造方法,分别为构造一个空的Canvas、从Bitmap中构造和从GL对象中创建,如下
Canvas()
Canvas(Bitmap bitmap)
Canvas(GL gl)
同时Canvas类的一些字段保存着重要的绘制方法定义,比如Canvas.HAS_ALPHA_LAYER_SAVE_FLAG保存时需要alpha层,对于Canvas类提供的方法很多,每个都很重要,下面我们一一作介绍
boolean clipPath(Path path)
boolean clipPath(Path path,Region.Op op)
boolean clipRect(float left,float top, float right, float bottom)
boolean clipRect(Rect rect)
boolean clipRect(float left,float top, float right, float bottom, Region.Op op)
boolean clipRect(Rect rect,Region.Op op)
boolean clipRect(RectF rect)
boolean clipRect(RectF rect,Region.Op op)
boolean clipRect(int left, inttop, int right, int bottom)
boolean clipRegion(Regionregion, Region.Op op)
boolean clipRegion(Regionregion)
void concat(Matrix matrix)
void drawARGB(int a, int r,int g, int b)
void drawArc(RectF oval, floatstartAngle, float sweepAngle, boolean useCenter, Paint paint)
void drawBitmap(Bitmap bitmap,Matrix matrix, Paint paint)
void drawBitmap(int[] colors,int offset, int stride, float x, float y, int width, int height, booleanhasAlpha, Paint paint)
void drawBitmap(Bitmap bitmap,Rect src, Rect dst, Paint paint)
void drawBitmap(Bitmap bitmap,float left, float top, Paint paint)
void drawBitmap(int[] colors,int offset, int stride, int x, int y, int width, int height, boolean hasAlpha,Paint paint)
void drawBitmap(Bitmap bitmap,Rect src, RectF dst, Paint paint)
void drawBitmapMesh(Bitmapbitmap, int meshWidth, int meshHeight, float[] verts, int vertOffset, int[]colors, int colorOffset, Paint paint)
void drawCircle(float cx,float cy, float radius, Paint paint)
void drawColor(int color)
void drawColor(int color,PorterDuff.Mode mode)
void drawLine(float startX,float startY, float stopX, float stopY, Paint paint)
void drawLines(float[] pts,Paint paint)
void drawLines(float[] pts,int offset, int count, Paint paint)
void drawOval(RectF oval,Paint paint)
void drawPaint(Paint paint)
void drawPath(Path path, Paintpaint)
void drawPicture(Picturepicture, RectF dst)
void drawPicture(Picturepicture, Rect dst)
void drawPicture(Picturepicture)
void drawPoint(float x, floaty, Paint paint)
void drawPoints(float[] pts,int offset, int count, Paint paint)
void drawPoints(float[] pts,Paint paint)
void drawPosText(char[] text,int index, int count, float[] pos, Paint paint)
void drawPosText(String text,float[] pos, Paint paint)
void drawRGB(int r, int g, intb)
void drawRect(RectF rect,Paint paint)
void drawRect(float left,float top, float right, float bottom, Paint paint)
void drawRect(Rect r, Paintpaint)
void drawRoundRect(RectF rect,float rx, float ry, Paint paint)
void drawText(String text, intstart, int end, float x, float y, Paint paint)
void drawText(char[] text, intindex, int count, float x, float y, Paint paint)
void drawText(String text,float x, float y, Paint paint)
void drawText(CharSequencetext, int start, int end, float x, float y, Paint paint)
void drawTextOnPath(Stringtext, Path path, float hOffset, float vOffset, Paint paint)
void drawTextOnPath(char[]text, int index, int count, Path path, float hOffset, float vOffset, Paintpain