Android:SurfaceView 和 Z 级问题
我有一个 FrameLayout,首先包含一个 SurfaceView,第二个包含RelativeLayout,后者又包含各种 Button 和 TextView。
在 SurfaceView 的画布上,我绘制了许多位图,并且通过触摸和运动事件允许用户拖动它们。
这些位图在拖动时会传递到relativelayout 内的按钮等下方。
现在,我(可能是错误的)理解 SurfaceView 的“Z 级别”,或者它所传递的任何内容,与其余部分的实际 Z 级别完全无关。布局。是这样吗?如果是这样,我该如何解决它,以便将拖动的位图绘制在其他视图之上?或者还有什么其他方法可以实现全屏画布,但又不让按钮等充当覆盖层的控件。
我想我真正需要的是一个底层,其中触摸事件仍然可以被下面的按钮等拾取。但我不知道如何实现这一点,因为在重新绘制画布时,我还必须重新绘制背景。
我可以交换FrameLayout内部RelativeLayout和SurfaceView的顺序,然后使Canvas的背景透明吗?如果是这样怎么办?触摸事件仍然会“落入”下面的按钮吗?
谢谢你对我的包容,我知道我有点胡扯。
I have a FrameLayout containing first a SurfaceView, and second a RelativeLayout which in turn contians various Buttons and TextViews.
Upon the canvas of the SurfaceView I am drawing numerous Bitmaps, and, via Touch and Motion Events am allowing the user to drag them around.
These Bitmaps, when dragged around pass underneath the Buttons etc that are inside the RelativeLayout.
Now, it's my (possibly mistaken) understanding that the "Z-level" of the SurfaceView, or whatever it has that passes for it, is entirely unrelated to the actual Z-level of the rest of the Layout. Is this the case? If so, how may I get around it, so that dragged Bitmaps are drawn ontop of other Views? Or what other way can I implement a full-screen canvas and yet not have my buttons etc act like the controls of an overlay.
I guess what I actually need is an underlay, where touch events can still be picked up by the Buttons etc underneath. But I don't know how to achieve this, as, when redrawing my Canvas, I have to also redraw the background.
Can I swap the order of the RelativeLayout and the SurfaceView inside the FrameLayout, and then make the background of the Canvas transparent? If so how? Will touch events still "fall through" to the buttons underneath?
Thanks for bearing with me, I know I'm a bit of a waffler.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
SurfaceView 不是这样工作的。
SurfaceView 有两个部分,“视图”部分和“表面”部分。 “视图”部分是一个透明的孔,与其他视图元素一起布局,并合成到视图层上。 “表面”部分是一个完全独立的层,将对其进行定位和缩放以匹配“视图”部分,并由系统合成器而不是应用程序合成。
首次创建 SurfaceView 时,您可以控制“表面”层的 Z 顺序,但它将位于所有 View 元素的上方或下方。它不能位于某些视图上方和其他视图下方,因为它们是完全独立的层。 “surface”层不会捕获输入事件,因此将其放在顶部(通过 SurfaceView#setZOrderOnTop())不会影响输入焦点。
对于API 14+,您可以使用TextureView,它的行为方式对视图更加友好。
编辑:有关完整详细信息,请参阅更长的说明。
SurfaceView doesn't work that way.
SurfaceView has two parts, the "view" part and the "surface" part. The "view" part is a transparent hole that is laid out with the other view elements, and composited onto the view layer. The "surface" part is a completely separate layer that will be positioned and scaled to match the "view" part, and composited by the system compositor rather than the app.
You can control the Z-order of the "surface" layer when the SurfaceView is first created, but it's going to be above or below all View elements. It can't go above some Views and below others because they're completely independent layers. The "surface" layer does not catch input events, so having it on top (via
SurfaceView#setZOrderOnTop()
) doesn't affect input focus.For API 14+, you can use a TextureView, which behaves in a more View-friendly way.
Edit: for full details, see the much longer explanation.
你能再澄清一点吗?在你的 SurfaceView 中你有分配背景吗?如果没有,如果您的目的只是简单地拖动片段,您也可以使用 AbsoluteView。如果没有背景,您应该能够将整个视图放置在您拥有的RelativeView之上,并且只有各种按钮等绘制在顶部视图上,这些按钮是可拖动的并保持在其他所有内容之上。
Can you clarify a little bit more? In your SurfaceView do you have a background assigned? If not, you could probably also use AbsoluteView if your intention is to simply drag pieces around. If there is no background, you should be able to place the entire view above the RelativeView that you have and only have the various Buttons and such drawn on the view on top, which would be draggable and remain above everything else.