2

我有一个 TabActivity,它有两个选项卡。每个选项卡与一个 Intent 相关联。而第二个选项卡在布局中有一个 SurfaceView,为了解决这个问题,我只是放了一个 100dip 宽度和 100dip 高度的 SurfaceView Tag,什么都不做。当 TabActivity已经加载完毕,我按了第二个tab。然后全屏一闪一黑。</p>

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

有人可以帮忙吗?

这是我的代码。

选项卡活动

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

0 回答 0