canvas中 save()和saveLayer()区别

来源:互联网 发布:ae数据模板 编辑:程序博客网 时间:2024/05/20 05:27

canvas中 save()和saveLayer()区别

这两天了解学习绘制相关的知识,对于save和saveLayer的不同之处网上没有找到很好的解释,可能是自己理解力的问题。这里重新自己梳理了下。

  • 相同点
    saveLayer可以实现save所能实现的功能
  • 不同点
    1,saveLaye生成一个独立的图层而save只是保存了一下当时画布的状态类似于一个还原点(本来就是)。
    2,saveLaye因为多了一个图层的原因更加耗费内存慎用。
    3,saveLaye可指定保存相应区域,尽量避免2中所指的情况。
    4,在使用混合模式setXfermode时会产生不同的影响。

下面将详细介绍以上所列
看下save()的API介绍:

Saves the current matrix and clip onto a private stack.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.

大致的意思是保存当前canvas的状态然后压入一个私有栈,可以接着进行一些移动,缩放,旋转剪切等操作.注意:在save之后调用的这些状态不影响save之前绘制的一些内容,但是会影响之后的一些绘制状态。当调用restore()后当前canvas状态会被重置到还原点时的状态。 这里不做过多解释,详细可以参考
http://blog.csdn.net/aigestudio/article/details/41799811

saveLayer的API介绍

This behaves the same as save(), but in addition it allocates
andredirects drawing to an offscreen bitmap。 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, or if the {@link #CLIP_TO_LAYER_SAVE_FLAG} is omitted from
the{@code saveFlags} parameter. 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. 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).

看到第一句话,功能和save类似。下面的but是我们研究的重点。这个操作会新生成一个offscreen bitmap,用过ps类似工具的完全可以把它理解为一个图层。因为新生成了一个bitmap所以就更耗费内存了,当心oom.后面是一些建议的用法。里面提到restore()。就是把当前图层所绘制的内容绘制到前一个图层。类比就是PS里面的合并图层了。

0 0