多个SurfaceView的切换除了问题(因为第一个surfaceview总是在最上面,切换时后面的surfaceview就没效果了)

情况是这样,自己写了一个DJContentView继承自SurfaceView,DJContentView主要是绘制图片。我选择了三张图片(如下图一),然后在程序中用TabHost创建三个标签,标签切换的时候打开不同的图片,但是第一次切换标签程序运行正常,后面再切换标签,程序就无法切换图片了,咨询下怎么处理,目前发现DJContentView中的this.setZOrderOnTop(true);代码会对切换效果有影响,但是仍不知道该问题应该怎么解决,望大侠指教下,有问题的demo的源码可在我CSDN资源里面下载,开发工具室eclipse,下载地址http://download.csdn.net/detail/libertyflying/9817553#


下面简要贴一下代码:

文件一:

public class MainActivity extends TabActivity {


private TabSpec spec;
    private Intent intent;
    private TabHost tabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//复制文件
copy();

tabHost = getTabHost();
TabWidget tabWidget = tabHost.getTabWidget();

intent = new Intent(this,CeActivity1.class);
spec = tabHost.newTabSpec("文件一").setIndicator("文件一").setContent(intent);
tabHost.addTab(spec);

intent = new Intent(this,CeActivity2.class);
spec = tabHost.newTabSpec("文件二").setIndicator("文件二").setContent(intent);
tabHost.addTab(spec);

intent = new Intent(this,CeActivity3.class);
spec = tabHost.newTabSpec("文件三").setIndicator("文件三").setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
/** 设置字体 */
   for (int i =0; i < tabWidget.getChildCount(); i++) {
    TextView tv = (TextView) tabWidget.getChildAt(i).findViewById(android.R.id.title);
    tv.setTextSize(30);
    tv.setTextColor(this.getResources().getColorStateList(android.R.color.white));
    }
}

public void copy() {
String[] names;
try {
File file = new File(Environment.getExternalStorageDirectory()+File.separator+"ceshi"+File.separator);
if(!file.exists()) {
file.mkdirs();
}
names = getAssets().list("ceshi");
for(String name:names) {
InputStream in = getAssets().open("ceshi"+File.separator+name);
/** 拷贝文件 */
saveTo(in, Environment.getExternalStorageDirectory()+File.separator+"ceshi"+File.separator+name);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.d("dianju", "获取assets资源错误!");
return;
}
}

public boolean saveTo(InputStream in,String path) throws IOException {
File file = new File(path);
OutputStream os = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int i = -1;
while((i=in.read(buffer)) != -1) {
os.write(buffer,0,i);
}
os.flush();
in.close();
os.close();
return true;
}
}


文件二(xml文件):


    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
            android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1000"
        android:orientation="vertical"/>




文件CeActivity1.java:

public class CeActivity1 extends Activity {
private LinearLayout contentLayout;
private String filePath = Environment.getExternalStorageDirectory()+"/ceshi/tu1.jpg";
DJContentView contentView;
Handler myHandler;
private boolean isListener = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.ce);
Log.d("chenlongfei", "onCreate1");
this.contentLayout = (LinearLayout) this.findViewById(R.id.contentLayout);
this.contentLayout.getViewTreeObserver().addOnPreDrawListener(new OnPreDrawListener() {

@Override
public boolean onPreDraw() {
// TODO Auto-generated method stub
if(isListener) {
isListener = false;
contentView = new DJContentView(CeActivity1.this);
//contentView.setBackgroundColor(Color.parseColor("#F63C3C"));
contentView.openAipFile(filePath);
contentLayout.addView(contentView);
}
return true;
}
});
}

}

//文件CeActivity2.java

public class CeActivity2 extends Activity {
private LinearLayout contentLayout;
DJContentView contentView;
private boolean isListener = true;
private String filePath = Environment.getExternalStorageDirectory()+"/ceshi/tu2.jpg";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.ce);
Log.d("chenlongfei", "onCreate2");
this.contentLayout = (LinearLayout) this.findViewById(R.id.contentLayout);
this.contentLayout.getViewTreeObserver().addOnPreDrawListener(new OnPreDrawListener() {

@Override
public boolean onPreDraw() {
// TODO Auto-generated method stub
if(isListener) {
isListener = false;
contentView = new DJContentView(CeActivity2.this);
//contentView.setBackgroundColor(Color.parseColor("#C0C942"));
contentView.openAipFile(filePath);
contentLayout.addView(contentView);
}
return true;
}
});
}


}


//文件CeActivity3.java

public class CeActivity3 extends Activity {
private LinearLayout contentLayout;
DJContentView contentView;
private boolean isListener = true;
private String filePath = Environment.getExternalStorageDirectory()+"/ceshi/tu3.jpg";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.ce);
Log.d("chenlongfei", "onCreate3");
this.contentLayout = (LinearLayout) this.findViewById(R.id.contentLayout);
this.contentLayout.getViewTreeObserver().addOnPreDrawListener(new OnPreDrawListener() {

@Override
public boolean onPreDraw() {
// TODO Auto-generated method stub
if(isListener) {
isListener = false;
contentView = new DJContentView(CeActivity3.this);
//contentView.setBackgroundColor(Color.parseColor("#C0C942"));
contentView.openAipFile(filePath);
contentLayout.addView(contentView);
}
return true;
}
});
}

}


//文件DJContentView构造函数(代码有点多,这里只给出构造函数部分,详细的代码请从上边给的下载地址下载整个demo进行查阅)

public DJContentView(Context context) {
super(context);
this.context = context;
getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
getHolder().addCallback(this);
this.setZOrderOnTop(true);//该行代码去掉会影响三张图片切换的效果
}







你可能感兴趣的:(多个SurfaceView的切换除了问题(因为第一个surfaceview总是在最上面,切换时后面的surfaceview就没效果了))