Why is SurfaceView created several times and dynamically changing?



  • I'll try to make the right question. I've got a simple app, but for this matter, I've made it even more difficult to focus on the root of the problem. The essence of the application is part of the screen, in my case. SurfaceViewwhere a geometric figure must be displayed dynamically after each application, and each pressure shall be increased. I did it, but I have a problem. I've already cut everything I could in a operation to figure out where the bug is, and I still don't understand why I'm having three of these. SurfaceView (and it's not clear to me why it's three) which, in turn, change the button after the button is pressed, although I have to have one. SurfaceView♪ That leads me to three different sets of figures that are in turn filled instead of one. First SurfaceView a green colour shall be created: mCanvas.drawColor(Color.GREEN);♪ Now, I don't know why, when the button is set up, I understand the other. SurfaceView And the screen becomes black. After the new press, the same thing. SurfaceView(sing) on the fourth strike, returns to the green and cycle again. I understand perfectly that the problem is two lines in the button listener, but I can't understand what a mistake is. ♪ ♪

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            hideActionBar();
            initView();
    
        mSurfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
    
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                mCanvas = null;
                mSurfaceHolder = null;
                mSurfaceHolder = mSurfaceView.getHolder();
                mCanvas = mSurfaceHolder.lockCanvas();
                mCanvas.drawColor(Color.GREEN);
                mSurfaceHolder.unlockCanvasAndPost(mCanvas);
            }
    
            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
            }
    
            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
            }
        });
    
        mRectangleButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Проблема тут!
                mCanvas = mSurfaceHolder.lockCanvas();
                // drawRectangle();
                mSurfaceHolder.unlockCanvasAndPost(mCanvas);
            }
        });
    }
    

    }



  • Put it inside.

    surfaceCreated() surfaceDestroyed() surfaceChanged()

    Log.E("surfaceCreated", "OK")
    

    And look at Logi after the buttons are pressed; So we'll find out if these methods come up after. mSurfaceHolder.lockCanvas();




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2