Override onDraw() or draw()?

后端 未结 6 989
隐瞒了意图╮
隐瞒了意图╮ 2021-02-18 20:27

My project is based on surfaceView and up until now I\'ve had all of my rendering in onDraw which I am overriding. All seemed to be OK.

However, I\'ve just updated my S

6条回答
  •  忘掉有多难
    2021-02-18 20:59

    I have the problem since ever.

    I handle it like this:

    1) Declare a method like the following.

    @SuppressLint("WrongCall")
    public void drawTheView() {
        theCanvas = null;
    
        try{
            theCanvas = getHolder().lockCanvas();
            if(theCanvas != null) {
                onDraw(theCanvas);
            }
        } finally {
            getHolder().unlockCanvasAndPost(theCanvas);
        }
    }
    

    2) Now you can modify the onDraw() Method:

    @Override
    public void onDraw(Canvas canvas) {
        //Do some drawing
    
    
    }
    

    You can call the drawTheView() method from everywhere you want and call the onDraw() method this way without getting the error...

    I think this is a practical way.

提交回复
热议问题