了解Android的Canvas.saveLayer(...) [英] Understanding Android's Canvas.saveLayer(...)
问题描述
我希望的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:
我有我需要画到画布上一个路径。在后台/最低的z-index我想画的路径关闭,并使用填充样式的几个附加分。然后,最重要的是,我想画的只有最初的路径点的轮廓。
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()似乎提供了该功能,但它不表现我所期待的方式。我操作的基本流程是这样的:
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);
在code运行时,背景和覆盖的顺序都没有改变;得到什么首先绘制在底部,什么得到最后绘制在上面。更奇怪的对我来说,我可以完全注释掉两个电话给restoreToCount()并没有什么变化。据javadoc的,没有什么应该被吸引到画布上,直到恢复平衡()被调用。
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.
显然,我完全误解了该方法的功能。任何人可以帮助我了解saveLayer的使用,或者是建议的另一种方法我分层绘制操作?
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! 尼克
推荐答案
saveLayer()不会让你重新排列层随机顺序。做到这一点的唯一方法就是在屏幕外的位图自己绘制。另请注意,您的观点的家长会调用保存()/恢复()在你的OnDraw()调用,这将导致你的图层进行合成。
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.
这篇关于了解Android的Canvas.saveLayer(...)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!