Android原生绘图(五):Canvas

1,395 阅读4分钟

小知识,大挑战!本文正在参与“程序员必备小知识”创作活动。

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拥有绘图的请求权,想要完成绘制,需要4个基本元素:

  • 保存像素的Bitmap
  • 一个可以调用draw方法的canvas
  • 要绘制的内容描述
  • 一个画笔。

凭借这4个元素就可以绘制到一个bitmap上,然后渲染到屏幕上。

1.初始化方法

//构造一个空的canvas,然后利用setBitmap设置一个bitmap,为canvas指定画图的画布,最终的东西都被绘制在bitmap上
public Canvas() ;

//所有的canvas的状态类似Layer,filters 和save/restore栈都会被重置,
//canvas的目标density也会更新匹配这个bitmap
public void setBitmap(@Nullable Bitmap bitmap)

//类似空参构造,这里直接传入bitmap构造Canvas
public Canvas(@NonNull Bitmap bitmap);

如何使用,想要直接使用可以直接重写view的onDraw方法,可以获取canvas.

2.Save函数

/**
 * Saves the current matrix and clip onto a private stack.
 * <p>
 * Subsequent calls to translate,scale,rotate,skew,concat or clipRect,
 * clipPath will all operate as usual, but when the balancing call to
 * restore() is made, those calls will be forgotten, and the settings that
 * existed before the save() will be reinstated.
 *
 * @return The value to pass to restoreToCount() to balance this save()
 */
public int save()
  • Save函数会保存当前canvas的matrix和Clip信息到stack中,
  • save之后调用 translate,scale,rotate,skew,concat or clipRect,clipPath都会作用于当前Canvas
  • 当调用restore函数时这些操作将被遗忘,会恢复save之前的canvas状态。

3.saveLayer

/**
     * This behaves the same as save(), but in addition it allocates and
     * redirects drawing to an offscreen bitmap.
     * <p class="note"><strong>Note:</strong> this method is very expensive,
     * incurring more than double rendering cost for contained content. Avoid
     * using this method, especially if the bounds provided are large. It is
     * recommended to use a {@link android.view.View#LAYER_TYPE_HARDWARE hardware layer} on a View
     * to apply an xfermode, color filter, or alpha, as it will perform much
     * better than this method.
     * <p>
     * All drawing calls are directed to a newly allocated offscreen bitmap.
     * Only when the balancing call to restore() is made, is that offscreen
     * buffer drawn back to the current target of the Canvas (either the
     * screen, it's target Bitmap, or the previous layer).
     * <p>
     * /
public int saveLayer(@Nullable RectF bounds, 
@Nullable Paint paint, @Saveflags int saveFlags)

saveLayer函数类似save函数的作用,但调用了saveLayer函数会分配并生成一个屏幕以外的bitmap(意思就是不在原来的bitmap上),之后的所有操作都是在这个新的offscreen bitmap上。
savelayer这是一个非常耗费性能的方法,会导致绘制相同的内容渲染时耗费两倍多的资源。当需要的形状很大时(超屏幕)禁止使用这个方法。当应用一个xfermode ,color filter或者alpha是推荐使用硬件加速会表现更好。

3.1 总结一下

所有的东西都是绘制在bitmap上的,canvas是一个虚拟的概念,它连接着一个bitmap,东西都绘制在bitmap上,每次调用drawxx函数都生成一个透明图层(layer),最终都会覆盖绘制在bitmap上,经过渲染才显示在屏幕上,Canvas可以比屏幕大很多,但是超出屏幕范围的图像是不会显示出来的,我们也就看不到。

3.2 SaveLayer中提到了图层,什么是layer呢?

Canvas 的setBitmap函数上有一段注释:
Specify a bitmap for the canvas to draw into. All canvas state such as layers, filters, and the save/restore stack are reset.
调用setBitmap时,会为canvas连接一个bitmap,所有的canvas的状态类似layers,filters和save/restore 栈都将重置。所以layer是canvas的一种状态,可以保存,它可以承载clip,matrix,图形,颜色等信息,所以每次调用draw方法都会生成一个新的图层layer。调用draw生成的图层最终会覆盖在它所依附的bitmap上。调用restore()、resoreToCount()函数以后,将恢复到Canvas对应的最原始的bitmap上进行绘制。

我们对layer和canvas对应的bitmap有了理解,bitmap可以看成我们平时说的画布,最终的东西都是绘制在这上面的,每次调用drawXXX方法会生成一个新的透明layer,东西被绘制在layer上,然后最终会被绘制在bitmap上。


调用savelayer函数会生成新的Bitmap,所以我认为调用saveLayer会生成一个新的Canvas连接一个新的Bitmap(或者改变了原来Canvas的bitmap的引用指向),然后再调用drawXX函数也会生成新的layer,但这个layer会被绘制到新生成的Bitmap上,其他所有的rotate,clip等操作,都会作用在新的Canvas上(或者新指向的bitmap上),不会影响原始的Canvas(原始bitmap),直到调用restore函数,才会被最终绘制到原始Canvas连接的bitmap上。所以canvas是一个包含了多种状态(clip,matrix等)的类,它有点类似坐标系(规定绘制图形的位置),所有的操作都不会影响已经绘制在上面的图形,会连接一个Bitmap,所有的东西最终都会被绘制在Bitmap上。