mini mini - 10 days ago 8
Android Question

View onDraw(Canvas c) versus draw(Canvas c) in android?

I am new to android development, I am exploring about

View
. I come across to known two methods
onDraw(Canvas c)
and
draw(Canvas c)
.

Could please explain me the difference and usage of these two methods? Which method will give better performance(FPS) when updating canvas with images?

Answer

There is difference between them

  1. The onDraw(Canvas c) is a override method and automatically called when the view is being rendered. Here you can do your additional drawing like make circles, lines or whatever you want.

  2. The draw(Canvas c) is used to manually render this view (and all of its children) to the given canvas. The view must have already done a full layout before this function is called. When implementing a view, implement onDraw(android.graphics.Canvas) instead of overriding this method. If you do need to override this method, call the superclass version.

Or in simple words draw(Canvas c) is simply a function that is not automatically called when the view is rendered. User is needed to provide the canvas on which this view will rendered and user also have to do all the drawing on the canvas before calling this function.

Comments