Override onDraw() or draw()?

2019-04-03 18:21发布

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 SDK and now it gives me an error telling me:

Suspicious method call; should probably call "draw" rather than "onDraw"

Could someone please explain the difference between these two?

I've read some similar questions around the net but I've not found an explanation that I understand.

Thanks

6条回答
Lonely孤独者°
2楼-- · 2019-04-03 18:35

As friiky said, @SuppressLint("WrongCall") fixed my problem. However it must be in front of the method name, not the above.

What I did is put mouse over the error code, right click and select Add @SuppressLint("WrongCall")

查看更多
Summer. ? 凉城
3楼-- · 2019-04-03 18:38

Note that in the case of drawing, overriding draw() and calling super.draw is often used when a ViewGroup wants to draw content over its child views. Content drawn in onDraw will appear under children.

查看更多
干净又极端
4楼-- · 2019-04-03 18:43

onDraw gives you a canvas to draw to the screen.

draw() allows you to manually draw a canvas to the screen (you have to make the canvas yourself).

查看更多
该账号已被封号
5楼-- · 2019-04-03 18:47

I tried cleaning my project and it did solve the problem. Try it.

查看更多
老娘就宠你
6楼-- · 2019-04-03 18:48

SurfaceView.draw() basically calls View.draw(); If you want to implement your drawing, you should do it in View.onDraw() which is for you to implement which even says in the source code comments.

This method is called by ViewGroup.drawChild() to have each child view draw itself. This draw() method is an implementation detail and is not intended to be overridden or to be called from anywhere else other than ViewGroup.drawChild().

As for difference between them:
draw():

13416        /*
13417         * Draw traversal performs several drawing steps which must be executed
13418         * in the appropriate order:
13419         *
13420         *      1. Draw the background
13421         *      2. If necessary, save the canvas' layers to prepare for fading
13422         *      3. Draw view's content
13423         *      4. Draw children
13424         *      5. If necessary, draw the fading edges and restore layers
13425         *      6. Draw decorations (scrollbars for instance)
13426         */

onDraw() is empty. Its for you to implement.

查看更多
萌系小妹纸
7楼-- · 2019-04-03 18: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.

查看更多
登录 后发表回答