Android 中 FrameLayout 中的分层 SurfaceView
我正在尝试为 Android 构建增强现实应用程序,并遇到了分层表面视图的问题。我需要一个表面视图来显示我将在其上叠加图形的相机预览,并且我需要一个表面视图来绘制图形。第二个表面也需要绘制在相机预览上方,但具有透明背景。在我当前的实现中,我有两个表面视图按预期工作和显示,但背景不透明,因此没有第二个表面视图的外观,绘制的图形叠加在相机预览表面视图上。如何才能实现这一目标?在搜索大量堆栈溢出问题以及其他论坛时,我遇到了许多与此问题相关的相互矛盾的意见。有人说在 Android 中分层表面视图是不可能的,而另一些人则说这是使用不同布局的问题(FrameLayout 与 LinearLayout?)具体来说,我的实现包括两个视图:一个类 CustomCameraView,它扩展了 SurfaceView,另一个类是CustomDrawView,它还扩展了 FrameLayout 中包含的 SurfaceView,其中 CustomDrawView 出现在 CustomCameraView 之后。如何将它们分层以便 CustomDrawView 看起来叠加在 CustomCameraView 上?
I am attempting to build an augmented reality application for Android and have come across the problem of layering surface views. I need a surface view to display the camera preview onto which I will overlay graphics, and I need a surface view to draw my graphics on. This second surface also needs to be drawn above the camera preview but have a transparent background. In my current implementation, I have both surface views working and appearing as they are supposed to, but the background is not transparent and therefore there is no appearance of the second surface view with the graphics drawn being superimposed on the camera preview surface view. How could this be achieved? Upon searching numerous stack overflow questions as well as other forums I've come across many conflicting opinions pertaining to this matter. Some say that layering surface views in Android is impossible whilst others say that it is a matter of using a different layout (FrameLayout vs. LinearLayout?) Specifically, my implementation includes two views: a class, CustomCameraView, that extends SurfaceView, and a class, CustomDrawView, that also extends SurfaceView contained in a FrameLayout with the CustomDrawView appearing after the CustomCameraView. How can these be layered so that CustomDrawView seems superimposed on CustomCameraView?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想很多人都尝试过这个。 Google 工程师明确指出(此处),您应该避免堆叠表面视图。即使有人找到了一些技巧来做到这一点,它也可能是不兼容的并且会导致问题。
我认为这提供了三个选项,具体取决于您的要求:
使用表面视图进行相机预览,并将视图堆叠在其顶部。这种方法的缺点是在“普通”视图上绘制 a) 速度较慢,b) 发生在 UI 线程中。如果您自己实现线程,则可以绕过 b)。但一般来说,如果您的叠加层包含 UI 元素或一些不需要经常更新的 Drawable 之类的内容,那么这可能是可行的方法。
这将为您提供更好的性能并减少运行时的开销。您只有一个 SurfaceView。在 SurfaceView 上合成叠加层并在那里绘制所有内容。当然,您可以结合使用这两种方法。
这可能是获得真正性能的方法。与上面类似,但相机视图渲染为 GLSurfaceView。
I think a lot of people have tried this. A Google Enginee stated (here) clearly, that you should avoid stacking Surface Views. Even if somebody has found some trick to do it, it is probably incompatible and will lead to problems.
I think this gives three options, depending on your requirements:
Use a Surface View for the Camera preview and stack Views on top of it. The disadvantage of this approach is that drawing on "normal" Views a) is slower and b) takes place in the UI thread. You can get around b) if you implement the threads yourself. But generally, that is probably the way to go if your overlays contain something like UI elements or a few Drawables that don't need to be updated too often.
This will give yo better performance and less overhead during runtime. You have just one SurfaceView. Composite the overlays on your SurfaceView and and draw everything there. Of course, you can combine both approaches.
This is probably the way to go for real performance. Like above, but with the camera view rendering to a OpenGL texture in a GLSurfaceView.
我使用以下方法在相机的 SurfaceView 上方有 2 个透明视图来完成此工作:
我的活动在
onCreate()
方法中设置其所有视图,我没有为此使用任何布局文件。它看起来如下:cameraPreview
的类型为SurfaceView
,animationView
和crosslinesViews
的类型为View< /代码>。后两个视图的
onDraw()
如下所示:祝你好运!
I got this working having 2 transparent views above the camera's SurfaceView using the following approach:
My activity sets up all its views in the
onCreate()
method, I make no use of any layout file for that. It looks as follows:cameraPreview
is of typeSurfaceView
,animationView
andcrosslinesViews
are of typeView
. TheonDraw()
of the 2 latter views looks as follows:Good luck!