项目中包含了一个腾讯地图,由于腾讯地图mapView不支持圆角背景,so决定自己画四个圆角view——CornerView,覆盖在mapView上以实现圆角矩形的效果。要实现这样的效果,需要重新定义View的onDraw()方法,一般地,重写onDraw()方法需要一个Canvas对象来进行绘图(提供多种绘图API),一个Paint对象来定义颜色和绘制属性,一个Bitmap来存储绘制的图片。我把它们总结为:Canvas定义“画什么”,Paint定义了“怎么画”,Canvas包含的Bitmap定义着“画在哪”。下面我们就来对它们分别进行介绍:
Canvas:
来看下官方文档对Canvas的定义:
Class Overview
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).
Canvas的主要功能为进行绘图操作,定义了“画什么”。我们可以把Canvas理解成系统提供给我们的一块内存区域(但实际上它只是一套画图的API,真正的内存是下面的Bitmap),并提供了一整套对这个内存区域进行操作的方法。Canvas类的常用方法如下:
drawRect(RectF rect, Paint paint) //绘制矩形
drawPath(Path path, Paint paint) //绘制一个路径
//直接绘制bitmap
drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint)
drawLine(float startX, float startY, float stopX, float stopY, Paintpaint) //画线
drawPoint(float x, float y, Paint paint) //画点
drawText(String text, float x, floaty, Paint paint) //渲染文本,Canvas类除了上面的还可以描绘文字
drawOval(RectF oval, Paint paint)//画椭圆
drawCircle(float cx, float cy, float radius,Paint paint)// 绘制圆
drawArc(RectF oval, float startAngle, float sweepAngle, boolean useCenter, Paint paint)<