Android FrameLayout 中的分层 SurfaceViews [英] Layered SurfaceViews in a FrameLayout in Android

查看:40
本文介绍了Android FrameLayout 中的分层 SurfaceViews的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为 Android 构建增强现实应用程序,但遇到了分层表面视图的问题.我需要一个表面视图来显示我将在其上叠加图形的相机预览,并且我需要一个表面视图来绘制我的图形.第二个表面也需要绘制在相机预览上方,但具有透明背景.在我当前的实现中,我有两个表面视图都按预期工作和显示,但背景不是透明的,因此第二个表面视图没有出现,绘制的图形叠加在相机预览表面视图上.这怎么可能实现?在搜索了许多堆栈溢出问题以及其他论坛后,我遇到了许多与此事有关的相互矛盾的意见.有人说在 Android 中分层表面视图是不可能的,而其他人说这是使用不同布局的问题(FrameLayout vs. 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?

推荐答案

我想很多人都尝试过这个.Google 工程师 (此处) 明确指出,你应该避免堆叠表面视图.即使有人找到了一些技巧来做到这一点,它也可能不兼容并会导致问题.

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:

  • 表面视图顶部的视图

使用表面视图进行相机预览并在其顶部堆叠视图.这种方法的缺点是在普通"视图上绘制 a) 速度较慢,b) 发生在 UI 线程中.如果您自己实现线程,则可以绕过 b).但一般来说,如果您的叠加层包含 UI 元素或一些不需要经常更新的 Drawable 之类的内容,那么这可能是可行的方法.

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.

  • 在 SurfaceView 中完成所有操作

这将在运行时提供更好的性能和更少的开销.你只有一个 SurfaceView.在 SurfaceView 上合成叠加层并在那里绘制所有内容.当然,您可以结合使用这两种方法.

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.

  • 在 GLSurfaceView 中完成所有操作

这可能是实现真正性能的方法.像上面一样,但相机视图渲染到 GLSurfaceView.

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.

这篇关于Android FrameLayout 中的分层 SurfaceViews的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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