如何在同一布局中使用多个GLSurfaceView组件?

Ruf*_*fio 23 android opengl-es glsurfaceview android-layout

我正在为Android编写信息可视化API,并试图将两个自定义单元GLSurfaceView放入布局中遇到问题.此时的Custom GLSurfaceView只是一个扩展,GLSurfaceView以消除自定义方法可能导致的故障.

当我在布局中添加了两个组件并启动它运行的应用程序时.但没有任何东西被绘制,似乎它进入了一个无限循环.因为Renderers中的调试消息被打印到LogCat中.但是,如果我只使用其中一个自定义GLSurfaceView组件,它的工作完全正常.

我读到GLSurfaceView在多个活动中使用时出现问题,我想在同时使用其中两个组件时也适用.我已经尝试过这里发布的解决方法,但似乎无法让它工作.

我将不胜感激任何帮助.我选择使用openGL以获得更好的性能,但如果我不能同时使用多个组件,我想我将不得不使用Canvas.

清单如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView android:text="@string/hello" android:id="@+id/TextView01"
        android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    <com.syntronic.vtadlib.VisualizationView android:id="@+id/glview"
        android:layout_width="fill_parent" android:layout_height="300px" />


    <TextView android:text="@string/hello" android:id="@+id/TextView02"
        android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    <LinearLayout 
        android:orientation="vertical" android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <com.syntronic.vtadlib.VisualizationView android:id="@+id/glview2"
            android:layout_width="fill_parent" android:layout_height="fill_parent" />

    </LinearLayout>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

从Activity中,代码如下:

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    mSurfaceView = (VisualizationView) findViewById(R.id.glview);
    mSurfaceView2 = (VisualizationView) findViewById(R.id.glview2);

    //Enables debug flags for Errors
    //mSurfaceView.setDebugFlags(GLSurfaceView.DEBUG_CHECK_GL_ERROR);   
    //mSurfaceView2.setDebugFlags(GLSurfaceView.DEBUG_CHECK_GL_ERROR);  

    mSurfaceView.setRenderer(new CoordinateSystemRenderer());
    mSurfaceView2.setRenderer(new CoordinateSystemRenderer());

}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    mSurfaceView.onPause();
    mSurfaceView2.onPause();
}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    mSurfaceView.onResume();
    mSurfaceView2.onResume();
}
Run Code Online (Sandbox Code Playgroud)

我错过了一些明显的东西吗 或者有人可以解释为什么它不起作用?

pep*_*kin 8

[更新:此答案不再正确,从Android 5.0(Lollipop)开始.请参阅fadden的讨论答案和链接.从Android 2.0开始它也是不正确的,并且在此之前显然只是OVERLAPPING曲面的一个问题.]

您不能将2个SurfaceViews(SV)放入一个Activity中.了解为什么你应该知道SV是如何工作的.

当您创建它并放置在活动上时,它实际上不会被放置在活动中(或者它的顶部),而是在当前活动的后面创建,并在该活动中创建"透明"视图.

在Android 4.0(API 14)中有一个名为TextureView的新视图 无法在较旧的平台上创建类似View的东西.

  • [你](http://groups.google.com/group/android-developers/browse_thread/thread/08e7df2e96a7973d?pli=1) [can](http://groups.google.com/group/android-developers/ msg/14cb1e5f59103e9f)[阅读](http://groups.google.com/group/android-developers/msg/1e908e6c24c5dfb6) [about](http://stackoverflow.com/a/7661664/1010946)[it]( http://developer.android.com/reference/android/view/SurfaceView.html) (2认同)

小智 0

GLSurfaceView 背后有很多事情要做,包括管理 GLContext,我很确定你无法让它工作,即使你成功了,以后可能会遇到更多意想不到的问题。所以我真的相信这不是正确的应用程序架构。