0

私は 2 つの surfaceView を持っています。一度にビュー階層に追加されるのは 1 つだけです。もう 1 つは削除されます。私の問題は、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