我有两个surfaceView,其中只有一个一次添加到视图层,另一个被删除,我的问题是,当一个surfaceView添加时,有一个黑色矩形直接覆盖它,我可以避免这个吗? / p>
这是测试代码:
public class MainActivity extends Activity {
private SurfaceView mSurfaceView;
private SurfaceView mSurfaceView1;
private SurfaceHolder.Callback mSurfaceCallback;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View rv = this.getWindow().getDecorView();
final FrameLayout frameLayout = (FrameLayout) this.getWindow().getDecorView()
.findViewById(android.R.id.content);
final LinearLayout ll = new LinearLayout(this);
mSurfaceView = new SurfaceView(this);
mSurfaceView1= new SurfaceView(this);
mSurfaceCallback = new SurfaceHolder.Callback() {
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}
@Override
public void surfaceCreated(SurfaceHolder holder) {
Canvas c = null;
try
{
c = holder.lockCanvas();
c.drawColor(Color.GREEN);
Paint p = new Paint();
p.setColor(Color.WHITE);
Rect r = new Rect(100, 50, 300, 250);
c.drawRect(r, p);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (c != null)
{
holder.unlockCanvasAndPost(c);
}
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {}
};
mSurfaceView.getHolder().addCallback(mSurfaceCallback);
mSurfaceView1.getHolder().addCallback(mSurfaceCallback);
Button b = new Button(this);b.setText("view1");
Button c = new Button(this);c.setText("view2");
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
try{
ll.addView(mSurfaceView);
ll.removeView(mSurfaceView1);
}catch(Exception e) {}
}
});
c.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
try{
ll.addView(mSurfaceView1);
ll.removeView(mSurfaceView);
}catch(Exception e) {}
}
});
ll.setOrientation(LinearLayout.VERTICAL);
ll.addView(b, new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
ll.addView(c, new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
frameLayout.addView(ll);
}
}
答案 0 :(得分:1)
我认为您可以在致电onCreate
时添加两个表面视图,并在onClick
中切换其状态。像这样:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View rv = this.getWindow().getDecorView();
final FrameLayout frameLayout = (FrameLayout) this.getWindow().getDecorView()
.findViewById(android.R.id.content);
final LinearLayout ll = new LinearLayout(this);
mSurfaceView = new SurfaceView(this);
mSurfaceView1= new SurfaceView(this);
mSurfaceCallback = new SurfaceHolder.Callback() {
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}
@Override
public void surfaceCreated(SurfaceHolder holder) {
Canvas c = null;
try
{
c = holder.lockCanvas();
c.drawColor(Color.GREEN);
Paint p = new Paint();
p.setColor(Color.WHITE);
Rect r = new Rect(100, 50, 300, 250);
c.drawRect(r, p);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (c != null)
{
holder.unlockCanvasAndPost(c);
}
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {}
};
mSurfaceView.getHolder().addCallback(mSurfaceCallback);
mSurfaceView1.getHolder().addCallback(mSurfaceCallback);
Button b = new Button(this);b.setText("view1");
Button c = new Button(this);c.setText("view2");
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
try{
// ll.addView(mSurfaceView);
// ll.removeView(mSurfaceView1);
mSurfaceView.setVisibility(View.VISIBLE);
mSurfaceView1.setVisibility(View.INVISIBLE);
}catch(Exception e) {}
}
});
c.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
try{
// ll.addView(mSurfaceView1);
// ll.removeView(mSurfaceView);
mSurfaceView1.setVisibility(View.VISIBLE);
mSurfaceView.setVisibility(View.INVISIBLE);
}catch(Exception e) {}
}
});
ll.setOrientation(LinearLayout.VERTICAL);
ll.addView(b, new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
ll.addView(c, new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
// add mSurfaceView & mSurfaceView1
ll.addView(mSurfaceView);
ll.addView(mSurfaceView1);
mSurfaceView.setVisibility(View.INVISIBLE);
mSurfaceView1.setVisibility(View.INVISIBLE);
frameLayout.addView(ll);
}