了解Android的Canvas.saveLayer(...)
我希望 saveLayer 方法允许我在不同的“图层”上绘制,然后一旦绘制完成,就按照我选择的顺序将图层与画布合并。
明显的问题是“为什么不重新安排绘图操作呢?”答案是我不能:
我有一条路径需要绘制到画布上。在背景/最低 z-index 中,我想绘制封闭的路径,并使用填充样式添加一些附加点。然后最重要的是,我想仅绘制路径中最初的点的轮廓。
由于我无法撤消向路径添加点,因此我唯一的选择是克隆路径,或者绘制到第二层,稍后可以将其放置在其他所有内容之上。
saveLayer() 似乎提供了该功能,但它的行为并不符合我的预期。我的操作基本流程是这样的:
int overlay = canvas.saveLayer(...);
// drawing operations for my uppermost layer
...
int background = canvas.saveLayer(...);
// drawing operations for my background layer
...
// merge the offscreen background bitmap with the canvas:
canvas.restoreToCount(background);
// merge the offscreen overlay bitmap with the canvas:
canvas.restoreToCount(overlay);
代码运行时,background和overlay的顺序根本没有改变;最先绘制的位于底部,最后绘制的位于顶部。对我来说更奇怪的是,我可以完全注释掉对 RestoreToCount() 的两次调用,并且没有任何变化。根据javadoc,在调用平衡restore()之前,不应将任何内容绘制到画布上。
显然我完全误解了这个方法的功能。任何人都可以帮助我了解 saveLayer 的用法,或者建议一种对绘图操作进行分层的替代方法吗?
谢谢! 缺口
I am hoping the saveLayer methods will allow me to do draw onto different "layers" and then once the drawing has finished, merge the layers with the canvas in whichever order i choose.
The obvious question is "why dont you just rearrange your drawing operations instead?" The answer is I can't:
I have a Path that I need to draw onto a Canvas. In the background/lowest z-index I want to draw the path closed and with a few additional points using a fill style. Then on top of that, I want to draw an outline of only the points that were originally in the Path.
Since I cannot undo the adding of points to the Path, my only choices are to clone the path, or to draw to a second layer which can later be placed on top of everything else.
saveLayer() seems to offer that functionality but it doesnt behave the way I was expecting. The basic flow of my operations is like this:
int overlay = canvas.saveLayer(...);
// drawing operations for my uppermost layer
...
int background = canvas.saveLayer(...);
// drawing operations for my background layer
...
// merge the offscreen background bitmap with the canvas:
canvas.restoreToCount(background);
// merge the offscreen overlay bitmap with the canvas:
canvas.restoreToCount(overlay);
When the code runs, the ordering of background and overlay have not changed at all; what gets drawn first is at the bottom and what gets drawn last is on top. Even stranger to me is that I can completely comment out both calls to restoreToCount() and nothing changes. According to the javadoc, nothing should be drawn to the canvas until a balancing restore() is invoked.
Obviously I am completely misunderstanding the function of this method. Can anybody help me to understand saveLayer's usage, or perhaps suggest an alternate way to layer my drawing operations?
Thx!
Nick
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
saveLayer() 不允许您以随机顺序重新排列图层。唯一的方法是自己绘制屏幕外位图。另请注意,您的视图的父视图将在您的 onDraw() 调用周围调用 save()/restore() ,这将导致您的图层被合成。
saveLayer() does not let you rearrange the layers in a random order. The only way to do it is to draw in offscreen bitmaps yourself. Note also that your view's parent will call save()/restore() around your onDraw() call, which will cause your layers to be composited.