Canvas绘制的总结

android绘图涉及三个类,Cavans、Paint和Path,今天的博客主角就是这三位啦!让我们一起走进android绘图的世界吧。

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)
这句话源于android官方API,指出绘图的4要素:承载像素的位图、持有绘画方法调用的画布、描述画图颜色和风格的画笔、画图的类型。

二、绘制方法
1、drawArc(RectF oval, float startAngle, float sweepAngle, boolean useCenter, Paint paint)
绘制扇形
2、drawBitmap(Bitmap bitmap, Matrix matrix, Paint paint)
结合矩阵绘制位图
3、drawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint)
结合src、dst绘图,src代表截图位置,为空的话则不截取;dst代表画布显示的位置,如果大于src则缩放图片;如果小于src则放大图片
4、drawBitmap(Bitmap bitmap, float left, float top, Paint paint)
结合左侧和上侧的起始位置绘图
5、drawBitmap(Bitmap bitmap, float left, float top, Paint paint)
绘制位图的像素块(扭曲图像)
6、drawCircle(float cx, float cy, float radius, Paint paint)
绘制圆形
7、 drawLine(float startX, float startY, float stopX, float stopY, Paint paint)
绘制直线
8、drawOval(float left, float top, float right, float bottom, Paint paint)
绘制椭圆
9、drawPoint(float x, float y, Paint paint)
绘制点
10、drawRect(RectF rect, Paint paint)
绘制矩形
11、drawText(String text, float x, float y, Paint paint)
绘制文字
12、drawTextOnPath(String text, Path path, float hOffset, float vOffset, Paint paint)
结合路径绘制文字

三、画布的操作
1、restore()
合并图像
2、save()
保存图像
3、rotate(float degrees)
旋转画布
4、scale(float sx, float sy)
缩放画布
5、skew(float sx, float sy)
错切画布
6、translate(float dx, float dy)
平移画布

Paint

一、含义
The Paint class holds the style and color information about how to draw geometries, text and bitmaps.
paint包含有要绘制的几何图形、文字、位图的颜色和风格信息

二、API
设置的API:
1、setARGB(int a, int r, int g, int b)
设置颜色
2、setAlpha(int a)
设置透明度
3、setAntiAlias(boolean aa)
设置是否抗锯齿
4、setColor(int color)
设置颜色
5、setColorFilter(ColorFilter filter)
设置颜色过滤器,可以在绘制颜色时实现不用颜色的变换效果
6、setDither(boolean dither)
设置是否使用图像抖动处理,会使绘制出来的图片颜色更加平滑和饱满,图像更加清晰
7、setFilterBitmap(boolean filter)
如果该项设置为true,则图像在动画进行中会滤掉对Bitmap图像的优化操作,加快显示 速度,本设置项依赖于dither和xfermode的设置
8、setMaskFilter(MaskFilter maskfilter)
设置MaskFilter,可以用不同的MaskFilter实现滤镜的效果,如滤化,立体等
9、setPathEffect(PathEffect effect);
设置绘制路径的效果,如点画线等
10、setShader(Shader shader);
设置图像效果,使用Shader可以绘制出各种渐变效果
11、setShadowLayer(float radius ,float dx,float dy,int color);
在图形下面设置阴影层,产生阴影效果,radius为阴影的角度,dx和dy为阴影在x轴和y轴上的距离,color为阴影的颜色
12、setStyle(Paint.Style style);
设置画笔的样式,为FILL,FILL_OR_STROKE,或STROKE Style.FILL: 实心 STROKE:空心 FILL_OR_STROKE:同时实心与空心
13、setStrokeCap(Paint.Cap cap);
当画笔样式为STROKE或FILL_OR_STROKE时,设置笔刷的图形样式,如圆形样式Cap.ROUND,或方形样式Cap.SQUARE
14、setSrokeJoin(Paint.Join join);
设置绘制时各图形的结合方式,如平滑效果等
15、setStrokeWidth(float width);
当画笔样式为STROKE或FILL_OR_STROKE时,设置笔刷的粗细度
16、setXfermode(Xfermode xfermode);
设置图形重叠时的处理方式,如合并,取交集或并集

文字相关API:
1、 setFakeBoldText(boolean fakeBoldText);
模拟实现粗体文字,设置在小字体上效果会非常差
2、setSubpixelText(boolean subpixelText);
设置该项为true,将有助于文本在LCD屏幕上的显示效果
3、setTextAlign(Paint.Align align);
设置绘制文字的对齐方向
4、setTextScaleX(float scaleX);
设置绘制文字x轴的缩放比例,可以实现文字的拉伸的效果
5、 setTextSize(float textSize);
设置绘制文字的字号大小
6、setTextSkewX(float skewX);
设置斜体文字,skewX为倾斜弧度
7、 setTypeface(Typeface typeface);
设置Typeface对象,即字体风格,包括粗体,斜体以及衬线体,非衬线体等
8、 setUnderlineText(boolean underlineText);
设置带有下划线的文字效果
9、setStrikeThruText(boolean strikeThruText);
设置带有删除线的效果

获取相关数据API:
1、getTextBounds(char[] text, int index, int count, Rect bounds)
获取文字的边界值

Path

一、含义

The Path class encapsulates compound (multiple contour) geometric paths consisting of straight line segments, quadratic curves, and cubic curves. It can be drawn with canvas.drawPath(path, paint), either filled or stroked (based on the paint’s Style), or it can be used for clipping or to draw text on a path.

以上源于官方定义,从定义我们可以得知以下信息:
第一:Path是一个封装了直线段、二次曲线、立方曲线的几何路径
第二:它可以用于绘制路径,填充、截取和绘制文字

二、API
1、addArc(RectF oval, float startAngle, float sweepAngle)
添加扇形轮廓
2、addCircle(float x, float y, float radius, Path.Direction dir)
添加圆形轮廓
3、addOval(float left, float top, float right, float bottom, Path.Direction dir)
添加椭圆形轮廓
4、addRect(float left, float top, float right, float bottom, Path.Direction dir)
添加矩形轮廓
5、addRoundRect(float left, float top, float right, float bottom, float rx, float ry, Path.Direction dir)
添加圆角矩形轮廓
6、moveTo(float x, float y)
设置下一个轮廓的开始点(x,y)
7、lineTo(float x, float y)
从最后一点到指定点(x,y)添加一行

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值