Android 中 FrameLayout 中的分层 SurfaceView

发布于 2024-10-17 04:59:11 字数 509 浏览 0 评论 0原文

我正在尝试为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

掀纱窥君容 2024-10-24 04:59:11

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

我认为这提供了三个选项,具体取决于您的要求:

  • 表面视图顶部的视图

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

  • 在 SurfaceView 中完成所有操作

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

  • 在 GLSurfaceView 中完成所有操作

这可能是获得真正性能的方法。与上面类似,但相机视图渲染为 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:

  • View(s) on Top of Surface View

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.

  • Do everything in a 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.

  • Do everything in a 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.

土豪我们做朋友吧 2024-10-24 04:59:11

我使用以下方法在相机的 SurfaceView 上方有 2 个透明视图来完成此工作:

我的活动在 onCreate() 方法中设置其所有视图,我没有为此使用任何布局文件。它看起来如下:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(null); //savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    cameraPreview = new CameraPreview(this);
    addContentView(cameraPreview, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

    animationView = new AnimationView(this);
    addContentView(animationView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

    crosslinesView = new CrosslinesView(this);
    addContentView(crosslinesView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
}

cameraPreview 的类型为 SurfaceViewanimationViewcrosslinesViews 的类型为 View< /代码>。后两个视图的 onDraw() 如下所示:

protected void onDraw(Canvas canvas) {
    // Have the view being transparent
    canvas.drawARGB(0, 0, 0, 0);
    // Your drawing code goes here
    // ...
}

祝你好运!

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:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(null); //savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    cameraPreview = new CameraPreview(this);
    addContentView(cameraPreview, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

    animationView = new AnimationView(this);
    addContentView(animationView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

    crosslinesView = new CrosslinesView(this);
    addContentView(crosslinesView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
}

cameraPreview is of type SurfaceView, animationView and crosslinesViews are of type View. The onDraw() of the 2 latter views looks as follows:

protected void onDraw(Canvas canvas) {
    // Have the view being transparent
    canvas.drawARGB(0, 0, 0, 0);
    // Your drawing code goes here
    // ...
}

Good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文