0

我有两个surfaceView,一次只有一个添加到视图层次结构中,另一个被删除,我的问题是,当添加一个surfaceView时,有一个黑色的矩形覆盖它,我可以避免这种情况吗?

这是测试代码:

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

1 回答 1

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);
    }
于 2013-10-08T08:36:12.760 回答