自定义View系列(一)

本系列是根据HenCoder大神自定义View系列学习而来,原文请查看http://hencoder.com/

 Paint.setColor(int color) 颜色

 Paint.setStyle(Paint.Style style) : FILL  STROKE  FILL_AND_STROKE 绘制样式

 Paint.setStrokeWidth(float width) 线条宽度

 Paint.setAntiAlias(boolean aa)/Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG)抗锯齿

 

 canvas.drawColor(@ColorInt int color) 颜色填充

 

 drawCircle(float centerX,float centerY, float radius, Paint paint) 画圆

 View坐标系:原点在View左上角的那个点,水平方向是x,右正左负,竖直方向是y,下正上负,

 

 

 drawRect(float left, float top, float right, float bottom, Paint paint) 画矩形 left, top, right, bottom是矩形四条边的坐标,它还有两个重载方法drawRect(RectF rect, Paint paint)drawRect(Rect rect, Paint paint),让你可以直接填写RectFRect对象来绘制矩形.

 

 drawPoint(float x, float y, Paint paint) 画点,点的形状可以通过paint.setStrokeCap(cap)设置,这是设置线条端点形状的方法,端点有圆头(Round),平头(BUTT),方头(SQUARE)三种,

 

 

 

 drawPoints(float[] pts, int offset, int count, Paint)/drawPoints(floats[] pts, Paint paint)画点(批量)

 

 drawOval(float left, float top, float right, float bottom, Paint paint) 画椭圆,它还有个重载方法,drawOval(RectF rect, Paint paint)可以直接填写RectF来绘制椭圆

 

 drawLine(float startX, float startY, float stopX, float stopY,Paint paint)画线

 

 drawLines(float[] pts, int offset, int count, Paint paint)/drawLines(float[] pts, Paint paint)批量划线

 

 drawRoundRect(float left, float top, float right, float bottom, float rx, float ry, Paint paint)画圆角矩形, left,top,right,bottom是四条边的坐标, rxry是圆角的横向半径和纵向半径,它还有一个重载方法drawRoundRect(RectF rect, float rx, float ry, Paint paint)可以直接填写RectF绘制圆角矩形

 

 drawArc(float left, float top, float right, float bottom, float start-Angel, float sweepAngel, boolean userCenter, Paint paint)绘制扇形或弧形, useCenter表示是否连接到圆心,如果不连接到圆心,就是弧形,如果连接到圆心,就是扇形,startAngel是弧形的起始角度,x轴的正向,即正右的方向,0度的位置,顺时针为正角度,逆时针为负角度,

 

 drawPath(path)可以绘制自定义图形

 Path可以描述直线,二次曲线,三次曲线,,椭圆,弧形,矩形,圆角矩形.把这些图形结合起来,就可以描述出很多复杂的图形,

 Path有两类:一类是直接描述路径,另一类是辅助的设置或计算

 Path方法第一类:直接描述:

 第一组: addXxx()------添加子图形

Path.addCircle(float x, float y, float radius, Direction dir) 添加圆,

最后一个参数dir是圆形的路径的方向(路径方向有两种,顺时针和逆时针,这个参数是在需要填充图形并且图形出现交互时,用于判断填充范围的)

类似的方法还有:addOval(float left, float top, float right, float bottom, Direction dir)/addOval(RectF oval, Direction dir)添加椭圆

addRect(float left, float top, float right, float bottom, Direction dir)/addRect(RectF rect, Direction dir)添加矩形

addRoundRect(RectF rect, float rx, float ry, Direction dir)/add-RoundRect(float left, float top, float right, float bottom, float rx,float ry, Direction dir)/addRoundRect(RectF rect, float[] radii, Direction dir)/addRoundRect(float left, float top, float right, float bottom, float[] radii, Direction dir)添加圆角矩形

 

addPath(Path path): 添加另一个path

 第二组: xxxTo()------画线(直线或曲线)

 精确坐标:lineTo(float x, float y)/相对坐标rLineTo(float x, float y):画直线

 quadTo(float x1, float y1, float x2, float y2)/rQuadTo(float dx1, float dy1, float dx2, float dy2)画二阶贝塞尔曲线

 cubicTo(float x1, float y1, float x2, float y2, float x3, float y3)/rCubicTo(float x1,floaty1, float x2, float y2, float x3, float y3)画三阶贝塞尔曲线

 moveTo(float x, float y)/rMoveTo(float x, float y)移动到目标位置

 arcTo(RectF oval, float startAngle, float sweepAngle, boolean forceMoveTo)/arcTo(float left, float top, float right, float bottom, float startAngle, float sweepAngle, boolean forceMoveTo)/arcTo(RectF oval, float startAngle, float sweepAngle)画弧形

 close()封闭当前子图形

  2. 辅助的设置或计算: Path.setFillType(Path.FillType ft)设置填充方式

 

方法中填入不同的FillType,就会有不同的填充效果. FillType的取值有四个

 EVEN_ODD

 WINDING(默认值)

 INVERSE_EVEN_ODD

 INVERSE_WINDING

后两个是前两个的反色版本,

 

 

drawBitmap(Bitmap bitmap, float left, float top, Paint paitn)Bitmap

drawText(String text, float x, float y, Paint paint) 绘制文字,文字的定制性太高,下篇讲解。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值