当Activity with surfaceView添加到TabActivity时,当第一次激活选项卡时,窗口将闪烁整个屏幕黑色

时间:2012-10-26 03:28:28

标签: android surfaceview flicker tabactivity

我有一个TabActivity,它有两个tabs.Each选项卡与Intent关联。 并且第二个选项卡在布局中有一个surfaceview,为了解决这个问题,我只是放了一个100dip宽度和100dip高度的SurfaceView标签,什么都不做。当TabActivity加载时,我按下第二个标签。 然后整个屏幕闪烁一秒黑色。

此外,我写了一些代码让表面做了canvas.drawColor(Color.red);黑色的闪烁仍然发生。

有人可以帮忙吗?

这是我的代码。

TabActivity

public class TestFlickerAcivity extends TabActivity {

  private TabHost tabHost;

  @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_test_flicker_acivity);
      tabHost = getTabHost();
      createTabs();
    }

  private void createTabs() {
    Intent tab1Intent = new Intent(this, Tab1Activity.class);
    tab1Intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    TabSpec tab1 = tabHost.newTabSpec("TAB1").setIndicator("tab1").setContent(tab1Intent);
    tabHost.addTab(tab1);

    Intent tab2Intent = new Intent(this, Tab2Activity.class);
    tab2Intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    TabSpec tab2 = tabHost.newTabSpec("TAB2").setIndicator("tab2").setContent(tab2Intent);
    tabHost.addTab(tab2);
  }
}

Tab2布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
   android:layout_height="match_parent">

    <SurfaceView android:layout_width="100dip" android:layout_height="100dip"
       android:id="@+id/tab2Surface" />

</RelativeLayout>

Tab2活动:

public class Tab2Activity extends Activity implements Callback, Runnable {

  private boolean mIsRunning;
  private SurfaceHolder mHolder;

  @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_tab2);
      SurfaceView sv = (SurfaceView) findViewById(R.id.tab2Surface);
      sv.getHolder().addCallback(this);
    }

  @Override
    public boolean onCreateOptionsMenu(Menu menu) {
      getMenuInflater().inflate(R.menu.activity_tab2, menu);
      return true;
    }

  @Override
    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {


    }

  @Override
    public void surfaceCreated(SurfaceHolder holder) {
      mHolder = holder;
      Canvas canvas = mHolder.lockCanvas();
      canvas.drawColor(Color.RED);
      mHolder.unlockCanvasAndPost(canvas);
      Thread t = new Thread(this);
      mIsRunning = true;
      t.start();

    }

  @Override
    public void surfaceDestroyed(SurfaceHolder arg0) {
      mIsRunning = false;
    }

  @Override
    public void run() {
      while (mIsRunning) {
        Canvas canvas = mHolder.lockCanvas();
        canvas.drawColor(Color.RED);
        mHolder.unlockCanvasAndPost(canvas);
      }
    }
}

0 个答案:

没有答案