Android自定义View之2D图形绘制(一)

关于Graphics2D图形绘制

Graphics2D是Android SDK提供的一个用于绘制图形的API,我们平时所见的各种规则或者不规则的图形(点、线、面、弧线、扇形等等)基本上都能通过Graphics2D绘制出来。

常见的绘图方法

Point类和PointF类

区别

共同点:这两个类都实现了Parcelable接口,可以实现序列化和反序列化。两个类都定义了两个成员变量x和y,用于确定点的绘制坐标
差异:Point的成员变量x和y是int类型的,PointF的成员变量x和y是float类型的,更加精确

常用的方法

1、set(int x, int y) 通过x,y设置点的坐标位置
2、set(PointF pointf) 通过另一个PointF的位置来设置(PointF独有)
3、negate() 将x,y取反
4、offset(int x, int y) 设置偏移量
5、length(float x, float y) 计算坐标原点到(x,y)的直线距离(PointF独有)

Rect类和RectF类

区别

这两个类都可以进行序列化和反序列化的操作,在方法调用上基本没有差别,只是参数精度的不一样,Rect的参数是int类型,而RectF的参数是float类型的

常用的方法

1、new Rect() 创建一个Rect的实例
2、new Rect(int left, int top, int right, int bottom) 创建一个实例并指定位置和宽高(left:左边线距离Y轴的距离,top:上边线距离X轴的距离,right:右边线距离Y周的距离,bottom:下边线距离X轴的距离)
3、new Rect(Rect r) 通过另外一个Rect创建一个新的Rect,RectF也可以通过这种方法创建
4、isEmpty()判断该矩形是否为空,即面积是否为0
5、setEmpty()将矩形四条边线的距离均设置为0
6、width() 返回矩形的宽度
7、height()返回矩形的高度
8、centerX()返回矩形中心的x坐标(int类型)
9、centerY()返回矩形中心的y坐标(int类型)
10、exactCenterX()返回矩形中心的x坐标(float类型)
11、exactCenterY()返回矩形中心的y坐标(float类型)
12、set(int left, int top, int right, int bottom)通过四条边线重新设置位置和宽高
13、set(Rect r)通过另外一个Rect重新设置位置和宽高
14、offset(int dx, int dy) 将矩形按X轴移动dx距离,按Y轴移动dy距离,相对定位
15、offsetTo(int newLeft, int newTop) 将矩形按X轴移动newLeft距离,按Y轴移动newTop距离,绝对定位
16、inset(int dx, int dy)对矩形进行缩放处理
17、contains(int x, int y)判断(x, y)这个点是否在矩形内
18、contains(int left, int top, int right, int bottom)、contains(Rect r)判断另外一个矩形是否在这个矩形的内部
19、intersect(Rect r) 两个矩形的交集
20、union(Rect r)两个矩形的并集

Bitmap类

常用的创建方法

创建一个空白的Bitmap:
Bitmap bitmap = Bitmap.createBitmap(1000, 1600, Bitmap.Config.ARGB_8888); 创建了一个宽1000,高1600,存储格式为ARGB_8888的空白图片
读取图片:
1、BitmapFactory.decodeStream(InputStream is) 从数据流中读取图片信息转换成bitmap
2、BitmapFactory.decodeByteArray(byte[] data, int offset, int length) 从字节数组中读取图片信息并转换成bitmap
3、BitmapFactory.decodeResource(Resources res, int id) 从资源文件中读取图片信息并转换成bitmap
4、BitmapFactory.decodeFile(String pathName) 从图片文件中读取图片信息并转换成bitmap

其他的常用方法:

1、copy(Config config, boolean isMutable) 复制Bitmap, isMutable为true时表示复制的新位图可以修改
2、isRecycled() 判断位图资源是否已经回收
3、recycle() 回收bitmap内存

Paint类和Canvas类

Paint类我们可以理解为画笔,Canvas就是画布了,通过画笔和画布的结合,我们才能画出自己想要的图形

