配置多个活动,每个活动在TabHost中包含GLSurfaceView

时间:2012-05-15 14:45:18

标签: android android-tabhost glsurfaceview

我的设置如下:TabHost有两个子活动,每个活动都有一个GLSurfaceView作为内容。这两个活动当然是将onPause()和onResume()事件转发给他们的GLSurfaceViews。

第一个Activity按预期工作,但切换到另一个选项卡没有视觉效果。 LogCat显示onSurfaceCreated(),onSurfaceChanged()和onSurfaceDraw()都在GLSurfaceView实例上按预期调用。

“修复”是分别使用onPause()和onResume()中的setVisibility(View.INVISIBLE / VISIBLE)设置每个GLSurfaceView的可见性。这会导致显示正确的视图,但在更改选项卡时会产生闪烁效果的缺点。当GLSurfaceView完成更多工作时,这一点尤其明显。

基本上,我怎样才能避免设置GLSurfaceViews的可见性?

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);
}

儿童活动:

    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);
      }

    }

渲染器:

    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);
        }
    }

布局:

<?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>

1 个答案:

答案 0 :(得分:0)

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