了解 Android 的 Canvas.saveLayer(...) [英] Understanding Android's Canvas.saveLayer(...)

查看:34
本文介绍了了解 Android 的 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 中,我想绘制闭合路径并使用填充样式绘制一些附加点.然后最重要的是,我想只绘制最初在 Path 中的点的轮廓.

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);

代码运行时,background和overlay的顺序没有任何变化;首先绘制的在底部,最后绘制的在顶部.对我来说更奇怪的是,我可以完全注释掉对 restoreToCount() 的两个调用,并且没有任何变化.根据 javadoc,在调用平衡 restore() 之前,不应将任何内容绘制到画布上.

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?

谢谢!尼克

推荐答案

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.

这篇关于了解 Android 的 Canvas.saveLayer(...)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