Skia简介和图形案例
Overview
Skia是一个开源的2D图形库,提供各种常用的API,并可在多种软硬件平台上运行。谷歌Chrome浏览器、Chrome OS、Android、火狐浏览器、火狐操作系统以及其它许多产品都使用它作为图形引擎。
Skia由谷歌出资管理,任何人都可基于BSD免费软件许可证使用Skia。Skia开发团队致力于开发其核心部分, 并广泛采纳各方对于Skia的开源贡献。
功能展示与图形案例
下图是一系列图形案例,用来展示Skia可以实现的功能。因为内容太多,浏览器无法渲染所有的图形示例,导致一部分Demo没有显示出来,变成了白色的方块。
![Skia Overview and API Reference_第1张图片](http://img.e-com-net.com/image/info8/2c3da9221445427b80c1aed8e9a20fe1.jpg)
这张图是Skia官方的示例图:
![Skia Overview and API Reference_第2张图片](http://img.e-com-net.com/image/info8/25de53ee5f6649eaab30c2a6b9052653.jpg)
API Reference
关键的类
- SkAutoCanvasRestore - Canvas save stack manager
- SkBitmap - two-dimensional raster pixel array
- SkBlendMode - pixel color arithmetic
- SkCanvas - drawing context
- SkColor - color encoding using integer numbers
- SkFont - text style and typeface
- SkImage - two dimensional array of pixels to draw
- SkImageInfo - pixel dimensions and characteristics
- SkIPoint - two integer coordinates
- SkIRect - integer rectangle
- SkMatrix - 3x3 transformation matrix
- SkPaint - color, stroke, font, effects
- SkPath - sequence of connected lines and curves
- SkPicture - sequence of drawing commands
- SkPixmap - pixel map: image info and pixel address
- SkPoint - two floating point coordinates
- SkRRect - floating point rounded rectangle
- SkRect - floating point rectangle
- SkRegion - compressed clipping mask
- SkSurface - drawing destination
- SkTextBlob - runs of glyphs
- SkTextBlobBuilder - constructor for runs of glyphs
API Overview
SkCanvas
是Skia的核心,Skia的逻辑都是围绕着 SkCanvas
对象组织的。drawRect
, drawPath
, drawText
等所有的draw calls
(绘制命令)都由 SkCanvas
来管理。
draw calls
由两个部分构成:
primitive
(图元)- SkRect
, SkPath
, etc.
color/style attributes
(颜色/样式属性)- SkPaint
SkCanvas
指明画在哪里(where),primitive
声明要画什么(what),SkPaint
说明怎么画(how)。
canvas->drawRect(rect, paint);
所以上面的代码的含义是:在给定的画布(canvas
)上绘制一个矩形,paint
参数用来描述如何绘制这个矩形,其中包含大量的状态用来描述:矩形是什么颜色,矩形是填充还是描边,应该如何与先前绘制的颜色混合。
相对于Paint
,Canvas
维护的状态较少,它的作用是:
- 指向要绘制的实际像素
- 维护
matrices
(矩阵)和 clips
(裁剪)等操作的栈
因此,在上述调用中,canvas
的矩阵操作可以转换矩形的坐标(平移,旋转,倾斜,透视),并且canvas
的剪辑操作可以限制矩形在画布上绘制的位置,除此之外所有其他样式绘图的属性都由paint
控制。
参考文档:
https://skia.org/user/api