canvas相关API

来源:互联网 发布:数据库 图标 编辑:程序博客网 时间:2024/06/08 11:49

一、canvas:画布操作

1、画布变换

//旋转public void rotate(float degrees)public final void rotate(float degrees, float px, float py)//倾斜public void skew(float sx, float sy)//缩放public void scale(float sx, float sy)public final void scale(float sx, float sy, float px, float py)//位移public void translate(float dx, float dy)

2、画布快照

//保存当前状态public int save()public int save(int saveFlags) //回滚到上一次保存的状态public void restore()//回滚到指定状态public void restoreToCount(int saveCount)//获取保存次数public int getSaveCount()//保存图层状态(没带flag的默认ALL_SAVE_FLAG)public int saveLayer(RectF bounds,                                     Paint paint, int saveFlags)public int saveLayer(RectF bounds,                                     Paint paint)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)public int saveLayerAlpha(RectF bounds,                                     int alpha, int saveFlags) public int saveLayerAlpha(RectF bounds,                                     int alpha)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)

3、Matrix(矩阵)

public void setMatrix(Matrix matrix)public void concat(Matrix matrix)

4、裁剪选区:

//设置画布的显示区域, 没带Op的均默认Region.Op.INTERSECTpublic boolean clipPath(Path path)public boolean clipPath(Path path, Region.Op op) public boolean clipRect(Rect rect)public boolean clipRect(Rect rect, Region.Op op)public boolean clipRect(RectF rect)public boolean clipRect(RectF rect, Region.Op op) public boolean clipRect(float left, float top, float right, float bottom)public boolean clipRect(float left, float top, float right, float bottom, Region.Op op)public boolean clipRegion(Region region)//废弃public boolean clipRegion(Region region, Region.Op op)//废弃

5、绘制颜色

public void drawRGB(int r, int g, int b)public void drawARGB(int a, int r, int g, int b)public void drawColor(int color)//默认PorterDuff.Mode.SRC_OVERpublic void drawColor(int color, PorterDuff.Mode mode) 

6、绘制路径(如贝塞尔曲线)

public void drawPath(Path path, Paint paint)

7、绘制文本

//绘制文字public void drawText(char[] text,                               int index, int count, float x, float y, Paint paint)public void drawText(String text,                               float x,   float y,                     Paint paint)public void drawText(String text,                               int start, int end,   float x, float y, Paint paint)public void drawText(CharSequence text,                         int start, int end,   float x, float y, Paint paint) //根据路径绘制文字public void drawTextOnPath(char[] text, int index, int count,                             Path path, float hOffset, float vOffset, Paint paint) public void drawTextOnPath(String text,                                                   Path path, float hOffset, float vOffset, Paint paint)public void drawTextRun(char[] text,int index, int count, int contextIndex,                         int contextCount, float x, float y, boolean isRtl, Paint paint)public void drawTextRun(CharSequence text, int start, int end, int contextStart,                        int contextEnd, float x, float y, boolean isRtl,Paint paint)//绘制文字时指定每个文字位置public void drawPosText(char[] text, int index, int count, float[] pos,Paint paint)//废弃public void drawPosText(String text,                       float[] pos,Paint paint)//废弃

8、绘制图片:

8.1 绘制位图

public void drawBitmap(Bitmap bitmap, float left, float top, Paint paint)public void drawBitmap(Bitmap bitmap, Rect src, RectF dst,Paint paint)public void drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint)  public void drawBitmap(int[] colors, int offset, int stride, float x,                         float y, int width, int height, boolean hasAlpha, Paint paint) //废弃public void drawBitmap(int[] colors, int offset, int stride, int x,                           int y,   int width, int height, boolean hasAlpha, Paint paint)//废弃 public void drawBitmap(Bitmap bitmap, Matrix matrix, Paint paint)

8.2 绘制矢量图

public void drawPicture(Picture picture)public void drawPicture(Picture picture, RectF dst) public void drawPicture(Picture picture, Rect dst)

9、绘制基本形状:

9.1 绘制点

public void drawPoint(float x, float y, Paint paint) public void drawPoints(float[] pts,                       Paint paint)public void drawPoints(float[] pts, int offset, int count,Paint paint)

