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?
The parameter to onDraw() is a Canvas object that the view can use to draw itself. The Canvas class defines methods for drawing text, lines, bitmaps, and many other graphics primitives. You can use these methods in onDraw() to create your custom user interface (UI).
Canvas is a class in Android that performs 2D drawing of different objects onto the screen. The saying “a blank canvas” is very similar to what a Canvas object is on Android. It is basically, an empty space to draw onto. The Canvas class is not a new concept, this class is actually wrapping a SKCanvas under the hood.
This can be achieved simply by extending the View class and define an onDraw() callback method. Inside your View component's onDraw(), use the Canvas given to you for all your drawing, using various Canvas. draw... () methods (Ex: canvas.
You can't call onDraw directly. Instead you should call invalidate to tell the view that it needs to redraw itself.
There is difference between them
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.
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 of a view that you can call after the view is rendered for the first time. This function can be used for custom drawing on any view. You need to provide the canvas on which this view will rendered and also you have to do all the drawing on the canvas before calling this function.
Just if someone was still looking for answer like me and didn't find it.
The draw() method is called by the framework when the view need to be re-drawn and the draw() method then calls the onDraw() to draw the view's content.
void draw(Canvas canvas) { ..... do default stuff (background, layers) onDraw(canvas) ..... do other stuff ( scroll bars, fading edges, children) }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With