canvas相关API

一、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.INTERSECT

public 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_OVER
public 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()//会保留FillType
public 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为本path
public 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) {

//跳转到下一个path
public boolean nextContour() {

//获取指定长度的位置坐标及该点Matrix
public 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]));
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值