9.2 绘制线

public void drawLine(float startX, float startY, float stopX, float stopY,Paint paint)public void drawLines(float[] pts, int offset, int count,Paint paint)           public void drawLines(float[] pts, Paint paint)

9.3 绘制矩形

public void drawRect(RectF rect,                                       Paint paint)public void drawRect(Rect r,                                           Paint paint)public void drawRect(float left, float top, float right, float bottom, Paint paint)

9.4 绘制圆角矩形

public void drawRoundRect(RectF rect,                                                                   float rx, float ry, Paint paint) public void drawRoundRect(float left, float top, float right, float bottom,                             float rx, float ry, Paint paint) 

9.5 绘制弧形

public void drawArc(RectF oval,                                                           float startAngle, float sweepAngle, boolean useCenter, Paint paint) public void drawArc(float left, float top, float right, float bottom,                     float startAngle, float sweepAngle, boolean useCenter, Paint paint) 

9.6 绘制椭圆

public void drawOval(RectF oval,                                       Paint paint) public void drawOval(float left, float top, float right, float bottom, Paint paint)

9.7 绘制圆

public void drawCircle(float cx, float cy, float radius, Paint paint)

10、顶点操作

//通过对顶点操作可以使图像形变,//drawVertices直接对画布作用,public void drawVertices( VertexMode mode, int vertexCount,  float[] verts, int vertOffset,                         float[] texs, int texOffset,  int[] colors, int colorOffset,                          short[] indices, int indexOffset, int indexCount, Paint paint) //drawBitmapMesh只对绘制的Bitmap作用public void drawBitmapMesh(Bitmap bitmap, int meshWidth, int meshHeight,                          float[] verts, int vertOffset, int[] colors, int colorOffset,                          Paint paint)

二、paint:画笔操作

//重置public void reset()//抗锯齿public void setAntiAlias(boolean aa)//抗抖动public void setDither(boolean dither)
//设置颜色public void setColor(int color)public void setARGB(int a, int r, int g, int b)//获取颜色public int getColor()
//设置画笔的风格(空心或实心),枚举参数public void setStyle(Style style) //设置空心(Stroke or StrokeAndFill)边框的宽度public void setStrokeWidth(float width) 
//颜色过滤public ColorFilter setColorFilter(ColorFilter filter) 
//遮罩滤镜public MaskFilter setMaskFilter(MaskFilter maskfilter)
//路径效果public PathEffect setPathEffect(PathEffect effect)
//着色器public Shader setShader(Shader shader)
//阴影public void setShadowLayer(float radius, float dx, float dy, int shadowColor)
//字体public Typeface setTypeface(Typeface typeface)
//图形混合模式public Xfermode setXfermode(Xfermode xfermode)

三、path:路径操作

基本操作

//重置路径public void reset()//会保留FillTypepublic void rewind()//不保留FillType//闭合路径public void close() //移动起点public void moveTo(float x, float y)public void rMoveTo(float dx, float dy)//设置终点public void setLastPoint(float dx, float dy)//连接直线public void lineTo(float x, float y)public void rLineTo(float dx, float dy) 
//是否为空public boolean isEmpty()//是否为矩形public boolean isRect(RectF rect)
//替换路径public void set(Path src) //偏移路径public void offset(float dx, float dy)public void offset(float dx, float dy, @Nullable Path dst)

布尔操作(API19)

public boolean op(Path path, Op op) //默认path1为本pathpublic boolean op(Path path1, Path path2, Op op) 

计算边界

public void computeBounds(RectF bounds, boolean exact)

设置,获取,判断和切换填充模式

public void setFillType(FillType ft) public FillType getFillType()public boolean isInverseFillType()public void toggleInverseFillType()

基本图形

Path.Direction.CW:顺时针
Path.Direction.CCW:逆时针

