1. 程式人生 > >多個SurfaceView的切換除了問題(因為第一個surfaceview總是在最上面,切換時後面的surfaceview就沒效果了)

多個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檔案):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <LinearLayout android:id="@+id/contentLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1000"
        android:orientation="vertical"/>


</LinearLayout>

檔案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);//該行程式碼去掉會影響三張圖片切換的效果
}