Override onDraw() or draw()?

后端 未结 6 943
隐瞒了意图╮
隐瞒了意图╮ 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:50

    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.

提交回复
热议问题