//矩形public void addRect(RectF rect, Direction dir)public void addRect(float left, float top, float right, float bottom, Direction dir)//圆角矩形public void addRoundRect(RectF rect,                                                               float rx, float ry, Direction dir) public void addRoundRect(float left, float top, float right, float bottom,                         float rx, float ry, Direction dir)public void addRoundRect(RectF rect,                                                               float[] radii, Direction dir)public void addRoundRect(float left, float top, float right, float bottom,                         float[] radii,Direction dir)//椭圆public void addOval(RectF oval, Direction dir) public void addOval(float left, float top, float right, float bottom, Direction dir)//圆public void addCircle(float x, float y, float radius, Direction dir)//弧线public void addArc(RectF oval,                                                           float startAngle, float sweepAngle)public void addArc(float left, float top, float right, float bottom,                     float startAngle, float sweepAngle)public void arcTo(RectF oval,                                                           float startAngle, float sweepAngle)public void arcTo(RectF oval,                                                           float startAngle, float sweepAngle, boolean forceMoveTo) public void arcTo(float left, float top, float right, float bottom,                     float startAngle,float sweepAngle, boolean forceMoveTo) 

贝塞尔曲线

//二阶public void quadTo(float x1, float y1,                     float x2, float y2)public void rQuadTo(float dx1, float dy1,                     float dx2, float dy2)//三阶public void cubicTo(float x1, float y1, float x2, float y2,                    float x3, float y3) public void rCubicTo(float x1, float y1, float x2, float y2,                     float x3, float y3)                     

四、Matrix:矩阵操作(改变坐标)

重置:

public void reset()

设置:

public void set(Matrix src) 
public void setValues(float[] values)public void getValues(float[] values) 
public boolean setConcat(Matrix a, Matrix b) public void setSkew(float kx, float ky)public void setSkew(float kx, float ky, float px, float py)public void setRotate(float degrees)public void setRotate(float degrees, float px, float py)public void setScale(float sx, float sy)public void setScale(float sx, float sy, float px, float py)public void setTranslate(float dx, float dy)
public boolean preConcat(Matrix other) public boolean preSkew(float kx, float ky)public boolean preSkew(float kx, float ky, float px, float py)public boolean preRotate(float degrees) public boolean preRotate(float degrees, float px, float py)public boolean preScale(float sx, float sy)public boolean preScale(float sx, float sy, float px, float py)public boolean preTranslate(float dx, float dy) 
public boolean postConcat(Matrix other)public boolean postSkew(float kx, float ky)public boolean postSkew(float kx, float ky, float px, float py)public boolean postRotate(float degrees)public boolean postRotate(float degrees, float px, float py)public boolean postScale(float sx, float sy)public boolean postScale(float sx, float sy, float px, float py)public boolean postTranslate(float dx, float dy) 

五、PathMeasure:测量路径

public float getLength() {public boolean isClosed() {public void setPath(Path path, boolean forceClosed) {//跳转到下一个pathpublic boolean nextContour() {//获取指定长度的位置坐标及该点Matrixpublic boolean getMatrix(float distance, Matrix matrix, int flags) {//截取片段://返回值:true 表示截取成功,结果存入dst中,false 截取失败,不会改变dst中内容//startD:起点为距离原path起点的距离,//stopD:终点为距离原path起点的距离,//dst:截取的path,//startWithMoveTo:是否移动截取片段的起点为第一个参数startD,如果为false,可保持 dst 的连续性public boolean getSegment(float startD, float stopD, Path dst, boolean startWithMoveTo) {//获取路径上某一位置的坐标以及该位置的正切值://返回值:true表示成功,数据会存入 pos 和 tan 中,false 表示失败,pos 和 tan 不会改变//distance:距离 Path 起点的长度,取值范围: 0 <= distance <= getLength//pos[]:该点的坐标值,当前点在画布上的位置,有两个数值,分别为x,y坐标。//tan[]:该点的正切值,当前点在曲线上的方向,使用 Math.atan2(tan[1], tan[0]) 获取到正切角的弧度值。public boolean getPosTan(float distance, float pos[], float tan[]) {//Math.atan2(y,x):求反正切//Math.toDegrees(angrad):弧度转角度//tan[]:两个数值:tan[1]为y坐标、tan[0]为x坐标float degrees = (float) (Math.atan2(tan[1], tan[0]) * 180.0 / Math.PI);//或float degrees = (float) Math.toDegrees(Math.atan2(tan[1], tan[0]));
原创粉丝点击