一. Canvas常用图形绘制方法和Paint基本API
二. Paint详解
颜色
效果
drawText()相关
初始化
三. Canvas对绘制的辅助–范围裁切和几何变换
四. 图形绘制顺序的控制
图形绘制API:
drawColor
:绘制背景drawLine
:绘制直线drawPoint
:绘制点drawPath
绘制路径drawRect
:绘制矩形drawOval
:绘制弧形addXXX
:直接添加一个固定形状图形的路径moveTo
:移动画笔位置但是不留下痕迹lineTo
:直线arcTo(Rect rect,float startAngle,float sweepAngel)
:曲线close()
自动连接起点与终点不是所有的Path都需要封闭图形,
但Style为FILL
、FILL_AND_STROKE
时会自动的封闭图形,所以这时需要将Style设置为STROKE
setColor()
:设置颜色
setStyle()
:设置填充样式
setStrokeWidth()
:设置描边的宽度
作用:region的主要作用不是去绘制图片,而是用来处理图形相交时的处理逻辑
如何获取Region
set(Rect rect)
:设置一块矩形区域set(int left,int top,int right,int bottom)
:通过左上与右下坐标确定一个矩形区域setPath(Path path,Region region)
:通过Path与另外一个Region的交集获取一个新的区域,不规则区域的获取一般都是通过这个方法图形相交的几种处理方式(op方法
)
UNION
:两个区域的并集INTERSECT
:两个区域的交集XOR
:两个区域的异并集(交集外的区域)DIFFERENCE
:区域1跟区域2不同的地方REVERSE_DIFFERENCE
:区域2跟区域1不同的地方REPLACE
:最终区域为区域2setColor(int color)
setARGB(int a,int r,int g,int b)
着色器的作用是用来给空白图形进行着色用的
BitmapShader
设置一个Matrix并进行位移来控制 /** Create a new compose shader, given shaders A, B, and a combining mode.
When the mode is applied, it will be given the result from shader A as its
"dst", and the result from shader B as its "src".
@param shaderA The colors from this shader are seen as the "dst" by the mode
@param shaderB The colors from this shader are seen as the "src" by the mode
@param mode The mode that combines the colors from the two shaders. If mode
is null, then SRC_OVER is assumed.
*/
public ComposeShader(Shader shaderA, Shader shaderB, Xfermode mode) {}
shaderA
代表的是dst
,shaderB
代表的是src
,所以当我们选择Xfermode
时要注意setColorFilter(ColorFilter colorFilter)
LightingColorFilter(int mul, int add)
,颜色的过滤或者加强减弱都可以通过这个公式R' = R * mul.R / 0xff + add.R
G' = G * mul.G / 0xff + add.G
B' = B * mul.B / 0xff + add.B
public ColorMatrixColorFilter(ColorMatrix matrix)
可以看到通过传入一个ColorMatrix
来进行颜色控制,同时ColorMatrix
也提供了很多常见的API进行色彩控制setXfermode(Xfermode xfermode)
目前只有这一种Xfermode
saveLayer()
,并在绘制完成后调用restore()
开启离屏缓冲
setAntiAlias(boolean aa):是否开启抗锯齿
setStyle(Paint.Style style):设置画笔样式
设置线帽样式(实际效果有线帽的话会比原本的长度多出一小段来)
setStrokeMiter(float miter):用来设置拐角延长线的最大值
setPathEffect(PathEffect effect)
setShadowLayer(float radius, float dx, float dy, int shadowColor)
radius 是阴影的模糊范围; dx dy 是阴影的偏移量; shadowColor 是阴影的颜色。
setMaskFilter(MaskFilter maskfilter)
setDither(boolean dither):图像防抖
setFilterBitmap(boolean filter):设置是否使用双线性过滤来绘制 Bitmap
drawTextOnPath(String text, Path path, float hOffset, float vOffset, Paint paint)
hOffset:水平偏移量
vOffset:数值偏移量
StaticLayout(CharSequence source, TextPaint paint, int width, Layout.Alignment align, float spacingmult, float spacingadd, boolean includepad)
width 是文字区域的宽度,文字到达这个宽度后就会自动换行;
align 是文字的对齐方向;
spacingmult 是行间距的倍数,通常情况下填 1 就好;
spacingadd 是行间距的额外增加值,通常情况下填 0 就好;
includepad 是指是否在文字上下添加额外的空间,来避免某些过高的字符的绘制出现越界。
baseline:基准线,用来确定文本绘制的位置
ascent:系统推荐的单个字符最高高度
descent:系统推荐的单个字符最低高度
top:可绘制的最高高度
bottom:可绘制的最低高度
FontMetrics metrics = paint.getFontMetrics();
需要注意的是FontMetric获得的ascent
、descent
并不是真实的对应的线,只是用来帮助我们得到对应线段的,想要获取我们需要通过baseline
的y坐标进行加减后获取。
常见的几何变化API
使用Matrix来做变换
使用Camera来做三位变换
《Android自定义控件开发入门与实战》
HendCoder