Paint类常用方法

1、setColor(int color) 设置颜色
2、setAlpha(int a) 设置透明度
3、setARGB(int a, int r, int g, int b) 通过设定argb的具体值来设置颜色
4、setTextSize(float textSize) 设置字体大小
5、setTextAlign(Paint.Align align) 设置文本的对齐方式(有三个值可选:Paint.Align.LEFT, Paint.Align.CENTER, Paint.Align.RIGHT )
6、setTextSkewX(float skewx) 设置文本的倾斜度(在0~1之间取值,正负值表示倾斜的方向)
7、setUnderlineText(boolean underline) 给文本设置下划线
8、setFakeBoldText(boolean bold) 设置文本粗体
9、setStrikeThruText(boolean strike) 设置文本的删除线
10、setStyle(Paint.Style style) 设置样式(三个值可选:FILL-实心, STROKE-空心, FILL_AND_STROKE-同时使用实心和空心)
11、setsStrokeJoin(Paint.Join join) 当样式为STROKE时,指定线条连接处的拐角样式(三个值可选:BEVEL-斜角, MITER-直角, ROUND-圆角)
12、setStrokeCap(Paint.Cap cap) 设置画笔离开画布时留下的最后一点图形 (三个值可选:BUTT, ROUND, SQUARE)
13、setStrokeWidth(float width) 设置线条的宽度

Canvas类常用方法

绘制Bitmap

1、drawBitmap(Bitmap bitmap, float left, float top, Paint paint) 将bitmap绘制到画布上
2、dewaBitmap(Bitmap bitmap, Rect src, Rect des, Paint paint) 将bitmap按照src进行裁剪,并且按照des的位置和大小绘制到画布上

绘制点

1、drawPoint(float x, float y, Paint paint) 将点(x, y)绘制到画布上
2、drawPoints(float[] pts, Paint paint) 将pts中的点按顺序依次取两个值作为x,y绘制到画布上,多余的数字会自动忽略
3、drawPoints(float[] pts, int offset, int count, Paint paint) 将数组中的数,从offset处开始取count个,绘制到画布上

绘制线

1、drawLine(float startX, float startY, float stopX, float stopY, Paint paint) 在起点(startX, startY)和终点(stopX, stopY)间绘制一条直线
2、drawLines(float[] pts, Paint paint) 将pts中的点按顺序依次取四个值作为起始点坐标绘制线条到画布上,多余的数字会自动忽略
3、drawLines(float[] pts, int offset, int count, Paint paint) 将数组中的数,从offset处开始取count个,绘制到画布上

绘制矩形
直角矩形

1、drawRect(float left, float top, float right, float bottom, Paint paint)
2、drawRect(Rect r, Paint paint)
3、drawRect(RectF r, Paint paint)

圆角矩形

1、drawRoundRect(float left, float top, float right, float bottom, float rx, float ry Paint paint) 圆角半径为rx和ry
2、drawRoundRect(Rect r, float rx, float ry Paint paint) 圆角半径为rx和ry

绘制圆
椭圆

1、drawOval(float left, float top, float right, float bottom, Paint paint)
2、drawOval(Rect oval, Paint paint)

1、drawCircle(float cx, float cy, float radius, Paint paint) 绘制中心点为(ce, cy), 半径为radius的圆形

扇形和弧线

1、drawArc(float left, float top, float right, float bottom, float startAngle, float sweepAngle, boolean useCenter, Paint paint)
2、drawArc(RectF oval, float startAngle, float sweepAngle, boolean useCenter, Paint paint)
startAngle为起始角度,sweepAngle为占用角度,useCenter为true表示扇形,为false表示弧线

绘制路径

1、drawPath(Path path, Paint paint) 将路径path绘制到画布上
由于Path路径绘制里面有很多东西需要讲,所以我会在下一篇中单独讲一下Path路径绘制的内容