简体   繁体   English

在TabHost中配置多个Activity,每个Activity包含一个GLSurfaceView

[英]Configuring multiple Activities each containing a GLSurfaceView within a TabHost

My set-up is as follows: a TabHost has two child Activities, each with a single GLSurfaceView as content. 我的设置如下:TabHost有两个子Activity,每个子Activity都有一个GLSurfaceView作为内容。 The two Activities are of course forwarding their onPause() and onResume() events to their GLSurfaceViews. 这两个活动当然会将其onPause()和onResume()事件转发到其GLSurfaceViews。

The first Activity works as expected, however switching to the other tab has no visual impact. 第一个活动按预期工作,但是切换到另一个选项卡没有视觉影响。 LogCat reveals that both onSurfaceCreated(), onSurfaceChanged() and onSurfaceDraw() are all invoked as expected on both GLSurfaceView instances. LogCat显示,在两个GLSurfaceView实例上,均按预期调用了onSurfaceCreated(),onSurfaceChanged()和onSurfaceDraw()。

A "fix" is to set the visibility of each GLSurfaceView using setVisibility(View.INVISIBLE/VISIBLE) in onPause() and onResume() respectively. 一个“解决方案”是分别在onPause()和onResume()中使用setVisibility(View.INVISIBLE / VISIBLE)设置每个GLSurfaceView的可见性。 This causes the correct views to show, but has the downside of causing a flickering effect when changing tab. 这会导致显示正确的视图,但不利之处在于在更改选项卡时会产生闪烁效果。 This is especially evident when the GLSurfaceView does more of a job. 当GLSurfaceView可以完成更多工作时,这一点尤其明显。

Essentially, how can I avoid having to set the VISIBILITY of the GLSurfaceViews? 本质上,如何避免设置GLSurfaceViews的可见性?

TabActivity: TabActivity:

    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.mainactivity);

      TabHost tabHost = getTabHost();
      TabHost.TabSpec spec;
      Intent intent;

      // First Vortex view
      intent = new Intent().setClass(this, VortexActivity.class);
      spec = tabHost.newTabSpec("A")
            .setIndicator("A")
            .setContent(intent);
      tabHost.addTab(spec);

      // Second Vortex view
      intent = new Intent().setClass(this, VortexActivity.class);
      spec = tabHost.newTabSpec("B")
            .setIndicator("B")
            .setContent(intent);
      tabHost.addTab(spec);
}

Child activity: 儿童活动:

    public class VortexActivity extends Activity {

        private GLSurfaceView view;

      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        view = new GLSurfaceView(this);
        view.setRenderer(new VortexRenderer());
            setContentView(view);
        }

      @Override
      public void onResume() {
        super.onResume();
        view.onResume();
        //view.setVisibility(View.VISIBLE);
      }

      @Override
      public void onPause() {
        super.onPause();
        view.onPause();
        //view.setVisibility(View.INVISIBLE);
      }

    }

Renderer: 渲染器:

    public class VortexRenderer implements GLSurfaceView.Renderer {
        public void onSurfaceCreated(GL10 glArgument, EGLConfig config) {
        }
        public void onSurfaceChanged(GL10 gl, int w, int h) {
        }
        public void onDrawFrame(GL10 gl) {
            long ms = System.currentTimeMillis() % 2000;
            if(ms > 1000)
                ms = 1000 - (ms - 1000);
            float intensity = ms / 500.0f;
            gl.glClearColor(intensity, intensity, intensity, 1.0f);
            gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        }
    }

Layout: 布局:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

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

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:padding="0dp" />

    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:padding="0dp" />
</LinearLayout>

另一个解决方法是拥有一个始终可见的GLSurfaceView,并切换显示的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM